using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FastReport.Design;
using FastReport.Utils;
using FastReport.Table;
using FastReport.Forms;
using System.Drawing;

namespace FastReport.Matrix
{
    internal class MatrixCellMenuBase : TableMenuBase
    {
        private MatrixObject matrix;
        private MatrixElement element;
        private MatrixDescriptor descriptor;
        private SelectedObjectCollection selection;
        private TableCell topCell;

        public ContextMenuItem miEdit;
        public ContextMenuItem miFormat;
        public ContextMenuItem miHyperlink;
        public ContextMenuItem miClear;
        public ContextMenuItem miMergeSplit;
        public ContextMenuItem miDelete;

        public MatrixObject Matrix
        {
            get { return matrix; }
        }

        public MatrixElement Element
        {
            get { return element; }
        }

        public MatrixDescriptor Descriptor
        {
            get { return descriptor; }
        }

        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 (Element == 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 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)
        {
            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();
        }

        private void miDelete_Click(object sender, EventArgs e)
        {
            if ((Element == MatrixElement.Column || Element == MatrixElement.Row) &&
              (Descriptor as MatrixHeaderDescriptor).TemplateTotalCell == Cell)
            {
                (Descriptor as MatrixHeaderDescriptor).Totals = false;
            }
            else
            {
                switch (Element)
                {
                    case MatrixElement.Column:
                        Matrix.Data.Columns.Remove(Descriptor as MatrixHeaderDescriptor);
                        break;

                    case MatrixElement.Row:
                        Matrix.Data.Rows.Remove(Descriptor as MatrixHeaderDescriptor);
                        break;

                    case MatrixElement.Cell:
                        Matrix.Data.Cells.Remove(Descriptor as MatrixCellDescriptor);
                        break;
                }
            }

            Change();
        }

        protected override void Change()
        {
            Matrix.BuildTemplate();
            Designer.SetModified(Matrix, "Change");
        }

        public MatrixCellMenuBase(MatrixObject matrix, MatrixElement element, MatrixDescriptor descriptor) :
          base(matrix.Report.Designer)
        {
            this.matrix = matrix;
            this.element = element;
            this.descriptor = descriptor;
            selection = Designer.SelectedObjects;
            topCell = Cell;

            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);
            miDelete = CreateMenuItem(51, Res.Get("Designer,Menu,Edit,Delete"), miDelete_Click);
            miDelete.BeginGroup = true;
            miClear = CreateMenuItem(82, Res.Get("ComponentMenu,TextObject,Clear"), miClear_Click);
            miMergeSplit = CreateMenuItem(217, Res.Get("ComponentMenu,TableCell,Merge"), miMergeSplit_Click);
            miMergeSplit.CheckOnClick = true;

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

            bool enabled = selection.Count == 1;
            miEdit.Enabled = enabled;
            miDelete.Enabled = enabled && descriptor != null && !matrix.IsAncestor;
            bool canJoin = selection.Count > 1;
            bool canSplit = selection.Count == 1 && topCell != null && (topCell.ColSpan > 1 || topCell.RowSpan > 1);
            miMergeSplit.Enabled = canJoin || canSplit;
            miMergeSplit.Checked = canSplit;
            if (miMergeSplit.Checked)
                miMergeSplit.Text = Res.Get("ComponentMenu,TableCell,Split");
        }
    }
}
