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

namespace MeasureThread
{
    class FibWork
    {
        NLog.Logger log;
        SmartSEMControl.ISEMControl iSEM;
        string FIBTemp;
        public FibWork(SmartSEMControl.ISEMControl aSEM,string a_FibTemp)
        {
            log = NLog.LogManager.GetCurrentClassLogger();
            iSEM = aSEM;
            FIBTemp = a_FibTemp;
        }
        //FIB切割
        public bool DoFIBWork()
        {
            //执行PT沉积的ELY文件
            if (!ExcuteEly(FIBTemp))
            {
                return false;
            }

            //等待切割完成
            while (true)
            {
                Thread.Sleep(10000);
                if (iSEM.GetFIBMode() == 0)
                {
                    break;
                }
            }

            return true;
        }
        public bool ModifyCutTemplate(float x1,float y1,float x2,float y2)
        {
            float x0 = 0, y0 = 0,  px = 0;
            //20201128将移动光束改为移动ely文件中的参数来实现沉积和切割
           

            float xc = (x1 + x2) / 2;
            float yc = (y1 + y2) / 2;
            x0 = xc - 512;
            y0 = 384 - yc;
            log.Info("移动像素 x = " + x0.ToString() + ",y=" + y0.ToString(), true);
            px = iSEM.GetPixelSize();
            //修改FIB切割的ELY文件坐标
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(FIBTemp);//加载baixml文件，xmlpath 为XML文件的路径du
            XmlNode xns = xmlDoc.SelectSingleNode("ELAYOUT/STRUCTURE_LIST/STRUCTURE/LAYER_REFERENCE/TRAPEZOID");
            if (xns != null)
            {
                XmlAttributeCollection attributeCol = xns.Attributes;
                //遍历自己点属性
                double width_a = Convert.ToDouble(attributeCol.GetNamedItem("width_a").Value) / 2.0;
                log.Info("XML-Width_a = " + width_a.ToString(), true);
                foreach (XmlAttribute attri in attributeCol)
                {
                    if (attri.Name == "x")
                    {
                        attri.InnerText = ((x0 * px) * 1000000 - width_a).ToString();
                        log.Info("XML-TopLeft-X = " + attri.InnerText, true);
                    }
                    else if (attri.Name == "y")
                    {
                        attri.InnerText = ((y0 * px) * 1000000 - 2.791).ToString();
                        log.Info("XML-TopLeft-Y = " + attri.InnerText, true);
                    }
                }
            }
            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;
                //遍历自己点属性
                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 - 2.791).ToString();
                    }
                }
            }
            xmlDoc.Save(FIBTemp);//保存的该XML文件,否则更新无效
            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;
        }
    }
}
