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

namespace HOZProject
{
    
    /// <summary>
    /// Class UCTimeLine.
    /// Implements the <see cref="System.Windows.Forms.UserControl" />
    /// </summary>
    /// <seealso cref="System.Windows.Forms.UserControl" />
    public partial class UCTimeLine : UserControl
    {
        /// <summary>
        /// Timed flashing
        /// </summary>
        int count = 0;
        int min = 0;
        /// <summary>
        /// The line color
        /// </summary>
        private Color lineColor = TextColors.Light;

        /// <summary>
        /// Gets or sets the color of the line.
        /// </summary>
        /// <value>The color of the line.</value>
        [Description("连接线颜色"), Category("自定义")]
        public Color LineColor
        {
            get { return lineColor; }
            set
            {
                lineColor = value;
                Invalidate();
            }
        }
        /// <summary>
        /// The title font
        /// </summary>
        private Font titleFont = new Font("微软雅黑", 11f);

        /// <summary>
        /// Gets or sets the title font.
        /// </summary>
        /// <value>The title font.</value>
        [Description("标题字体"), Category("自定义")]
        public Font TitleFont
        {
            get { return titleFont; }
            set
            {
                titleFont = value;
            }
        }

        /// <summary>
        /// The title forcolor
        /// </summary>
        private Color titleForcolor = TextColors.Light;

        /// <summary>
        /// Gets or sets the title forcolor.
        /// </summary>
        /// <value>The title forcolor.</value>
        [Description("标题颜色"), Category("自定义")]
        public Color TitleForcolor
        {
            get { return titleForcolor; }
            set
            {
                titleForcolor = value;
            }
        }

        /// <summary>
        /// The details font
        /// </summary>
        private Font detailsFont = new Font("微软雅黑", 12);

        /// <summary>
        /// Gets or sets the details font.
        /// </summary>
        /// <value>The details font.</value>
        [Description("详情字体"), Category("自定义")]
        public Font DetailsFont
        {
            get { return detailsFont; }
            set
            {
                detailsFont = value;
            }
        }

        /// <summary>
        /// The details forcolor
        /// </summary>
        private Color detailsForcolor = TextColors.MoreDark;

        /// <summary>
        /// Gets or sets the details forcolor.
        /// </summary>
        /// <value>The details forcolor.</value>
        [Description("详情颜色"), Category("自定义")]
        public Color DetailsForcolor
        {
            get { return detailsForcolor; }
            set
            {
                detailsForcolor = value;
            }
        }

        /// <summary>
        /// The items
        /// </summary>
        public TimeLineItem[] items;

        /// <summary>
        /// Gets or sets the items.
        /// </summary>
        /// <value>The items.</value>
        [Description("项列表"), Category("自定义")]
        public TimeLineItem[] Items
        {
            get { return items; }
            set
            {
                items = value;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UCTimeLine"/> class.
        /// </summary>
        public UCTimeLine(TimeLineItem[] tlItem,int measureType)
        {
            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);
            InitializeComponent();
            items = tlItem;
            //加载节点信息
            ReloadItems(measureType);

        }

        /// <summary>
        /// Reloads the items.
        /// </summary>
        public void ReloadItems(int measureType)
        {
            try
            {
                ControlHelper.FreezeControl(this, true);
                this.Controls.Clear();
                if (items != null)
                {
                    foreach (var item in items)
                    {
                        switch (measureType)
                        {
                            case (int)MeasureMsgManage.measureType.Photo:
                                if (item.Type.ToUpper() == "PT" || item.Type.ToUpper() == "FIB")
                                {
                                    continue;
                                }
                                break;
                            case (int)MeasureMsgManage.measureType.FIB:
                                if (item.Type.ToUpper() == "PT")
                                {
                                    continue;
                                }
                                break;
                            //case (int)MeasureMsgManage.measureType.PT:

                            //    break;
                        }
                        FlowLayoutPanel panelTitle = new FlowLayoutPanel();
                        panelTitle.Dock = DockStyle.Top;
                        panelTitle.AutoScroll = false;
                        panelTitle.Padding = new System.Windows.Forms.Padding(5);
                        panelTitle.Name = "Code_" + item.Code;//+ Guid.NewGuid().ToString();
                        panelTitle.Tag = item.Code;
                        //panelTitle.BackColor = Color.Yellow;
                        Label lblTitle = new Label();
                        lblTitle.Dock = DockStyle.Top;
                        lblTitle.AutoSize = true;
                        lblTitle.Font = titleFont;
                        lblTitle.ForeColor = titleForcolor;
                        lblTitle.Text = item.Title;
                        lblTitle.Tag = item.Code;
                        lblTitle.Name = "lbl"+item.Code;
                        lblTitle.SizeChanged += item_SizeChanged;
                        panelTitle.Controls.Add(lblTitle);
                        this.Controls.Add(panelTitle);
                        panelTitle.BringToFront();


                        FlowLayoutPanel panelDetails = new FlowLayoutPanel();
                        panelDetails.Dock = DockStyle.Top;
                        panelDetails.AutoScroll = false;
                        panelDetails.Padding = new System.Windows.Forms.Padding(5);
                        panelDetails.Name = "details_" + item.Code; //"details_" + Guid.NewGuid().ToString();
                        panelDetails.BackColor = Color.White;
                        Label lblDetails = new Label();
                        lblDetails.AutoSize = true;
                        lblDetails.Dock = DockStyle.Top;
                        lblDetails.Font = detailsFont;
                        lblDetails.ForeColor = titleForcolor;
                        lblDetails.Text = item.Details;
                        lblDetails.SizeChanged += item_SizeChanged;
                        panelDetails.Controls.Add(lblDetails);
                        this.Controls.Add(panelDetails);
                        panelDetails.BringToFront();
                    }
                }
            }
            finally
            {
                ControlHelper.FreezeControl(this, false);
            }
        }

        /// <summary>
        /// Handles the SizeChanged event of the item control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void item_SizeChanged(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            lbl.Parent.Height = lbl.Height + 10;
        }

        /// <summary>
        /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            bool twinkle = true;
            if (count <= 2)
            {
                count = count + 1;
            }
            else
            {
                count = 0;
            }
            base.OnPaint(e);
            var g = e.Graphics;
            g.SetGDIHigh();
            var lst = this.Controls.ToArray().Where(p => p.Name.StartsWith("Code_")).ToList();
            var lst1 = this.Controls.ToArray().Where(p => p.Name.StartsWith("details_")).ToList();
            for (int i =  0; i<lst.Count; i++)
            {
                Label label = (Label)lst[i].Controls[0];
                Label labeText = (Label)lst1[i].Controls[0];
                foreach (var item in items)
                {
                    if (item.State!=1)
                    {
                        twinkle = true;
                    }
                    else
                    {
                        twinkle = false;
                    }
                }
                foreach (var item in items)
                {
                    //if (item.State==1)
                    if(label.Tag.ToString() == item.Code)
                    {
                        #region State Int 判断方法
                        switch (item.State)
                        {
                            //出错
                            case 0:
                                lineColor = Color.Red;
                                //空心
                                g.DrawEllipse(new Pen(new SolidBrush(Color.Red)), new Rectangle(7, lst[i].Location.Y + 10, 16, 16));
                                //画实心圆
                                g.FillEllipse(new SolidBrush(Color.Red), new Rectangle(7 + 5, lst[i].Location.Y + 16, 5, 5));
                                label.ForeColor = Color.Red;
                                labeText.ForeColor = Color.Red;
                                FormHOZMain.ControlFlicker = false;
                                break;
                            //完成
                            case 1:
                                lineColor = Color.Lime;
                                //画实心圆
                                g.FillEllipse(new SolidBrush(Color.Lime), new Rectangle(7 + 5, lst[i].Location.Y + 16, 5, 5));
                                //空心
                                g.DrawEllipse(new Pen(new SolidBrush(Color.Lime)), new Rectangle(7, lst[i].Location.Y + 10, 16, 16));
                                label.ForeColor = TextColors.Light;
                                labeText.ForeColor = TextColors.Light;
                                break;
                            default:
                                lineColor = TextColors.Light;
                                if (min<=i)
                                {
                                    min = i;
                                }
                                g.DrawEllipse(new Pen(new SolidBrush(lineColor)), new Rectangle(7, lst[i].Location.Y + 10, 16, 16));
                                g.FillEllipse(new SolidBrush(lineColor), new Rectangle(7+5, lst[i].Location.Y + 16, 5, 5));
                               
                                break;
                        }
                        #endregion
                        break;
                    }
                }
                //划线
                if (i != lst.Count - 1)
                {
                    g.DrawLine(new Pen(new SolidBrush(lineColor)), new Point(7 + 8, lst[i].Location.Y + 10 - 2), new Point(7 + 8, lst[i + 1].Location.Y + 10 + 16 + 2));
                }
            }
            if (FormHOZMain.ControlFlicker)
            {
                for (int i = 0; i < lst.Count; i++)
                {
                    if(twinkle)
                    {
                        if (min >= 0)
                        {
                            if (count == 1)
                            {
                                //画空心圆
                                g.DrawEllipse(new Pen(new SolidBrush(Color.DarkOrange)), new Rectangle(7, lst[min].Location.Y + 10, 16, 16));
                                //画实心圆
                                g.FillEllipse(new SolidBrush(Color.DarkOrange), new Rectangle(7 + 5, lst[min].Location.Y + 16, 5, 5));
                                Label label = (Label)lst[min].Controls[0];
                                Label labeText = (Label)lst1[min].Controls[0];
                                label.ForeColor = Color.DarkOrange;
                                labeText.ForeColor = Color.DarkOrange;
                            }
                            else
                            if (count == 2)
                            {
                                //画空心圆
                                g.DrawEllipse(new Pen(new SolidBrush(lineColor)), new Rectangle(7, lst[min].Location.Y + 10, 16, 16));
                                //画实心圆
                                g.FillEllipse(new SolidBrush(lineColor), new Rectangle(7 + 5, lst[min].Location.Y + 16, 5, 5));
                                Label label = (Label)lst[min].Controls[0];
                                Label labeText = (Label)lst1[min].Controls[0];
                                label.ForeColor = TextColors.Light;
                                labeText.ForeColor = TextColors.Light;
                            }
                        }
                    }
                    if (i == lst.Count - 1)
                    {
                        min = 0;
                    }
                }

            }
        }

       

        private void UCTimeLine_Paint(object sender, PaintEventArgs e)
        {

        }
    }

    /// <summary>
    /// Class TimeLineItem.
    /// </summary>
    public class TimeLineItem
    {
        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title { get; set; }
        /// <summary>
        /// Gets or sets the details.
        /// </summary>
        /// <value>The details.</value>
        public string Details { get; set; }
        /// <summary>
        /// Gets or sets the Code.
        /// </summary>
        /// <value>The Code.</value>
        public string Code { get; set; }
        /// <summary>
        /// Gets or sets the State.
        /// </summary>
        /// <value>The State.</value>
        public int State { get; set; }
        /// <summary>
        /// Gets or sets the IsData.
        /// </summary>
        /// <value>The IsData.</value>
        public bool IsData { get; set; }
        /// <summary>
        /// Gets or sets the Type.
        /// </summary>
        /// <value>The Type.</value>
        public string Type { get; set; }
        /// <summary>
        /// Gets or sets the Show.
        /// </summary>
        /// <value>The Show.</value>
        public bool IsShow { get; set; }
        /// <summary>
        /// Gets or sets the Index.
        /// </summary>
        /// <value>The Index.</value>
        public int Index { get; set; }
    }
}
