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

namespace monitor_main_windows
{
    public partial class StepControl : UserControl
    {

        [Description("步骤更改事件"), Category("自定义")]
        public event EventHandler IndexChecked;

        private Color m_stepCompleteColor = Color.Green;
        private Color m_stepRunningColor = Color.Yellow;
        private Color m_stepWaitColor = Color.FromArgb(100, 100, 100);
        private Color m_stepErrorColor = Color.Red;

        private Timer m_timer = new Timer();
        /// <summary>
        /// 步骤背景色
        /// </summary>
        [Description("步骤等待色"), Category("自定义")]
        public Color StepWaitColor
        {
            get { return m_stepWaitColor; }
            set { m_stepWaitColor = value; }
        }
        [Description("步骤运行色"), Category("自定义")]
        public Color StepRunningColor
        {
            get { return m_stepRunningColor; }
            set { m_stepRunningColor = value; }
        }
        [Description("步骤完成色"), Category("自定义")]
        public Color StepCompleteColor
        {
            get { return m_stepCompleteColor; }
            set { m_stepCompleteColor = value; }
        }
        [Description("步骤故障色"), Category("自定义")]
        public Color StepErrorColor
        {
            get { return m_stepErrorColor; }
            set { m_stepErrorColor = value; }
        }

        private Color m_stepFontColor = Color.White;
        /// <summary>
        /// 步骤文字颜色
        /// </summary>
        [Description("步骤文字景色"), Category("自定义")]
        public Color StepFontColor
        {
            get { return m_stepFontColor; }
            set { m_stepFontColor = value; }
        }


        private string[] m_steps =  new string[] { /*"停车开始", "预定车位","搬运","确认车位","完成"*/ };
        private string[] m_back_steps = new string[] { };

         [Description("步骤"), Category("自定义")]
         public string[] Steps
         {
             get { return m_steps; }
             set
             {
                 if (m_steps == null )
                     return;
                 m_steps = value;
                 Refresh();
             }
         }

        [Description("回退步骤"), Category("自定义")]
        public string[] BackSteps
        {
            get { return m_back_steps; }
            set
            {
                if (m_back_steps == null)
                    return;
                m_back_steps = value;
                Refresh();
            }
        }

        private int m_stepIndex = 1;

        private string m_label = "车牌：鄂A00000";
        [Description("标题文字"), Category("自定义")]
        public string Label
        {
            get { return m_label; }
            set { m_label = value; }
        }


        [Description("步骤位置"), Category("自定义")]
        public int StepIndex
        {
            get { return m_stepIndex; }
            set
            {
                if (m_stepIndex > m_steps.Length+ m_back_steps.Length)
                    return;
                m_stepIndex = value;
                Refresh();
                if (IndexChecked != null)
                {
                    IndexChecked(this, null);
                }
            }
        }

        public StepControl()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.BorderStyle = BorderStyle.FixedSingle;
            m_timer.Enabled = true;
            m_timer.Tick += Timer_Tick;
            m_timer.Interval = 500;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            m_stepRunningColor = m_stepRunningColor==Color.Green?Color.Yellow:Color.Green;
            Refresh();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;

            if (BackSteps.Length == 0)
                g.Clear(Color.PaleGreen);
            else
                g.Clear(Color.LightPink);
            g.SmoothingMode = SmoothingMode.HighQuality;  //图片柔顺模式
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
            g.CompositingQuality = CompositingQuality.HighQuality;//

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            //g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高，即消除锯齿


            int vertical_line = BackSteps.Length>0?20:0;
            int step_w = 25;
            System.Drawing.SizeF sizeFirst = g.MeasureString(m_steps[0], this.Font);
            if (BackSteps.Length > 0)
            {
                System.Drawing.SizeF sizeBacKEnd = g.MeasureString(BackSteps[BackSteps.Length-1], this.Font);
                if (sizeBacKEnd.Width > sizeFirst.Width)
                    sizeFirst = sizeBacKEnd;
            }

            System.Drawing.SizeF sizeLabel = g.MeasureString(Label, this.Font);
            int label_x = (Width - (int)sizeLabel.Width) / 2;
            label_x = label_x >= 0 ? label_x : 0;
            g.DrawString(Label, Font, new SolidBrush(m_stepCompleteColor), new Point(label_x, 3));


            int y =  5 + (int)sizeFirst.Height;
            if (y < 0)
                y = 0;

            int intTxtY = y + step_w + 3;
            int intLeft = 3;
            if (sizeFirst.Width > step_w)
            {
                intLeft = (int)(sizeFirst.Width - step_w) / 2 + 1;
            }

            int intRight = 3;

            System.Drawing.SizeF sizeEnd = g.MeasureString(m_steps[m_steps.Length - 1], this.Font);
            if(BackSteps.Length>0)
            {
                System.Drawing.SizeF sizeBacKFirst = g.MeasureString(BackSteps[0], this.Font);
                if (sizeBacKFirst.Width > sizeEnd.Width)
                    sizeEnd = sizeBacKFirst;
            }

            if (sizeEnd.Width > step_w)
            {
                intRight = (int)(sizeEnd.Width - step_w) / 2 + 1;
            }

            int intSplitWidth = 0;
            intSplitWidth = (this.Width-intLeft - (m_steps.Length * step_w) - intRight) / (m_steps.Length - 1);
            if (intSplitWidth < 10)
                intSplitWidth = 10;

            for (int i = 0; i < m_steps.Length; i++)
            {
                #region 画圆，横线
                
                g.FillEllipse(new SolidBrush(m_stepWaitColor), new Rectangle(new Point(intLeft + i * (step_w + intSplitWidth), y), new Size(step_w, step_w)));
                
                if (m_stepIndex > i)
                {
                    //存在回退步骤时， 最后一个表示错误
                    if (m_back_steps.Length > 0 && i == m_steps.Length - 1)
                    {
                        g.FillEllipse(new SolidBrush(m_stepErrorColor),
                            new Rectangle(new Point(intLeft + i * (step_w + intSplitWidth) + 2, y + 2),
                            new Size(step_w - 4, step_w - 4)));
                    }
                    else
                    {
                        g.FillEllipse(new SolidBrush(m_stepCompleteColor),
                            new Rectangle(new Point(intLeft + i * (step_w + intSplitWidth) + 2, y + 2),
                            new Size(step_w - 4, step_w - 4)));
                    }
                    if (i != m_steps.Length - 1)
                    {

                        if (m_stepIndex == i + 1)
                        {
                            //当前步骤运行中闪烁，完成绿色

                            g.FillEllipse(new SolidBrush(m_stepRunningColor),
                                  new Rectangle(new Point(intLeft + i * (step_w + intSplitWidth) + 2, y + 2),
                                   new Size(step_w - 4, step_w - 4)));
                            g.DrawLine(new Pen(m_stepWaitColor, 2), new Point(intLeft + i * (step_w + intSplitWidth) + step_w, y + (step_w / 2)), 
                                new Point(intLeft + (i+1) * (step_w + intSplitWidth) , y + (step_w / 2)));
                        }
                        else
                        {
                            g.DrawLine(new Pen(m_stepCompleteColor, 2), new Point(intLeft + i * (step_w + intSplitWidth) + step_w, y + (step_w / 2)),
                                new Point(intLeft + (i+1) * (step_w + intSplitWidth) , y + (step_w / 2)));
                        }
                    }
                   
                }
                else
                {
                    if (i != m_steps.Length - 1)
                    {
                        g.DrawLine(new Pen(m_stepWaitColor, 2), new Point(intLeft + i * (step_w + intSplitWidth) + step_w, y + (step_w / 2)), 
                            new Point(intLeft + (i+1) * (step_w + intSplitWidth) , y + (step_w / 2)));
                    }
                }

                System.Drawing.SizeF _numSize = g.MeasureString((i + 1).ToString(), this.Font);
                g.DrawString((i + 1).ToString(), Font, new SolidBrush(m_stepFontColor), 
                    new Point(intLeft + i * (step_w + intSplitWidth) + (step_w - (int)_numSize.Width) / 2 , y + (step_w - (int)_numSize.Height) / 2 + 1));
                #endregion

                System.Drawing.SizeF sizeTxt = g.MeasureString(m_steps[i], this.Font);
                g.DrawString(m_steps[i], Font, new SolidBrush(m_stepIndex > i ? m_stepCompleteColor : m_stepWaitColor), 
                    new Point(intLeft + i * (step_w + intSplitWidth) + (step_w - (int)sizeTxt.Width) / 2 , intTxtY));


            }
            //绘制回退
            if (m_back_steps.Length > 0)
            {
                int winHeight = step_w * 2 + vertical_line + 20 + 3 * (int)sizeFirst.Height;
                if (Height < winHeight)
                    Height = winHeight;
                
                int by = intTxtY + (int)sizeFirst.Height + vertical_line;
                int brx = intLeft + (m_steps.Length - 1) * (step_w + intSplitWidth);

                int back_step_index = m_stepIndex - m_steps.Length;
                back_step_index = back_step_index >= 0 ? back_step_index : 0;
                if (back_step_index > 0)
                {
                    g.DrawLine(new Pen(m_stepCompleteColor, 2), new Point(brx + step_w / 2, by - vertical_line),
                        new Point(brx + step_w / 2, by));
                }
                else
                {
                    g.DrawLine(new Pen(m_stepWaitColor, 2), new Point(brx + step_w / 2, by - vertical_line),
                       new Point(brx + step_w / 2, by));
                }

               
                
                for (int i = 0; i < m_back_steps.Length; i++)
                {
                    g.FillEllipse(new SolidBrush(m_stepWaitColor),
                         new Rectangle(new Point(brx - i * (step_w + intSplitWidth), by),
                         new Size(step_w, step_w)));
                    if (back_step_index > i)
                    {
                        if (back_step_index == i + 1)
                        {
                            //当前步骤运行中闪烁，完成绿色
                            g.FillEllipse(new SolidBrush(m_stepRunningColor),
                             new Rectangle(new Point(brx - i * (step_w + intSplitWidth) + 2, by + 2),
                             new Size(step_w - 4, step_w - 4)));

                        }
                        else
                        {
                            g.FillEllipse(new SolidBrush(m_stepCompleteColor),
                            new Rectangle(new Point(brx - i * (step_w + intSplitWidth) + 2, by + 2),
                            new Size(step_w - 4, step_w - 4)));
                        }

                        if (i != m_back_steps.Length - 1)
                        {
                            if (back_step_index == i + 1)
                            {
                                g.DrawLine(new Pen(m_stepWaitColor, 2),
                                    new Point(brx - i * (step_w + intSplitWidth), by + (step_w / 2)),
                                    new Point(brx - (i + 1) * (step_w + intSplitWidth), by + (step_w / 2)));
                            }
                            else
                            {
                                g.DrawLine(new Pen(m_stepCompleteColor, 2),
                                    new Point(brx - i * (step_w + intSplitWidth), by + (step_w / 2)),
                                    new Point(brx - (i + 1) * (step_w + intSplitWidth), by + (step_w / 2)));
                            }
                        }
                    }
                    else
                    {
                        if (i != m_back_steps.Length - 1)
                        {
                            g.DrawLine(new Pen(m_stepWaitColor, 2), new Point(brx - i * (step_w + intSplitWidth), by + (step_w / 2)),
                                new Point(brx - (i + 1) * (step_w + intSplitWidth), by + (step_w / 2)));
                        }
                    }

                    System.Drawing.SizeF _numSize = g.MeasureString((i + 1).ToString(), this.Font);
                    g.DrawString((m_steps.Length + i + 1).ToString(), Font, new SolidBrush(m_stepFontColor),
                        new Point(brx - i * (step_w + intSplitWidth) + step_w / 2 - (int)_numSize.Width / 2, by + (step_w - (int)_numSize.Height) / 2 + 1));


                    System.Drawing.SizeF sizeTxt = g.MeasureString(m_back_steps[i], this.Font);
                    g.DrawString(m_back_steps[i], Font, new SolidBrush(back_step_index > i ? m_stepCompleteColor : m_stepWaitColor),
                        new Point(brx - i * (step_w + intSplitWidth) + (step_w - (int)sizeTxt.Width) / 2 + 1, by + step_w + (int)sizeTxt.Height / 2 + 1));


                }
            }


        }

    }
}
