using System;
using System.Drawing;
using System.Windows.Forms;
using FastReport.Design;
using FastReport.Utils;
using FastReport.Table;
using FastReport.Forms;

namespace FastReport.AdvMatrix
{
    internal class MatrixCellMenuBase : TableMenuBase
    {
        private AdvMatrixObject matrix;
        private SelectedObjectCollection selection;

        public ContextMenuItem miEdit;
        public ContextMenuItem miFormat;
        public ContextMenuItem miHyperlink;
        public ContextMenuItem miClear;
        public ContextMenuItem miMergeSplit;
        public ContextMenuItem miCollapseButton;
        public ContextMenuItem miSortButton;

        public AdvMatrixObject Matrix
        {
            get { return matrix; }
        }

        public TableCell Cell
        {
            get { return selection[0] as TableCell; }
        }

        private void miEdit_Click(object sender, EventArgs e)
        {
            Matrix.HandleCellDoubleClick(Cell);
        }

        private void miFormat_Click(object sender, EventArgs e)
        {
            using (FormatEditorForm form = new FormatEditorForm())
            {
                form.TextObject = Cell;
                if (form.ShowDialog() == DialogResult.OK)
                {
                    SelectedTextBaseObjects components = new SelectedTextBaseObjects(Designer);
                    components.Update();
                    components.SetFormat(form.Formats);
                    Change();
                }
            }
        }

        private void miHyperlink_Click(object sender, EventArgs e)
        {
            using (HyperlinkEditorForm form = new HyperlinkEditorForm())
            {
                form.ReportComponent = Cell;
                if (Cell.GetMatrixElement() == MatrixElement.Cell)
                    form.IsMatrixHyperlink = true;

                if (form.ShowDialog() == DialogResult.OK)
                {
                    SelectedReportComponents components = new SelectedReportComponents(Designer);
                    components.Update();
                    components.SetHyperlink(form.Hyperlink, form.ModifyAppearance, false);
                    Change();
                }
            }
        }

        private void miCollapseButton_Click(object sender, EventArgs e)
        {
            if (miCollapseButton.Checked)
                Cell.AddCollapseButton();
            else
                Cell.GetCollapseButton().Dispose();
            Change();
        }

        private void miSortButton_Click(object sender, EventArgs e)
        {
            if (miSortButton.Checked)
                Cell.AddSortButton();
            else
                Cell.GetSortButton().Dispose();
            Change();
        }

        private void miClear_Click(object sender, EventArgs e)
        {
            foreach (Base c in Designer.SelectedObjects)
            {
                if (c is TableCell)
                    (c as TableCell).Text = "";
            }
            Change();
        }

        private void miMergeSplit_Click(object sender, EventArgs e)
        {
            TableCell topCell = Cell;
            if (miMergeSplit.Checked)
            {
                Rectangle rect = matrix.GetSelectionRect();

                // reset spans inside selection
                for (int x = 0; x < rect.Width; x++)
                {
                    for (int y = 0; y < rect.Height; y++)
                    {
                        TableCell cell = matrix[x + rect.X, y + rect.Y];
                        cell.ColSpan = 1;
                        cell.RowSpan = 1;
                    }
                }

                topCell.ColSpan = rect.Width;
                topCell.RowSpan = rect.Height;
                selection.Clear();
                selection.Add(topCell);
            }
            else
            {
                topCell.ColSpan = 1;
                topCell.RowSpan = 1;
            }

            Change();
        }

        protected override void Change()
        {
            if (!Designer.IsPreviewPageDesigner)
            {
                Matrix.BuildTemplate();
                Designer.ActiveReportTab.ActivePageDesigner.FillObjects(false);
                Matrix.FixParentHeight();
            }
            Designer.SetModified(Matrix, "Change");
        }

        public MatrixCellMenuBase(AdvMatrixObject matrix) : base(matrix.Report.Designer)
        {
            this.matrix = matrix;
            selection = Designer.SelectedObjects;

            miEdit = CreateMenuItem(Res.Get("ComponentMenu,Component,Edit"), miEdit_Click);
            miFormat = CreateMenuItem(168, Res.Get("ComponentMenu,TextObject,Format"), miFormat_Click);
            miHyperlink = CreateMenuItem(167, Res.Get("ComponentMenu,ReportComponent,Hyperlink"), miHyperlink_Click);
            miCollapseButton = CreateMenuItem(Res.Get("ComponentMenu,AdvMatrixHeader,CollapseButton"), miCollapseButton_Click);
            miCollapseButton.BeginGroup = true;
            miCollapseButton.CheckOnClick = true;
            miSortButton = CreateMenuItem(Res.Get("ComponentMenu,AdvMatrixHeader,SortButton"), miSortButton_Click);
            miSortButton.CheckOnClick = true;
            miClear = CreateMenuItem(82, Res.Get("ComponentMenu,TextObject,Clear"), miClear_Click);
            miClear.BeginGroup = true;
            miMergeSplit = CreateMenuItem(217, Res.Get("ComponentMenu,TableCell,Merge"), miMergeSplit_Click);
            miMergeSplit.CheckOnClick = true;

            Items.AddRange(new ContextMenuItem[] { miEdit, miFormat, miHyperlink, miCollapseButton, miSortButton, miClear, miMergeSplit });

            miEdit.Enabled = selection.Count == 1;
            miCollapseButton.Enabled = miEdit.Enabled && Cell.GetMatrixElement() != MatrixElement.Corner;
            miCollapseButton.Checked = Cell.GetCollapseButton() != null;
            miSortButton.Enabled = miEdit.Enabled;
            miSortButton.Checked = Cell.GetSortButton() != null;
            bool canJoin = selection.Count > 1;
            bool canSplit = selection.Count == 1 && (Cell.ColSpan > 1 || Cell.RowSpan > 1);
            miMergeSplit.Enabled = canJoin || canSplit;
            miMergeSplit.Checked = canSplit;
            if (miMergeSplit.Checked)
                miMergeSplit.Text = Res.Get("ComponentMenu,TableCell,Split");
        }
    }
}
