using Cognex.VisionPro;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.ToolBlock;
using QWhale.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WaferAdjust
{
    public delegate void OnGetCircleResult(double x, double y, double r);
    public delegate void OnToolReady(bool ready);
    public delegate void OnGetNashResult(double x, double y);
    internal class GetCircleToolBlock
    {
        private CogToolBlock cogToolBlock;
        private bool initialized = false;
        public event OnToolReady OnToolReady;
        public event OnGetCircleResult OnGetCircleResult;
        public event OnGetNashResult OnGetNashResult;
        public List<PointInfo> PointInfos;
        public void Initialize(string vpp)
        {
            try
            {
                if (cogToolBlock != null)
                {
                    cogToolBlock.Ran -= CogToolBlock_Ran;
                    cogToolBlock.Dispose();
                    cogToolBlock = null;
                }
                initialized = false;
                PointInfos = new List<PointInfo>();
                cogToolBlock = CogSerializer.LoadObjectFromFile(vpp) as CogToolBlock;
                cogToolBlock.Ran += CogToolBlock_Ran;
                initialized = true;
                OnToolReady?.Invoke(initialized);
                LogHelper.LogInfo($"{vpp} initialized successfully: " + vpp);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex.Message);
            }
        }
        private void CogToolBlock_Ran(object sender, EventArgs e)
        {
            try
            {
                CogFindCircleTool cogFindCircleTool = cogToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
                PointInfos.Clear();

                if (cogFindCircleTool.Results != null)
                {
                    foreach (CogFindCircleResult item in cogFindCircleTool.Results)
                    {
                        if (item.Found && item.Used)
                            PointInfos.Add(new PointInfo(item.X, item.Y, item.DistanceToCircle));
                    }
                }

                OnGetCircleResult?.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));

                if ((int)cogToolBlock.Outputs["Results_Count"].Value == 1)
                {
                    OnGetNashResult?.Invoke(Math.Round((double)cogToolBlock.Outputs["TranslationX"].Value, 3),
                    Math.Round((double)cogToolBlock.Outputs["TranslationY"].Value, 3));
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex.Message + ex.StackTrace);
            }
        }
        public void Run(Bitmap bmp)
        {
            try
            {
                CogImage8Grey image8Grey = new CogImage8Grey(bmp);
                Run(image8Grey);
            }
            catch (Exception ex)
            {
                LogHelper.LogError("ScanToolBlock Run Error: " + ex.Message);
            }
        }
        public void Run(CogImage8Grey image8Grey)
        {
            try
            {
                cogToolBlock.Inputs["OutputImage"].Value = image8Grey;
                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;
            }
        }
    }
}
