using MeasureThread;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace HOZProject
{
    public class MeasureMsgManage
    {
        public enum measureType
        {
            Photo=0,
            FIB=1,
            PT=2
        }

        #region 线程消息处理，显示内容
        /// <summary>
        /// 线程消息处理，显示内容
        /// </summary>
        /// <param name="formHOZ">主窗体对象</param>
        /// <param name="args">消息对象</param>
        public static void ShowMsgContent(FormHOZMain formHOZ, ThreadStatusEventArgs args)
        {
            #region 判断线程是否停止
            switch (args.Step_Code)
            {
                case "0-0":
                    //停止线程
                    if (formHOZ.m_BackgroundWorker.IsBusy)
                    {
                        formHOZ.m_BackgroundWorker.CancelAsync();
                        //是否关闭窗体的标识
                        if (formHOZ.IsClose)
                        {
                            formHOZ.Close();
                        }
                    }
                    break;
                case "0-1":
                    //初始化失败
                    if (formHOZ.m_BackgroundWorker.IsBusy)
                    {
                        //修改切孔状态
                        formHOZ.ChangeCutHoleState(args.HoleName, (int)ThreadState.Failed);
                    }
                    break;
            }
            #endregion
            //是否当前编号是流程中有数据的节点
            if (IsCutHolePhotoInfo(formHOZ, args))
            {
                if (!args.Picture_Information.Picture_FullPath.Equals(""))
                {
                    //显示状态信息
                    ShowStateMessage(formHOZ, args);
                    //设置图像
                    //FileStream fileStream = new FileStream(args.Picture_Information.Picture_FullPath, FileMode.Open, FileAccess.Read);
                    //formHOZ.pbImage.Image = Image.FromStream(fileStream);
                    //fileStream.Close();
                    //fileStream.Dispose();
                    formHOZ.pbImage.Image = GetFile(args.Picture_Information.Picture_FullPath);
                    //流程内容
                    double Work_Voltage = args.Picture_Information.Work_Voltage / 1000;
                    double Magnification = args.Picture_Information.Magnification;
                    double Work_Distance = args.Picture_Information.Work_Distance * 1000;
                    string semParaContent = "电压:" + Work_Voltage.ToString("f1") + "KV";
                    semParaContent += "  放大倍数：" + Magnification + "X";
                    semParaContent += "  工作距离:" + Work_Distance.ToString("f1") + "mm";
                    formHOZ.lblFlowContent.Text = semParaContent;
                }
            }
            else
            {
                formHOZ.lblFlowContent.Text = string.Empty ;
            }
            //修改切孔中流程状态
            ChageCutHoleFlowNodeState(formHOZ, args);
        }

        #region 将文件改换为内存流
        public static MemoryStream ReadFile(string path)
        {
            if (!File.Exists(path))
                return null;

            using (FileStream file = new FileStream(path, FileMode.Open))
            {
                byte[] b = new byte[file.Length];
                file.Read(b, 0, b.Length);
                file.Close();
                file.Dispose();
                MemoryStream stream = new MemoryStream(b);
                return stream;
            }
        }
        /// 
        /// 将内存流转为图片 
        /// 
        ///  
        ///  
        public static Image GetFile(string path)
        {
            MemoryStream stream = ReadFile(path);
            return stream == null ? null : Image.FromStream(stream);
        }
        #endregion

        /// <summary>
        /// 显示状态信息
        /// </summary>
        /// <param name="formHOZ"></param>
        /// <param name="args"></param>
        private static void ShowStateMessage(FormHOZMain formHOZ, ThreadStatusEventArgs args)
        {
            //显示状态信息
            if (args.Message == "自动对焦")
            {
                formHOZ.lblStateMessage.Text = "正在对焦中...请等待";
            }
            else if (args.Message == "自动像散")
            {
                formHOZ.lblStateMessage.Text = "正在消像散中...请等待";
            }
            else if (args.Message == "FIB自动对焦")
            {
                formHOZ.lblStateMessage.Text = "正在FIB自动对焦中...请等待";
            }
            else
            {
                formHOZ.lblStateMessage.Text = "";
            }
        } 
        #endregion

        #region 显示切孔流程中拍照信息
        /// <summary>
        /// 是否当前编号是流程中有数据的节点
        /// </summary>
        /// <param name="formHOZ">主窗体对象</param>
        /// <param name="args">线程参数</param>
        /// <returns></returns>
        public static bool IsCutHolePhotoInfo(FormHOZMain formHOZ, ThreadStatusEventArgs args)
        {
            if (formHOZ.plPrarInfo.Controls.Count > 0)
            {
                foreach (Control item in formHOZ.plPrarInfo.Controls)
                {
                    if (item is UserControl)
                    {
                        if (item.Name == args.HoleName)
                        {
                            UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
                            TimeLineItem[] ParaItem = uControl_ParaInfo.TlItem;
                            foreach (TimeLineItem tlItem in ParaItem)
                            {
                                if (tlItem.Code == args.Step_Code && tlItem.IsData)
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
        #endregion

        #region 修改切孔中流程状态
        /// <summary>
        /// 修改切孔中流程状态
        /// </summary>
        /// <param name="formHOZ">主窗体对象</param>
        /// <param name="cutHoleName">切孔名称</param>
        /// <param name="Code">流程编号</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public static bool ChageCutHoleFlowNodeState(FormHOZMain formHOZ, ThreadStatusEventArgs args)
        {
            if (formHOZ.plPrarInfo.Controls.Count > 0)
            {
                foreach (Control item in formHOZ.plPrarInfo.Controls)
                {
                    if (item is UserControl)
                    {
                        if (item.Name == args.HoleName)
                        {
                            UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
                            TimeLineItem[] ParaItem = uControl_ParaInfo.TlItem;
                            foreach (TimeLineItem tlItem in ParaItem)
                            {
                                if (tlItem.Code == args.Step_Code)
                                {
                                    tlItem.State = args.State?1:0;
                                    break;
                                }
                            }
                            //更新进度条
                            uControl_ParaInfo.UpdateCurrentMeasureSchedule();
                            //重新绘制
                            uControl_ParaInfo.TimeLineInvalidate();
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        #endregion
    }
}
