using System;
using System.Drawing;
using FastReport.Table;

namespace FastReport.AdvMatrix
{
    internal enum DragPosition
    {
        Left,
        Right,
        Top,
        Bottom,
        Inside
    }

    internal enum HeaderInsertPosition
    {
        Before,
        After,
        Parent,
        Child,
        Inside
    }

    internal class DragInfo
    {
        public TableCell SelectedCell;
        public MatrixElement Source;
        public TableCell TargetCell;
        public MatrixElement Target;
        public DragPosition Position;
        public RectangleF Indicator;
        public bool DragSelectedCell;

        public HeaderInsertPosition InsertPosition
        {
            get
            {
                switch (Position)
                {
                    case DragPosition.Left:
                        return Target == MatrixElement.Column ? HeaderInsertPosition.Before : HeaderInsertPosition.Parent;
                    case DragPosition.Right:
                        return Target == MatrixElement.Column ? HeaderInsertPosition.After : HeaderInsertPosition.Child;
                    case DragPosition.Top:
                        return Target == MatrixElement.Column ? HeaderInsertPosition.Parent : HeaderInsertPosition.Before;
                    case DragPosition.Bottom:
                        return Target == MatrixElement.Column ? HeaderInsertPosition.Child : HeaderInsertPosition.After;
                }
                return HeaderInsertPosition.Inside;
            }
        }

        public void SetDragPosition(TableCell cell, PointF point)
        {
            if (point.X < cell.AbsLeft + cell.Width / 4)
            {
                Position = DragPosition.Left;
                Indicator = new RectangleF(cell.Left, cell.Top, 0, cell.Height);
            }
            else if (point.X > cell.AbsRight - cell.Width / 4)
            {
                Position = DragPosition.Right;
                Indicator = new RectangleF(cell.Right, cell.Top, 0, cell.Height);
            }
            else if (point.Y < cell.AbsTop + cell.Height / 3)
            {
                Position = DragPosition.Top;
                Indicator = new RectangleF(cell.Left, cell.Top, cell.Width, 0);
            }
            else if (point.Y > cell.AbsBottom - cell.Height / 3)
            {
                Position = DragPosition.Bottom;
                Indicator = new RectangleF(cell.Left, cell.Bottom, cell.Width, 0);
            }
            else
            {
                Position = DragPosition.Inside;
                Indicator = RectangleF.Empty;
            }
        }

        public void SetHeaderTargetInfo(TableCell cell, PointF point)
        {
            HeaderDescriptor descr = cell.GetHeaderDescriptor();
            Target = descr.IsColumn ? MatrixElement.Column : MatrixElement.Row;
            TargetCell = cell;
            if (descr.IsEmpty())
            {
                // dragging into empty dummy cell
                Position = DragPosition.Inside;
                Indicator = RectangleF.Empty;
                return;
            }

            SetDragPosition(cell, point);

            if (Position == DragPosition.Inside && Source != MatrixElement.None)
            {
                // do not allow this drag if dragged not from Data window
                Target = MatrixElement.None;
                TargetCell = null;
            }
        }

        public void SetCellTargetInfo(TableCell cell, PointF point)
        {
            Target = MatrixElement.Cell;
            TargetCell = cell;
            if (String.IsNullOrEmpty(cell.Text))
            {
                // dragging into empty dummy cell
                Position = DragPosition.Inside;
                Indicator = RectangleF.Empty;
                return;
            }

            SetDragPosition(cell, point);
        }
    }
}