using MGdu.WinFormUI;
using MGdu.WinFormUI.MyGraphics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeClock.API
{
    class ControlBoxManager
    {
        private DbOp.FrmSticky _owner;

        private WLButton closeBtn;
        private WLButton AddBtn;
        #region event handler

        private void CloseBtnClick(object sender, EventArgs e)
        {
            _owner.CloseByUser();
        }
        private void AddBtnClick(object sender, EventArgs e)
        {
            _owner.AddClick();
        }
        #endregion
        public static Color ChangeColor(Color color, float correctionFactor)
        {
            float red = (float)color.R;
            float green = (float)color.G;
            float blue = (float)color.B;
            if (correctionFactor < 0)
            {
                correctionFactor = 1 + correctionFactor;
                red *= correctionFactor;
                green *= correctionFactor;
                blue *= correctionFactor;
            }
            else
            {
                red = (255 - red) * correctionFactor + red;
                green = (255 - green) * correctionFactor + green;
                blue = (255 - blue) * correctionFactor + blue;
            }
            if (red < 0) red = 0;
            if (red > 255) red = 255;
            if (green < 0) green = 0;
            if (green > 255) green = 255;
            if (blue < 0) blue = 0;
            if (blue > 255) blue = 255;
            return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
        }
        private Color _ButtonColor = Color.Gray;
        /// <summary>
        /// 按钮字体颜色
        /// </summary>
        [Category("GMForm")]
        [Description("按钮字体颜色")]
        public Color ButtonColor
        {
            get { return _ButtonColor; }
            set
            {
                _ButtonColor = value;
            }
        }
        private void SetControlBoxColor()
        {
            ButtonColorTable closeTable = new ButtonColorTable();
            closeTable.ForeColorNormal = closeTable.ForeColorHover
                = closeTable.ForeColorPressed = _ButtonColor;
            closeTable.BackColorHover = ChangeColor(_owner.BackColor,-0.3f);
            closeTable.BackColorPressed = Color.FromArgb(240, 231, 232);
            closeBtn.ColorTable = closeTable;
            AddBtn.ColorTable = closeTable;
            //closeBtn.BackImageNormal = _owner.XTheme.CloseBoxBackImageNormal;
            //closeBtn.BackImageHover = _owner.XTheme.CloseBoxBackImageHover;
            //closeBtn.BackImagePressed = _owner.XTheme.CloseBoxBackImagePressed;
        }
        public void ButtonVisible(bool visible)
        {
            AddBtn.Visible = visible;
            closeBtn.Visible = visible;
        }
        public bool AddBtnVisible 
        {
            get { return AddBtn.Visible; }
        }
        private void BtnIni()
        {
            // add box
            AddBtn = new WLButton(_owner);
            AddBtn.Visible = true;
            AddBtn.Bounds = _owner.AddBoxRect;

            AddBtn.Click += new EventHandler(AddBtnClick);
            AddBtn.ForePathGetter = new ButtonForePathGetter(
                    GraphicsPathHelper.CreatePlusFlag);
            // close box
            closeBtn = new WLButton(_owner);
            closeBtn.Visible = true;
            closeBtn.Bounds = _owner.CloseBoxRect;

            closeBtn.Click += new EventHandler(CloseBtnClick);
            closeBtn.ForePathGetter = new ButtonForePathGetter(
                    GraphicsPathHelper.CreateCloseFlagPath);
            SetControlBoxColor();
        }

        public ControlBoxManager(DbOp.FrmSticky owner)
        {
            _owner = owner;
            BtnIni();
        }

        public void FormResize()
        {
            AddBtn.Bounds = _owner.AddBoxRect;
            closeBtn.Bounds = _owner.CloseBoxRect;
        }

        public void MouseOperation(Point location, MouseOperationType type)
        {
            AddBtn.MouseOperation(location, type);
            closeBtn.MouseOperation(location, type);
        }

        public void DrawBoxes(Graphics g)
        {
            if (_owner.ControlBox)
            {
                AddBtn.DrawButton(g);
                closeBtn.DrawButton(g);
            }
        }

        public void ResetBoxColor()
        {
            SetControlBoxColor();
        }
    }
}
