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

namespace Corp3s.Controls
{
    public partial class StatusButton : Control
    {
        private Image m_BackImage;
        private Image m_IconImage;
        private bool m_bShowIcon;
        private Color m_hoverColor;

        private ButtonStatus m_eStatus = ButtonStatus.Normal;

        public StatusButton()
        {
            InitializeComponent();
        }

        public Color HoverColor
        {
            get { return m_hoverColor; }
            set
            {
                if (m_hoverColor != value)
                {
                    m_hoverColor = value;
                    this.Invalidate();
                }
            }
        }

        public Image BackImage
        {
            get { return m_BackImage; }
            set
            {
                m_BackImage = value;
                this.Size = m_BackImage.Size;
            }
        }

        public bool DisplayIcon
        {
            get { return m_bShowIcon; }
            set 
            {
                if (m_bShowIcon == value)
                    return;

                m_bShowIcon = value;
                this.Invalidate();
            }
        }

        public Image IconImage
        {
            get { return m_IconImage; }
            set
            {
                if (m_IconImage == value)
                    return;

                m_IconImage = value;
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawImage(e.Graphics, ClientRectangle);
            DrawIcon(e.Graphics, ClientRectangle);
            DrawString(e.Graphics, ClientRectangle);
            if (m_eStatus == ButtonStatus.Pressed)
                DrawBorder(e.Graphics, ClientRectangle, HoverColor);
        }

        private void DrawIcon(Graphics g, Rectangle bound)
        {
            if (m_bShowIcon == false || m_IconImage == null)
                return;

            int x = 5;
            int y = (bound.Height - m_IconImage.Height) / 2;

            g.DrawImage(m_IconImage, x, y);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (m_eStatus != ButtonStatus.Pressed)
            {
                m_eStatus = ButtonStatus.Pressed;
                this.Invalidate();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (m_eStatus != ButtonStatus.Normal)
            {
                m_eStatus = ButtonStatus.Normal;
                this.Invalidate();
            }
        }

        private void DrawImage(Graphics g, Rectangle rect)
        {
            if (BackImage == null)
                return;

            g.DrawImage(m_BackImage, 0, 0);            
        }

        private void DrawBorder(Graphics g, Rectangle rect, Color color)
        {
            Pen pen = new Pen(color);
            Rectangle bound = new Rectangle(1, 1, Width - 3, Height - 3);
            g.DrawRectangle(pen, bound);
            pen.Dispose();
        }

        private void DrawString(Graphics g, Rectangle rect)
        {
            if (Text == null)
                return;
            
            SizeF size = g.MeasureString(Text, this.Font);
            int x = (int)(rect.Width - size.Width) / 2;
            int y = (int)(rect.Height - size.Height) / 2;

            if (m_bShowIcon == true && m_IconImage != null)
            {
                int width = rect.Width - m_IconImage.Width - 3;
                if (width > size.Width)
                    x = (int)((width - size.Width) / 2) + m_IconImage.Width + 3;
                else
                    x = m_IconImage.Width + 5;
            }

            Brush brush = new SolidBrush(ForeColor);
            g.DrawString(Text, Font, brush, x, y);
            brush.Dispose();
        }
        protected enum ButtonStatus { Normal, Pressed }        
    }
}
