using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace MeasureThread
{
    class PTWork
    {
        NLog.Logger log;
        SmartSEMControl.ISEMControl iSEM;
        string PTTemp;
        private string m_ProgramFolder = Directory.GetCurrentDirectory();
        const String MacoInsertPt = "GIS Insert.MLF"; //传入PT针
        const String MacoRetractPt = "GIS Retract.MLF"; //退出PT针
        public PTWork(SmartSEMControl.ISEMControl aSEM, string a_PTTemp)
        {
            log = NLog.LogManager.GetCurrentClassLogger();
            iSEM = aSEM;
            PTTemp = a_PTTemp;
        }
        public bool DoWholePTWork()
        {
            if (!iSEM.CmdFIBModeFIB())
            {
                log.Error("测量线程报错：切换到SEM模式失败", false);
                return false;
            }
            Thread.Sleep(200);
            //12.根据样品类型参数确定是否需要PT沉积，控制PT针插入
          
                log.Info("测量线程：插入PT针", true);
                if (!InsertPT())
                {

                    log.Error("测量线程报错：插入PT针失败", false);
                    return false;
                }

            Thread.Sleep(200);


            //3. 根据坐标进行PT沉积   
            log.Info("测量线程： 进行PT沉积");
            if (!DoPTWork())
            {

                log.Error("测量线程报错： PT沉积失败");
                return false;
            }
            

            Thread.Sleep(200);
            log.Info ("测量线程：撤出PT针", false);
            if (!RetractPT())
                {

                    log.Error("测量线程报错：撤出PT针失败", false);
                    return false;
                }

            Thread.Sleep(200);


            if (!iSEM.CmdFIBModeSEM())
            {

                log.Error("测量线程报错：切换到SEM模式失败", false);
                return false;
            }
            Thread.Sleep(200);
            return true;

        }
        public bool  ModifyPTTemp(float x1,float y1,float x2,float y2)
        {
          var  px = iSEM.GetPixelSize();
            float x0 = 0, y0 = 0;
            float xc = (x1 + x2) / 2;
            float yc = (y1 + y2) / 2;
            x0 = xc - 512;
            y0 = 384 - yc;
            var  xmlDoc = new XmlDocument();
            xmlDoc.Load(PTTemp);//加载baixml文件，xmlpath 为XML文件的路径du
           var xns = xmlDoc.SelectSingleNode("ELAYOUT/STRUCTURE_LIST/STRUCTURE/LAYER_REFERENCE/RECT");
            if (xns != null)
            {
                XmlAttributeCollection attributeCol = xns.Attributes;
                double width = Convert.ToDouble(attributeCol.GetNamedItem("width").Value) / 2.0;
                //因为ELY文件里的矩形高就是负值，所以在下面的Y值加上这个值就好。
                double height = Convert.ToDouble(attributeCol.GetNamedItem("height").Value) / 1.5;
                //遍历自己点属性
                foreach (XmlAttribute attri in attributeCol)
                {
                    if (attri.Name == "x")
                    {
                        attri.InnerText = ((x0 * px) * 1000000 - width).ToString();
                    }
                    else if (attri.Name == "y")
                    {
                        attri.InnerText = ((y0 * px) * 1000000 - 2.791 - height).ToString();
                    }
                }
            }
            xmlDoc.Save(PTTemp);
            return true;

        }
        //插入PT针
        public bool InsertPT()
        {
            string fn = m_ProgramFolder + "\\Macro\\" + MacoInsertPt;
           
            iSEM.CMDMCFFilename(fn);
           
            Thread.Sleep(1000);
            return true;
        }

        //撤出PT针
        public bool RetractPT()
        {
            string fn = m_ProgramFolder + "\\Macro\\" + MacoRetractPt;
            //SendMsg("调用宏撤出PT针宏文件" + fn);
            iSEM.CMDMCFFilename(fn);
            //延时1s？？
            Thread.Sleep(1000);
            return true;
        }

        //PT沉积
        public bool DoPTWork()
        {
            //执行PT沉积的ELY文件
            if (!ExcuteEly(PTTemp))
            {
                return false;
            }

            //等待沉积完成
            while (true)
            {
                Thread.Sleep(7000);
                if (iSEM.GetFIBMode() == 0)
                {
                    break;
                }
            }
            return true;
        }
        //执行ELY文件的步骤
        public bool ExcuteEly(string a_filename)
        {
            //执行ELy文件有三个动作
            //1. 选择ELY文件
            //SendMsg("选择ELY文件");
            if (!iSEM.CmdFIBLoadELY(a_filename))
            {
                //SendMsg("选择ELY文件失败");
                return false;
            }
            Thread.Sleep(1000);
            //2. 确认ELY文件
            //SendMsg("确认ELY文件");
            if (!iSEM.CmdFIBEXPOSUREELY())
            {
                //SendMsg("确认ELY文件失败");
                return false;
            }
            Thread.Sleep(1000);
            //3. 执行ELY文件
            //SendMsg("执行ELY文件");
            if (!iSEM.CmdFIBSTARTELY())
            {
                //SendMsg("执行ELY文件失败");
                return false;
            }
            Thread.Sleep(1000);

            return true;
        }
    }
}
