using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MeasureData;
using FileManager;
using System.Xml;

namespace HOZProject
{
    public partial class UControl_ParaInfo : UserControl
    {
        #region 成员变量
        /// <summary>
        /// 开始时间
        /// </summary>
        private string startTime;
        /// <summary>
        /// 结束时间
        /// </summary>
        private string endTime;
        /// <summary>
        /// 状态
        /// </summary>
        private int state;
        /// <summary>
        /// 位置
        /// </summary>
        private SemPosition position;
        private bool isSwitch;
        private string cutHoleName;
        private FormHOZMain formHOZMain;
        private TimeLineItem[] tlItem = null;
        private UCTimeLine uCTimeLine = null;
        public FormHOZMain FormHOZMainObject { get => formHOZMain; set => formHOZMain = value; }
        public string StartTime { get => startTime; set => startTime = value; }
        public string EndTime { get => endTime; set => endTime = value; }
        public int State { get => state; set => state = value; }
        public SemPosition Position { get => position; set => position = value; }
        public string CutHoleName { get => cutHoleName; set => cutHoleName = value; }
        public bool IsSwitch { get => isSwitch; set => isSwitch = value; }
        public TimeLineItem[] TlItem { get => tlItem; set => tlItem = value; } 
        #endregion

        public UControl_ParaInfo(FormHOZMain formHOZ)
        {
            InitializeComponent();
            FormHOZMainObject = formHOZ;
            //显示测量流程
            ShowMeasureFlow();
        }

        #region 显示开始与结束时间
        public void ShowTime()
        {
            if (StartTime == "0001/1/1 0:00:00")
            {
                lblShowStartTime.Text = "";
            }
            else
            {
                lblShowStartTime.Text = StartTime;
            }
            if (EndTime == "0001/1/1 0:00:00")
            {
                lblShowEndTime.Text = "";
            }
            else
            {
                lblShowEndTime.Text = EndTime;
            }
        }
        #endregion

        #region 当前进度
        public void UpdateCurrentMeasureSchedule()
        {
            if (TlItem != null)
            {
                //完成状态数量
                int finishCount = 0;
                //未完成状态数量
                int unFinishedCount = 0;
                foreach (TimeLineItem item in TlItem)
                {
                    if (item.State>0)
                    {
                        finishCount++;
                    }
                    switch (FormHOZMainObject.m_MeasureType)
                    {
                        case (int)MeasureMsgManage.measureType.Photo:
                            if (item.Type.ToUpper() == "PT" || item.Type.ToUpper() == "FIB")
                            {
                                continue;
                            }
                            else
                            {
                                unFinishedCount++;
                            }
                            break;
                        case (int)MeasureMsgManage.measureType.FIB:
                            if (item.Type.ToUpper() == "PT")
                            {
                                continue;
                            }
                            else
                            {
                                unFinishedCount++;
                            }
                            break;
                        case (int)MeasureMsgManage.measureType.PT:
                            unFinishedCount = TlItem.Length;
                            break;
                    }
                }
                try
                {
                    if (lblShowState.Text == "失败")
                    {
                        pbMeasure.Value = 100;
                        lblCompletedAmount.Text = "100%";
                    }
                    else
                    {
                        int pbValue = (int)((float)finishCount / (float)unFinishedCount * 100);
                        pbMeasure.Value = pbValue;
                        lblCompletedAmount.Text = pbValue + "%";
                    }

                }
                //try
                //{
                //    int pbValue = (int)((float)finishCount / (float)unFinishedCount*100);
                //    pbMeasure.Value = pbValue;
                //    lblCompletedAmount.Text = pbValue + "%";
                //}
                catch (Exception)
                {

                    pbMeasure.Value = 0;
                }
            }
        }
        #endregion

        #region 隐藏属性层
        private void btnClose_Click(object sender, EventArgs e)
        {
            FormHOZMainObject.plPrarInfo.Visible = false;
        }
        #endregion

        #region 重绘时间轴
        /// <summary>
        /// 重绘时间轴
        /// </summary>
        public void TimeLineInvalidate()
        {
            plTimeLine.Invalidate();
            uCTimeLine.Invalidate();
        } 
        #endregion

        #region 显示切孔参数信息
        /// <summary>
        /// 显示切孔参数信息
        /// </summary>
        public void ShowParaInfo()
        {
            int multiple = 1000;
            //设置Position参数
            lblX.Text = (Position.X * multiple).ToString("f2");
            lblY.Text = (Position.Y * multiple).ToString("f2");
            lblZ.Text = (Position.Z * multiple).ToString("f2");
            lblR.Text = Position.R.ToString();
            lblT.Text = Position.T.ToString();
            lblM.Text = (Position.M * multiple).ToString();
            try
            {
                DateTime dtst = Convert.ToDateTime(StartTime);
                if (dtst.Year != System.DateTime.Now.Year)
                {
                    lblShowStartTime.Text = "";
                }
                else
                {
                    lblShowStartTime.Text = StartTime;
                }
            }
            catch
            {
                lblShowStartTime.Text = "";
            }
            try
            {
                DateTime dtet = Convert.ToDateTime(EndTime);
                if (dtet.Year != System.DateTime.Now.Year)
                {
                    lblShowEndTime.Text = "";
                }
                else
                {
                    lblShowEndTime.Text = EndTime;
                }
            }
            catch
            {
                lblShowEndTime.Text = "";
            }
            
            //设置切孔状态
            switch (State)
            {
                //准备
                case (int)MeasureThread.ThreadState.Ready:
                    //修改切孔状态
                    lblShowState.Text = "准备";
                    break;
                //等待
                case (int)MeasureThread.ThreadState.Waiting:
                    //修改切孔状态
                    lblShowState.Text = "等待";
                    break;
                //进行中
                case (int)MeasureThread.ThreadState.InProcess:
                    //修改切孔状态
                    lblShowState.Text = "进行中";
                    break;
                //完成
                case (int)MeasureThread.ThreadState.Success:
                    //修改切孔状态
                    lblShowState.Text = "完成";
                    break;
                //失败
                case (int)MeasureThread.ThreadState.Failed:
                    lblShowState.Text = "失败";
                    break;
            }
            lblCutHoleName.Text = CutHoleName;
            CkIsSwitch.Checked = IsSwitch;
        } 
        #endregion

        #region 绑定流程信息
        /// <summary>
        /// 绑定流程信息
        /// </summary>
        /// <param name="flowCode"></param>
        private void ShowMeasureFlow()
        {
            if (TlItem == null)
            {
                TlItem = GetMeasureFlowStructInfo();
            }
            if (TlItem != null)
            {
                ShowUCTimeLine(TlItem);
            }
        }
        public void ShowUCTimeLine(TimeLineItem[] tlItem)
        {
            if (plTimeLine.Controls.Count == 0)
            {
                uCTimeLine = new UCTimeLine(tlItem, FormHOZMainObject.m_MeasureType);
                uCTimeLine.Dock = DockStyle.Fill;
                plTimeLine.Controls.Add(uCTimeLine);
            }
        }
        private TimeLineItem[] GetMeasureFlowStructInfo()
        {
            string xmlfullname = Application.StartupPath + @"\MeasureXML\MeasureStructXml.xml";
            XmlNodeList nodeList = XmlManager.GetXmlMeasureFlowNodeInfo(xmlfullname);
            if (nodeList != null)
            {
                return XmlConvertTimeListItem(nodeList);
            }
            return null;
        }
        private TimeLineItem[] XmlConvertTimeListItem(XmlNodeList nodeList)
        {
            List<TimeLineItem> timeLineList = new List<TimeLineItem>();
            for (int i = 0; i < nodeList.Count; i++)
            {
                if (Convert.ToBoolean(nodeList[i].Attributes["IsShow"].Value))
                {
                    TimeLineItem tlItem = new TimeLineItem();
                    tlItem.Details = nodeList[i].Attributes["Details"].Value;
                    tlItem.Code = nodeList[i].Attributes["Code"].Value;
                    tlItem.State = -1;
                    tlItem.Title = nodeList[i].Attributes["Title"].Value;
                    tlItem.IsData = Convert.ToBoolean(nodeList[i].Attributes["IsData"].Value);
                    tlItem.Type = nodeList[i].Attributes["Type"].Value;
                    tlItem.IsShow = Convert.ToBoolean(nodeList[i].Attributes["IsShow"].Value);
                    tlItem.Index = Convert.ToInt32(nodeList[i].Attributes["Index"].Value);
                    timeLineList.Add(tlItem);
                }
            }
            TimeLineItem[] timeLineItem = new TimeLineItem[timeLineList.Count];
            //按照Index进行升序
            timeLineItem = timeLineList.OrderBy(i => i.Index).ToArray();
            //清空时间轴列表
            timeLineList.Clear();
            return timeLineItem;
        } 
        #endregion

        #region 关闭按钮 鼠标操作事件
        private void pbClose_MouseEnter(object sender, EventArgs e)
        {
            pbClose.BackgroundImage = global::HOZProject.Properties.Resources.Exit;
        }

        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            pbClose.BackgroundImage = global::HOZProject.Properties.Resources.Exit;
        }
        #endregion

        #region 设置切孔是否检测
        private void CkIsSwitch_CheckedChanged(object sender, EventArgs e)
        {
            //设置切孔是否检测
            List<CutHole> cutHoles = formHOZMain.m_MeasureFile.ListCutHole;
            foreach (CutHole item in cutHoles)
            {
                if (item.HoleName == CutHoleName)
                {
                    item.SWITCH = CkIsSwitch.Checked;
                    //是否已保存
                    if (formHOZMain.IsSave)
                    {
                        //保存测量文件
                        formHOZMain.m_MeasureFile.Save();
                    }
                    break;
                }
            }
        } 
        #endregion
    }
}
