using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using FastReport.Utils;
#if !MONO
using FastReport.DevComponents.DotNetBar;
#endif

namespace FastReport.Design.Toolbars
{
    internal class LayoutToolbar : ToolbarBase
    {
        #region Fields
#if !MONO
        public ButtonItem btnAlignToGrid;
        public ButtonItem btnLeft;
        public ButtonItem btnCenter;
        public ButtonItem btnRight;
        public ButtonItem btnTop;
        public ButtonItem btnMiddle;
        public ButtonItem btnBottom;
        public ButtonItem btnSameWidth;
        public ButtonItem btnSameHeight;
        public ButtonItem btnSameSize;
        public ButtonItem btnSizeToGrid;
        public ButtonItem btnSpaceHorizontally;
        public ButtonItem btnIncreaseHorizontalSpacing;
        public ButtonItem btnDecreaseHorizontalSpacing;
        public ButtonItem btnRemoveHorizontalSpacing;
        public ButtonItem btnSpaceVertically;
        public ButtonItem btnIncreaseVerticalSpacing;
        public ButtonItem btnDecreaseVerticalSpacing;
        public ButtonItem btnRemoveVerticalSpacing;
        public ButtonItem btnCenterHorizontally;
        public ButtonItem btnCenterVertically;
        public ButtonItem btnBringToFront;
        public ButtonItem btnSendToBack;
#else
        public ToolStripButton btnAlignToGrid;
        public ToolStripButton btnLeft;
        public ToolStripButton btnCenter;
        public ToolStripButton btnRight;
        public ToolStripButton btnTop;
        public ToolStripButton btnMiddle;
        public ToolStripButton btnBottom;
        public ToolStripButton btnSameWidth;
        public ToolStripButton btnSameHeight;
        public ToolStripButton btnSameSize;
        public ToolStripButton btnSizeToGrid;
        public ToolStripButton btnSpaceHorizontally;
        public ToolStripButton btnIncreaseHorizontalSpacing;
        public ToolStripButton btnDecreaseHorizontalSpacing;
        public ToolStripButton btnRemoveHorizontalSpacing;
        public ToolStripButton btnSpaceVertically;
        public ToolStripButton btnIncreaseVerticalSpacing;
        public ToolStripButton btnDecreaseVerticalSpacing;
        public ToolStripButton btnRemoveVerticalSpacing;
        public ToolStripButton btnCenterHorizontally;
        public ToolStripButton btnCenterVertically;
        public ToolStripButton btnBringToFront;
        public ToolStripButton btnSendToBack;
#endif
        #endregion

        #region Private Methods
        private void UpdateControls()
        {
            bool oneObjSelected = Designer.SelectedComponents.Count > 0;
            bool threeObjSelected = Designer.SelectedComponents.Count >= 3;
            bool severalObjSelected = Designer.SelectedComponents.Count > 1;
            bool canChangeOrder = Designer.SelectedComponents.Count > 0 &&
              Designer.SelectedComponents.First.HasFlag(Flags.CanChangeOrder);
            bool canMove = Designer.SelectedComponents.Count > 0 &&
              Designer.SelectedComponents.First.HasFlag(Flags.CanMove);
            bool canResize = Designer.SelectedComponents.Count > 0 &&
              Designer.SelectedComponents.First.HasFlag(Flags.CanResize);

            btnAlignToGrid.Enabled = oneObjSelected && canMove;
            btnLeft.Enabled = severalObjSelected && canMove;
            btnCenter.Enabled = severalObjSelected && canMove;
            btnRight.Enabled = severalObjSelected && canMove;
            btnTop.Enabled = severalObjSelected && canMove;
            btnMiddle.Enabled = severalObjSelected && canMove;
            btnBottom.Enabled = severalObjSelected && canMove;
            btnSameWidth.Enabled = severalObjSelected && canResize;
            btnSameHeight.Enabled = severalObjSelected && canResize;
            btnSameSize.Enabled = severalObjSelected && canResize;
            btnSizeToGrid.Enabled = oneObjSelected && canResize;
            btnSpaceHorizontally.Enabled = threeObjSelected && canMove;
            btnIncreaseHorizontalSpacing.Enabled = severalObjSelected && canMove;
            btnDecreaseHorizontalSpacing.Enabled = severalObjSelected && canMove;
            btnRemoveHorizontalSpacing.Enabled = severalObjSelected && canMove;
            btnSpaceVertically.Enabled = threeObjSelected && canMove;
            btnIncreaseVerticalSpacing.Enabled = severalObjSelected && canMove;
            btnDecreaseVerticalSpacing.Enabled = severalObjSelected && canMove;
            btnRemoveVerticalSpacing.Enabled = severalObjSelected && canMove;
            btnCenterHorizontally.Enabled = oneObjSelected && canMove;
            btnCenterVertically.Enabled = oneObjSelected && canMove;
            btnBringToFront.Enabled = canChangeOrder;
            btnSendToBack.Enabled = canChangeOrder;
        }

        private void btnAlignToGrid_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.AlignToGrid();
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.AlignLeft();
        }

        private void btnCenter_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.AlignCenter();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.AlignRight();
        }

        private void btnTop_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.AlignTop();
        }

        private void btnMiddle_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.AlignMiddle();
        }

        private void btnBottom_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.AlignBottom();
        }

        private void btnSameWidth_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.SameWidth();
        }

        private void btnSameHeight_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.SameHeight();
        }

        private void btnSameSize_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.SameSize();
        }

        private void btnCenterHorizontally_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.CenterHorizontally();
        }

        private void btnCenterVertically_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.CenterVertically();
        }

        private void btnSizeToGrid_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.SizeToGrid();
        }

        private void btnSpaceHorizontally_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.SpaceHorizontally();
        }

        private void btnIncreaseHorizontalSpacing_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.IncreaseHorizontalSpacing();
        }

        private void btnDecreaseHorizontalSpacing_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.DecreaseHorizontalSpacing();
        }

        private void btnRemoveHorizontalSpacing_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.RemoveHorizontalSpacing();
        }

        private void btnSpaceVertically_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.SpaceVertically();
        }

        private void btnIncreaseVerticalSpacing_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.IncreaseVerticalSpacing();
        }

        private void btnDecreaseVerticalSpacing_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.DecreaseVerticalSpacing();
        }

        private void btnRemoveVerticalSpacing_Click(object sender, EventArgs e)
        {
            Designer.SelectedComponents.RemoveVerticalSpacing();
        }

        #endregion

        #region Public Methods
        public override void SelectionChanged()
        {
            base.SelectionChanged();
            UpdateControls();
        }

        public override void UpdateContent()
        {
            base.UpdateContent();
            UpdateControls();
        }

        public override void Localize()
        {
            base.Localize();
            MyRes res = new MyRes("Designer,Toolbar,Layout");
            Text = res.Get("");

            SetItemText(btnAlignToGrid, res.Get("AlignToGrid"));
            SetItemText(btnLeft, res.Get("Left"));
            SetItemText(btnCenter, res.Get("Center"));
            SetItemText(btnRight, res.Get("Right"));
            SetItemText(btnTop, res.Get("Top"));
            SetItemText(btnMiddle, res.Get("Middle"));
            SetItemText(btnBottom, res.Get("Bottom"));
            SetItemText(btnSameWidth, res.Get("SameWidth"));
            SetItemText(btnSameHeight, res.Get("SameHeight"));
            SetItemText(btnSameSize, res.Get("SameSize"));
            SetItemText(btnSizeToGrid, res.Get("SizeToGrid"));
            SetItemText(btnSpaceHorizontally, res.Get("SpaceHorizontally"));
            SetItemText(btnIncreaseHorizontalSpacing, res.Get("IncreaseHorizontalSpacing"));
            SetItemText(btnDecreaseHorizontalSpacing, res.Get("DecreaseHorizontalSpacing"));
            SetItemText(btnRemoveHorizontalSpacing, res.Get("RemoveHorizontalSpacing"));
            SetItemText(btnSpaceVertically, res.Get("SpaceVertically"));
            SetItemText(btnIncreaseVerticalSpacing, res.Get("IncreaseVerticalSpacing"));
            SetItemText(btnDecreaseVerticalSpacing, res.Get("DecreaseVerticalSpacing"));
            SetItemText(btnRemoveVerticalSpacing, res.Get("RemoveVerticalSpacing"));
            SetItemText(btnCenterHorizontally, res.Get("CenterHorizontally"));
            SetItemText(btnCenterVertically, res.Get("CenterVertically"));
            SetItemText(btnBringToFront, res.Get("BringToFront"));
            SetItemText(btnSendToBack, res.Get("SendToBack"));
        }
        #endregion

        public LayoutToolbar(Designer designer)
            : base(designer)
        {
            Name = "LayoutToolbar";

#if !MONO
            btnAlignToGrid = CreateButton("btnLayoutAlignToGrid", 98, btnAlignToGrid_Click);
            btnSizeToGrid = CreateButton("btnLayoutSizeToGrid", 57, btnSizeToGrid_Click);
            btnLeft = CreateButton("btnLayoutLeft", 41, btnLeft_Click);
            btnLeft.BeginGroup = true;
            btnCenter = CreateButton("btnLayoutCenter", 42, btnCenter_Click);
            btnRight = CreateButton("btnLayoutRight", 45, btnRight_Click);
            btnTop = CreateButton("btnLayoutTop", 46, btnTop_Click);
            btnTop.BeginGroup = true;
            btnMiddle = CreateButton("btnLayoutMiddle", 47, btnMiddle_Click);
            btnBottom = CreateButton("btnLayoutBottom", 50, btnBottom_Click);
            btnSameWidth = CreateButton("btnLayoutSameWidth", 83, btnSameWidth_Click);
            btnSameWidth.BeginGroup = true;
            btnSameHeight = CreateButton("btnLayoutSameHeight", 84, btnSameHeight_Click);
            btnSameSize = CreateButton("btnLayoutSameSize", 91, btnSameSize_Click);
            btnSpaceHorizontally = CreateButton("btnLayoutSpaceHorizontally", 44, btnSpaceHorizontally_Click);
            btnSpaceHorizontally.BeginGroup = true;
            btnIncreaseHorizontalSpacing = CreateButton("btnLayoutIncreaseHorizontalSpacing", 92, btnIncreaseHorizontalSpacing_Click);
            btnDecreaseHorizontalSpacing = CreateButton("btnLayoutDecreaseHorizontalSpacing", 93, btnDecreaseHorizontalSpacing_Click);
            btnRemoveHorizontalSpacing = CreateButton("btnLayoutRemoveHorizontalSpacing", 94, btnRemoveHorizontalSpacing_Click);
            btnSpaceVertically = CreateButton("btnLayoutSpaceVertically", 49, btnSpaceVertically_Click);
            btnSpaceVertically.BeginGroup = true;
            btnIncreaseVerticalSpacing = CreateButton("btnLayoutIncreaseVerticalSpacing", 95, btnIncreaseVerticalSpacing_Click);
            btnDecreaseVerticalSpacing = CreateButton("btnLayoutDecreaseVerticalSpacing", 96, btnDecreaseVerticalSpacing_Click);
            btnRemoveVerticalSpacing = CreateButton("btnLayoutRemoveVerticalSpacing", 97, btnRemoveVerticalSpacing_Click);
            btnCenterHorizontally = CreateButton("btnLayoutCenterHorizontally", 43, btnCenterHorizontally_Click);
            btnCenterHorizontally.BeginGroup = true;
            btnCenterVertically = CreateButton("btnLayoutCenterVertically", 48, btnCenterVertically_Click);
            btnBringToFront = CreateButton("btnLayoutBringToFront", 14, Designer.cmdBringToFront.Invoke);
            btnBringToFront.BeginGroup = true;
            btnSendToBack = CreateButton("btnLayoutSendToBack", 15, Designer.cmdSendToBack.Invoke);

            Items.AddRange(new BaseItem[] {
                btnAlignToGrid, btnSizeToGrid,
                btnLeft, btnCenter, btnRight,
                btnTop, btnMiddle, btnBottom, 
                btnSameWidth, btnSameHeight, btnSameSize, 
                btnSpaceHorizontally, btnIncreaseHorizontalSpacing, btnDecreaseHorizontalSpacing, btnRemoveHorizontalSpacing,
                btnSpaceVertically, btnIncreaseVerticalSpacing, btnDecreaseVerticalSpacing, btnRemoveVerticalSpacing, 
                btnCenterHorizontally, btnCenterVertically, 
                btnBringToFront, btnSendToBack, CustomizeItem });
#else
            btnAlignToGrid = CreateButton("btnLayoutAlignToGrid", 98, btnAlignToGrid_Click);
            btnSizeToGrid = CreateButton("btnLayoutSizeToGrid", 57, btnSizeToGrid_Click);
            btnLeft = CreateButton("btnLayoutLeft", 41, btnLeft_Click);
            btnCenter = CreateButton("btnLayoutCenter", 42, btnCenter_Click);
            btnRight = CreateButton("btnLayoutRight", 45, btnRight_Click);
            btnTop = CreateButton("btnLayoutTop", 46, btnTop_Click);
            btnMiddle = CreateButton("btnLayoutMiddle", 47, btnMiddle_Click);
            btnBottom = CreateButton("btnLayoutBottom", 50, btnBottom_Click);
            btnSameWidth = CreateButton("btnLayoutSameWidth", 83, btnSameWidth_Click);
            btnSameHeight = CreateButton("btnLayoutSameHeight", 84, btnSameHeight_Click);
            btnSameSize = CreateButton("btnLayoutSameSize", 91, btnSameSize_Click);
            btnSpaceHorizontally = CreateButton("btnLayoutSpaceHorizontally", 44, btnSpaceHorizontally_Click);
            btnIncreaseHorizontalSpacing = CreateButton("btnLayoutIncreaseHorizontalSpacing", 92, btnIncreaseHorizontalSpacing_Click);
            btnDecreaseHorizontalSpacing = CreateButton("btnLayoutDecreaseHorizontalSpacing", 93, btnDecreaseHorizontalSpacing_Click);
            btnRemoveHorizontalSpacing = CreateButton("btnLayoutRemoveHorizontalSpacing", 94, btnRemoveHorizontalSpacing_Click);
            btnSpaceVertically = CreateButton("btnLayoutSpaceVertically", 49, btnSpaceVertically_Click);
            btnIncreaseVerticalSpacing = CreateButton("btnLayoutIncreaseVerticalSpacing", 95, btnIncreaseVerticalSpacing_Click);
            btnDecreaseVerticalSpacing = CreateButton("btnLayoutDecreaseVerticalSpacing", 96, btnDecreaseVerticalSpacing_Click);
            btnRemoveVerticalSpacing = CreateButton("btnLayoutRemoveVerticalSpacing", 97, btnRemoveVerticalSpacing_Click);
            btnCenterHorizontally = CreateButton("btnLayoutCenterHorizontally", 43, btnCenterHorizontally_Click);
            btnCenterVertically = CreateButton("btnLayoutCenterVertically", 48, btnCenterVertically_Click);
            btnBringToFront = CreateButton("btnLayoutBringToFront", 14, Designer.cmdBringToFront.Invoke);
            btnSendToBack = CreateButton("btnLayoutSendToBack", 15, Designer.cmdSendToBack.Invoke);

            Items.AddRange(new ToolStripItem[] {
                btnAlignToGrid, btnSizeToGrid, new ToolStripSeparator(),
                btnLeft, btnCenter, btnRight, new ToolStripSeparator(),
                btnTop, btnMiddle, btnBottom,  new ToolStripSeparator(),
                btnSameWidth, btnSameHeight, btnSameSize,  new ToolStripSeparator(),
                btnSpaceHorizontally, btnIncreaseHorizontalSpacing, btnDecreaseHorizontalSpacing, btnRemoveHorizontalSpacing, new ToolStripSeparator(),
                btnSpaceVertically, btnIncreaseVerticalSpacing, btnDecreaseVerticalSpacing, btnRemoveVerticalSpacing, new ToolStripSeparator(),
                btnCenterHorizontally, btnCenterVertically,  new ToolStripSeparator(),
                btnBringToFront, btnSendToBack });
#endif

            Localize();
            UpdateDpiDependencies();
        }

    }

}
