using Cognex.VisionPro;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.ToolBlock;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace WaferAdjust
{
    public delegate void OnFitCircleResult(double x, double y, double r, double rms);
    internal class FitCircleToolBlock
    {
        private CogToolBlock cogToolBlock;
        private bool initialized = false;
        public event OnToolReady OnToolReady;
        public event OnFitCircleResult OnFitCircleResult;
        public CogImage8Grey FitCircleImage;
        public void Initialize(string vpp)
        {
            try
            {
                if (cogToolBlock != null)
                {
                    cogToolBlock.Ran -= CogToolBlock_Ran;
                    cogToolBlock.Dispose();
                    cogToolBlock = null;
                }
                initialized = false;
                cogToolBlock = CogSerializer.LoadObjectFromFile(vpp) as CogToolBlock;
                cogToolBlock.Ran += CogToolBlock_Ran;
                initialized = true;
                OnToolReady?.Invoke(initialized);
                LogHelper.LogInfo("FitCircleToolBlock initialized successfully: " + vpp);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex.Message);
            }
        }
        private void CogToolBlock_Ran(object sender, EventArgs e)
        {
            try
            {
                FitCircleImage = cogToolBlock.Outputs["OutputImage"].Value as CogImage8Grey;
                OnFitCircleResult?.Invoke(Math.Round((double)cogToolBlock.Outputs["CenterX"].Value, 3),
                    Math.Round((double)cogToolBlock.Outputs["CenterY"].Value, 3),
                    Math.Round((double)cogToolBlock.Outputs["Radius"].Value, 3),
                    Math.Round((double)cogToolBlock.Outputs["RMSError"].Value, 3));
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex.Message + ex.StackTrace);
            }
        }
        public void Run(Bitmap bmp, List<PointInfo> pointInfos)
        {
            try
            {
                CogImage8Grey image8Grey = new CogImage8Grey(bmp);
                Run(image8Grey, pointInfos);
            }
            catch (Exception ex)
            {
                LogHelper.LogError("ScanToolBlock Run Error: " + ex.Message);
            }
        }
        public void Run(CogImage8Grey image8Grey, List<PointInfo> pointInfos)
        {
            try
            {
                cogToolBlock.Inputs["OutputImage"].Value = image8Grey;
                CogFitCircleTool fitCircleTool = cogToolBlock.Tools["CogFitCircleTool1"] as CogFitCircleTool;
                fitCircleTool.RunParams.NumPoints = 0;
                foreach (var item in pointInfos)
                    fitCircleTool.RunParams.AddPoint(item.X, item.Y);
                cogToolBlock.Run();
            }
            catch (Exception ex)
            {
                LogHelper.LogError("ScanToolBlock Run Error: " + ex.Message);
            }
        }
        public bool Ready()
        {
            return initialized;
        }
        public void Stop()
        {
            if (cogToolBlock != null)
            {
                cogToolBlock.Ran -= CogToolBlock_Ran;
                cogToolBlock.Dispose();
                cogToolBlock = null;
            }
        }
    }
}
