using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using SmartSEMControl;

namespace MeasureThread
{
    class Photo : IPhoto
    {
        NLog.Logger log;
        private PhotoParam prm;
        private ISEMControl iSEM;
        public Photo( ISEMControl sem)
        {
            this.log = NLog.LogManager.GetCurrentClassLogger();
           
            this.iSEM = sem ?? throw new ArgumentNullException(nameof(sem));
        }
        public void SetPhotoParam(PhotoParam a_prm)
        {
            this.prm = a_prm ?? throw new ArgumentNullException(nameof(a_prm));
        }
        public PhotoParam GetParam()
        {
            if (prm == null)
            {

                prm = new PhotoParam();
            }
            return prm;
        }
        public void EnforceBrightness()
        {
            float b = iSEM.GetBrightness();
            Thread.Sleep(100);
            iSEM.SetBrightness(b + 1);
            Thread.Sleep(200);
        }
        public void EnforceContrast()
        {
            float c= iSEM.GetContrast();
            Thread.Sleep(100);
            iSEM.SetContrast(c + 1);
            Thread.Sleep(200);
        }
        public bool TakePhoto()
        {
            

            if (!iSEM.SetMagnification(prm.Mag))
            {
                return false;
            }


            float set = prm.Mag;
            Thread.Sleep(500);
            float current = iSEM.GetMagnification();
            while (Math.Abs(current - set) > 1)
            {
                iSEM.SetMagnification(set);
                Thread.Sleep(200);
                current = iSEM.GetMagnification();
            }

            iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//iSEM.CmdSaveRate();
           
            Thread.Sleep(200);
            float cycle_time = iSEM.GetCycleTime();
            Thread.Sleep(Convert.ToInt32(cycle_time) + 100);

            //拍照前改变速度,延时          
            iSEM.CmdLineScan();
            Thread.Sleep(200);
             cycle_time = iSEM.GetCycleTime();
            Thread.Sleep(Convert.ToInt32(cycle_time) + 100);

            if (!GetImage(prm.savePath))
            {

                log.Error("SEM拍照失败", false);
                return false;
            }
            iSEM.CmdPixelScan();
            Thread.Sleep(200);
            iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//CmdFocusRate(4);
            cycle_time = iSEM.GetCycleTime();
            Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
           
            return true;
        }
        //拍图
        public bool GetImage(String a_fileName)
        {
            log.Info("获取分辨率开始！", true);
            //3. 获取分辨率
            int[] ImageSize = iSEM.GetImageStore();
            if (ImageSize[0] == 0 || ImageSize[1] == 0)
            {
                return false;
            }
            short width = (short)ImageSize[0];
            short height = (short)ImageSize[1];
            log.Info("获取分辨率结束！" + a_fileName + "====" + width + "*" + height, true);
            //4. 抓图
            if (!iSEM.GrabImage(a_fileName, 0, 0, width, height, 0))
            {
                log.Info("抓图失败！", true);
                return false;
            }

            return true;
        }
    }
}
