using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace _SCREEN_CAPTURE
{
    /// <summary>
    /// 
    /// </summary>
    [Designer(typeof(ToolButtonDesigner))]
    internal partial class ToolButton : Control
    {
        /// <summary>
        /// 
        /// </summary>
        public ToolButton() {
            InitializeComponent();
        }

        private Image btnImage;
        /// <summary>
        /// 
        /// </summary>
        public Image BtnImage {
            get { return btnImage; }
            set {
                btnImage = value;
                this.Invalidate();
            }
        }

        private bool isSelectedBtn;
        /// <summary>
        /// 
        /// </summary>
        public bool IsSelectedBtn {
            get { return isSelectedBtn; }
            set { 
                isSelectedBtn = value;
                if (!isSelectedBtn) this.isSingleSelectedBtn = false;
            }
        }

        private bool isSingleSelectedBtn;
        /// <summary>
        /// 
        /// </summary>
        public bool IsSingleSelectedBtn {
            get { return isSingleSelectedBtn; }
            set { 
                isSingleSelectedBtn = value;
                if (isSingleSelectedBtn) this.isSelectedBtn = true;
            }
        }

        private bool isSelected;
        /// <summary>
        /// 
        /// </summary>
        public bool IsSelected {
            get { return isSelected; }
            set {
                //if (!this.isSelectedBtn) return;
                if (value == isSelected) return;
                isSelected = value;
                this.Invalidate();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override string Text {
            get {
                return base.Text;
            }
            set {
                base.Text = value;
                Size se = TextRenderer.MeasureText(this.Text, this.Font);
                this.Width = se.Width + 21;
            }
        }

        private bool m_bMouseEnter;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e) {
            m_bMouseEnter = true;
            this.Invalidate();
            base.OnMouseEnter(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e) {
            m_bMouseEnter = false;
            this.Invalidate();
            base.OnMouseLeave(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(EventArgs e) {
            if (this.isSelectedBtn) {
                if (this.isSelected) {
                    if (!this.isSingleSelectedBtn) {
                        this.isSelected = false;
                        this.Invalidate();
                    }
                } else {
                    this.isSelected = true; this.Invalidate();
                    for (int i = 0, len = this.Parent.Controls.Count; i < len; i++) {
                        if (this.Parent.Controls[i] is ToolButton button && this.Parent.Controls[i] != this) {
                            if (button.isSelected)
                                button.IsSelected = false;
                        }
                    }
                }
            }
            this.Focus();
            base.OnClick(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDoubleClick(EventArgs e) {
            this.OnClick(e);
            base.OnDoubleClick(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e) {
            Graphics g = e.Graphics;
            if (m_bMouseEnter) {
                g.FillRectangle(Brushes.LightBlue, this.ClientRectangle);
                g.DrawRectangle(Pens.DarkCyan, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            }
            if (this.btnImage == null)
                g.DrawImage(global::MyDb.Properties.Resources.none, new Rectangle(2, 2, 17, 17));
            else
                g.DrawImage(this.btnImage, new Rectangle(2, 2, 17, 17));
            g.DrawString(this.Text, this.Font, Brushes.Black, 21, (this.Height - this.Font.Height) / 2);
            if (this.isSelected)
                g.DrawRectangle(Pens.DarkCyan, new Rectangle(0, 0, this.Width - 1, this.Height - 1));

            base.OnPaint(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="specified"></param>
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
            base.SetBoundsCore(x, y, TextRenderer.MeasureText(this.Text, this.Font).Width + 21, 21, specified);
        }
    }
}
