using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace PostCardHitsuji.Ui.New
{
    public partial class DesignedLabel : UserControl
    {
        public event EventHandler OnClickedEvent;

        private Graphics graphics;
        private Design degign1;
        private Design forecusedDegign1;
        private Font textFont_;
        private SolidBrush brushBack_;

        private bool isMouseOn_;
        private bool IsMouseOn
        {
            get { return isMouseOn_; }
            set { isMouseOn_ = value; }
        }

        private string text_ = "あいうえお";
        public string TextValue
        {
            get { return text_; }
            set { text_ = value; }
        }

        public enum BackColorTypes
        {
            LightBlue = 10,
            YellowGreen = 20,
            Orange = 30
        }

        private BackColorTypes backColor_ = BackColorTypes.LightBlue;
        public BackColorTypes BackColorType
        {
            get { return backColor_; }
            set 
            { 
                backColor_ = value;
                setBackColor();
            }
        }


        public DesignedLabel()
        {
            InitializeComponent();
            setBackColor();

            graphics = this.CreateGraphics();
            degign1 = new Design();
            forecusedDegign1 = new Design();
            textFont_ = new Font(FontFamily.GenericSerif, 14, FontStyle.Bold);

            this.MouseEnter += new EventHandler(mouseEnter);
            this.MouseLeave += new EventHandler(mouseLeave);
            this.Paint += new PaintEventHandler(draw);
            this.Click += new EventHandler(onClicked);
        }

        private void setBackColor()
        {
            brushBack_ = backColor_ == BackColorTypes.LightBlue ? new SolidBrush(Color.FromArgb(150, Color.LightBlue))
                         : backColor_ == BackColorTypes.YellowGreen ? new SolidBrush(Color.FromArgb(150, Color.YellowGreen))
                         : new SolidBrush(Color.FromArgb(150, Color.Orange));

        }

        private void draw(object sender, PaintEventArgs ev)
        {
            if (isMouseOn_)
                degign1.draw(graphics, Brushes.LightGreen);
            else
                forecusedDegign1.draw(graphics, brushBack_);
            graphics.DrawString(text_, textFont_, Brushes.Black, 20, 10);
        }

        private void onClicked(object sender, EventArgs ev)
        {
            if (OnClickedEvent != null) OnClickedEvent(this, ev);
        }
        private void mouseEnter(object sender, EventArgs ev)
        {
            isMouseOn_ = true;
            draw(null, null);
        }

        private void mouseLeave(object sender, EventArgs ev)
        {
            isMouseOn_ = false;
            draw(null, null);
        }

        class Design
        {
            private IList<Point> rectPoints;

            public Design()
            {
                createPrimRect();
            }

            private void createPrimRect()
            {
                rectPoints = new List<Point>();
                rectPoints.Add(new Point(30, 5));
                rectPoints.Add(new Point(5, 15));
                rectPoints.Add(new Point(20, 40));
                rectPoints.Add(new Point(115, 32));
                rectPoints.Add(new Point(20, 20));
            }

            public void draw(Graphics graphics, Brush brush)
            {
                drawRect(graphics, brush);
            }

            private void drawRect(Graphics graphics, Brush brush)
            {
                //.NetFramework4.0以降
                //graphics.FillPolygon(brush, rectPoints.ToArray());

                //3.5以前
                var points = new Point[rectPoints.Count];
                for (var index = 0; index < rectPoints.Count; index++)
                    points[index] = rectPoints[index];
                graphics.FillPolygon(brush, points);
            }

        }

    }

}
