using System.Drawing;
using System.Windows.Forms;

namespace RyzStudio.Windows.Forms
{
    public class TFlatButton : Label
    {
        public class ButtonStyle
        {
            public Color BackColour { get; set; } = Color.Transparent;
            public Color PenColour { get; set; } = Color.Transparent;
        }

        public enum FlatButtonState
        {
            Idle = 0,
            Hover,
            Down
        }

        protected FlatButtonState controlState = FlatButtonState.Idle;

        public TFlatButton() : base()
        {
            this.AutoSize = false;
            this.ImageAlign = ContentAlignment.MiddleCenter;
            this.TextAlign = ContentAlignment.MiddleCenter;

            // customise
            this.StyleOver = new ButtonStyle()
            {
                BackColour = Color.FromArgb(51, 51, 51),
                PenColour = Color.White
            };
            this.StyleDown = new ButtonStyle()
            {
                BackColour = Color.FromArgb(179, 179, 179),
                PenColour = Color.Black
            };
            this.StyleDefault = new ButtonStyle()
            {
                BackColour = Color.White,
                PenColour = Color.Black
            };

            this.VisualState = FlatButtonState.Idle;

            this.Click += delegate { this.OnClick(null); };
            this.MouseEnter += delegate { this.VisualState = FlatButtonState.Hover; };
            this.MouseLeave += delegate { this.VisualState = FlatButtonState.Idle; };
            this.MouseDown += delegate { this.VisualState = FlatButtonState.Down; };
            this.MouseUp += delegate { this.VisualState = FlatButtonState.Idle; };
        }

        protected FlatButtonState VisualState
        {
            get { return controlState; }
            set
            {
                switch (value)
                {
                    case FlatButtonState.Idle:
                        if (this.VisualState == FlatButtonState.Down)
                        {
                            updateButton(StyleOver);
                        }
                        else
                        {
                            updateButton(StyleDefault);
                        }

                        break;
                    case FlatButtonState.Hover:
                        updateButton(StyleOver);
                        break;
                    case FlatButtonState.Down:
                        updateButton(StyleDown);
                        break;
                    default:
                        updateButton(StyleDefault);
                        break;
                }

                controlState = value;
            }
        }

        protected void updateButton(ButtonStyle style)
        {
            this.ForeColor = style.PenColour;
            this.BackColor = style.BackColour;
        }

        protected ButtonStyle StyleOver { get; set; } = new ButtonStyle();

        protected ButtonStyle StyleDown { get; set; } = new ButtonStyle();

        protected ButtonStyle StyleDefault { get; set; } = new ButtonStyle();

        public void PerformClick()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() => {
                    this.OnClick(null);
                }));
            }
            else
            {
                this.OnClick(null);
            }
        }

    }
}