using System;
using System.Windows.Forms;
using FastReport.Utils;

namespace FastReport.AdvMatrix
{
    internal class MatrixHeaderMenu : MatrixCellMenuBase
    {
        private HeaderDescriptor descr;
        
        public ContextMenuItem miAddTotal;
        public ContextMenuItem miAddTotalBefore;
        public ContextMenuItem miAddTotalAfter;

        private ContextMenuItem miInsert;
        private ContextMenuItem miInsertAfter;
        private ContextMenuItem miInsertBefore;
        private ContextMenuItem miInsertParent;
        private ContextMenuItem miInsertChild;

        private ContextMenuItem miDelete;
        private ContextMenuItem miDeleteThisItem;
        private ContextMenuItem miDeleteItemTree;
        
        private void AfterInsert(HeaderDescriptor newItem)
        {
            Change();
            
            Designer.SelectedObjects.Clear();
            Designer.SelectedObjects.Add(newItem.TemplateCell);
            Designer.SelectionChanged(null);
        }

        private void AfterDelete()
        {
            Change();

            Designer.SelectedObjects.Clear();
            Designer.SelectedObjects.Add(Matrix);
            Designer.SelectionChanged(null);
        }

        private void AddTotalAfter_Click(object sender, EventArgs e)
        {
            HeaderDescriptor d = new HeaderDescriptor();
            d.DisplayText = Res.Get("ComponentsMisc,Matrix,Total");
            AfterInsert(descr.InsertAfter(d));
        }

        private void AddTotalBefore_Click(object sender, EventArgs e)
        {
            HeaderDescriptor d = new HeaderDescriptor();
            d.DisplayText = Res.Get("ComponentsMisc,Matrix,Total");
            AfterInsert(descr.InsertBefore(d));
        }

        private void InsertAfter_Click(object sender, EventArgs e)
        {
            AfterInsert(descr.InsertAfter(new HeaderDescriptor()));
        }

        private void InsertBefore_Click(object sender, EventArgs e)
        {
            AfterInsert(descr.InsertBefore(new HeaderDescriptor()));
        }

        private void InsertParent_Click(object sender, EventArgs e)
        {
            AfterInsert(descr.InsertParent(new HeaderDescriptor()));
        }

        private void InsertChild_Click(object sender, EventArgs e)
        {
            AfterInsert(descr.InsertChild(new HeaderDescriptor()));
        }

        private void DeleteThisItem_Click(object sender, EventArgs e)
        {
            descr.DeleteTopNItems();
            descr.DeleteThis();
            AfterDelete();
        }

        private void DeleteItemTree_Click(object sender, EventArgs e)
        {
            descr.DeleteTopNItems();
            descr.DeleteTree();
            AfterDelete();
        }

        public MatrixHeaderMenu(AdvMatrixObject matrix) : base(matrix)
        {
            descr = Cell.GetHeaderDescriptor();

            MyRes res = new MyRes("ComponentMenu,AdvMatrixHeader");

            miAddTotal = CreateMenuItem(65, res.Get("AddTotal"), null);
            miAddTotal.BeginGroup = true;
            miAddTotalAfter = CreateMenuItem(res.Get("Insert,After"), AddTotalAfter_Click);
            miAddTotalBefore = CreateMenuItem(res.Get("Insert,Before"), AddTotalBefore_Click);

            miAddTotal.DropDownItems.AddRange(new ContextMenuItem[] { miAddTotalAfter, miAddTotalBefore });

            res = new MyRes("ComponentMenu,AdvMatrixHeader,Insert");
            miInsert = CreateMenuItem(res.Get(""), null);
            miInsert.BeginGroup = true;
            
            miInsertAfter = CreateMenuItem(res.Get("After"), InsertAfter_Click);
            miInsertBefore = CreateMenuItem(res.Get("Before"), InsertBefore_Click);
            miInsertParent = CreateMenuItem(res.Get("Parent"), InsertParent_Click);
            miInsertChild = CreateMenuItem(res.Get("Child"), InsertChild_Click);

            miInsert.DropDownItems.AddRange(new ContextMenuItem[] { miInsertAfter, miInsertBefore, miInsertParent, miInsertChild });

            res = new MyRes("ComponentMenu,AdvMatrixHeader,Delete");
            miDelete = CreateMenuItem(51, res.Get(""), null);

            miDeleteThisItem = CreateMenuItem(res.Get("ThisItem"), DeleteThisItem_Click);
            miDeleteItemTree = CreateMenuItem(res.Get("ItemTree"), DeleteItemTree_Click);

            miDelete.DropDownItems.AddRange(new ContextMenuItem[] { miDeleteThisItem, miDeleteItemTree });

            int insertIndex = Items.IndexOf(miClear);
            Items.Insert(insertIndex, miAddTotal);
            Items.Insert(insertIndex + 1, miInsert);
            Items.Insert(insertIndex + 2, miDelete);

            miDelete.Enabled = !descr.IsTopNItem;
            miDeleteItemTree.Enabled = descr.Items.Count > 0;
            miClear.Visible = false;
            miMergeSplit.Visible = false;
        }
    }
}
