using System;
using System.Drawing;
using System.Windows.Forms;
using FastReport.Gauge.Simple.Progress;
using FastReport.MSChart;
using FastReport.Table;
using FastReport.Utils;

namespace FastReport.AdvMatrix
{
    internal class MatrixCellMenu : MatrixCellMenuBase
    {
        private ContextMenuItem miFunctions;
        private ContextMenuItem miPercents;
        private ContextMenuItem miInsert;
        private ContextMenuItem miInsertChart;
        private ContextMenuItem miInsertGauge;

        private void Function_Click(object sender, EventArgs e)
        {
            string func = (sender as ContextMenuItem).Tag.ToString();
            foreach (Base c in Designer.SelectedObjects)
            {
                if (c is TableCell)
                    (c as TableCell).ChangeAggregateFunction(func);
            }
            Change();
        }

        private void Percent_Click(object sender, EventArgs e)
        {
            string func = (sender as ContextMenuItem).Tag.ToString();
            foreach (Base c in Designer.SelectedObjects)
            {
                if (c is TableCell)
                    (c as TableCell).ChangePercentFunction(func);
            }

            Change();
        }

        private void InsertChart_Click(object sender, EventArgs e)
        {
            Cell.ChangeAggregateFunction("ValuesList");
            SparklineObject obj = new SparklineObject();
            obj.Parent = Cell;
            obj.Dock = DockStyle.Fill;
            obj.CreateUniqueName();
            obj.OnBeforeInsert(0);
            Change();
        }

        private void InsertGauge_Click(object sender, EventArgs e)
        {
            Cell.ChangePercentFunction("PercentOfColumnTotal");
            SimpleProgressGauge obj = new SimpleProgressGauge();
            obj.Parent = Cell;
            obj.Dock = DockStyle.Fill;
            obj.Border = Cell.Border.Clone();
            obj.Maximum = 1;
            obj.CreateUniqueName();
            Change();
        }

        public MatrixCellMenu(AdvMatrixObject matrix) : base(matrix)
        {
            MyRes res = new MyRes("ComponentMenu,AdvMatrixCell,Function");
            miFunctions = CreateMenuItem(132, res.Get(""), null);
            miFunctions.BeginGroup = true;
            
            foreach (string name in Aggregates.Names)
            {
                string resName = "ComponentMenu,AdvMatrixCell,Function," + name;
                string text = Res.StringExists(resName) ? Res.Get(resName) : name;
                ContextMenuItem mi = CreateMenuItem(text, Function_Click);
                mi.CheckOnClick = true;
                mi.Tag = name;
                miFunctions.DropDownItems.Add(mi);
            }

            res = new MyRes("ComponentMenu,AdvMatrixCell,Percent");
            miPercents = CreateMenuItem(res.Get(""), null);

            foreach (string name in CellExt.PercentNames)
            {
                ContextMenuItem mi = CreateMenuItem(res.Get(name), Percent_Click);
                mi.CheckOnClick = true;
                mi.Tag = name;
                miPercents.DropDownItems.Add(mi);
            }

            res = new MyRes("ComponentMenu,AdvMatrixCell,Insert");
            miInsert = CreateMenuItem(res.Get(""), null);
            miInsert.BeginGroup = true;
            miInsertChart = CreateMenuItem(130, res.Get("Chart"), InsertChart_Click);
            miInsertGauge = CreateMenuItem(140, res.Get("Gauge"), InsertGauge_Click);
            miInsert.DropDownItems.Add(miInsertChart);
            miInsert.DropDownItems.Add(miInsertGauge);

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

            miCollapseButton.Visible = false;
            miSortButton.Visible = false;
            miMergeSplit.Visible = false;
        }
    }
}
