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 ImageButtom : Control
    {
        private Image m_Image = null;        

        private Color m_hoverColor;

        private ButtonStatus m_eStatus = ButtonStatus.Normal;

        public ImageButtom()
        {
            InitializeComponent();
        }

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

        public Image Image
        {
            get { return m_Image; }
            set
            {
                if (value == null)
                    return;

                m_Image = value;
                this.Size = Image.Size;
                this.Invalidate();
            }
        }        

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

        private void DrawDisableState(Graphics g)
        {
            if (this.Enabled == false)
            {
                Color color = Color.FromArgb(0xc4c4c5);
                Brush brush = new SolidBrush(color);
                g.FillRectangle(brush, ClientRectangle);

                brush.Dispose();
            }
        }

        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(Image == null)
                return;

            if (this.Enabled == false)
            {
                Bitmap bitmap = new Bitmap(Image);
                bitmap = ConvertToGrayscale(bitmap);
                g.DrawImage(bitmap, 0, 0);                
            }
            else
                g.DrawImage(Image, 0, 0);            
        }

        public Bitmap ConvertToGrayscale(Bitmap source)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);
                    int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                    bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
                }
            }
            return bm;
        }

        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;

            Brush brush = null;
            if(Enabled)
                brush = new SolidBrush(ForeColor);
            else
                brush = new SolidBrush(Color.FromArgb(118, 118, 118));

            g.DrawString(Text, Font, brush, x, y);
            brush.Dispose();
        }

        public new String Text
        {
            get { return base.Text; }
            set
            {
                if(base.Text != value)
                    this.Invalidate();
                base.Text = value;                
            }
        }

        public new bool Enabled
        {
            get { return base.Enabled; }
            set 
            {
                if (value != base.Enabled)
                {
                    base.Enabled = value;
                    Invalidate();
                }
            }
        }

        protected enum ButtonStatus { Normal, Pressed }
    }
}
