using System;
using FastReport.Table;
using FastReport.Design;
using FastReport.Utils;

namespace FastReport.AdvMatrix
{
    internal class MatrixObjectMenu : TableObjectMenu
    {
        private AdvMatrixObject matrix;
        private ContextMenuItem miMatrixStyle;
        private ContextMenuItem miSwapColumnsRows;

        private new void Change()
        {
            if (!Designer.IsPreviewPageDesigner)
                matrix.BuildTemplate();
            base.Change();
        }

        private void BuildStyleMenu()
        {
            ContextMenuItem styleItem = CreateMenuItem(76, Res.Get("Designer,Toolbar,Style,NoStyle"), styleItem_Click);
            styleItem.Tag = "";
            styleItem.Checked = matrix.Style == "";
            styleItem.CheckOnClick = true;
            miMatrixStyle.DropDownItems.Add(styleItem);

            for (int i = 0; i < matrix.StyleSheet.Count; i++)
            {
                string text = matrix.StyleSheet[i].Name;
                styleItem = CreateMenuItem(Res.Get("ComponentsMisc,Matrix," + text), styleItem_Click);
                styleItem.Image = matrix.StyleSheet.GetStyleBitmap(i);
                styleItem.Tag = text;
                styleItem.Checked = matrix.Style == text;
                styleItem.CheckOnClick = true;
                miMatrixStyle.DropDownItems.Add(styleItem);
            }
        }

        private void styleItem_Click(object sender, EventArgs e)
        {
            matrix.Style = (string)(sender as ContextMenuItem).Tag;
            Change();
        }

        private void SwapColumnsRows_Click(object sender, EventArgs e)
        {
            matrix.SwapColumnsRows();
            Change();
        }

        public MatrixObjectMenu(Designer designer) : base(designer)
        {
            matrix = Designer.SelectedObjects[0] as AdvMatrixObject;
            MyRes res = new MyRes("ComponentMenu,AdvMatrixObject");

            miMatrixStyle = CreateMenuItem(87, res.Get("Style"), null);
            miMatrixStyle.BeginGroup = true;
            BuildStyleMenu();

            miSwapColumnsRows = CreateMenuItem(res.Get("Swap"), SwapColumnsRows_Click);

            int insertIndex = Items.IndexOf(miRepeatHeaders);
            Items.Insert(insertIndex, miMatrixStyle);
            Items.Insert(insertIndex + 1, miSwapColumnsRows);

            miRepeatHeaders.BeginGroup = false;
            miCanBreak.Visible = false;
            miGrowToBottom.Visible = false;
            miStyle.Visible = false;
        }
    }
}
