using MainShell.Log;
using SemiconductorVisionAlgorithm.SemiCalib;
using System;
using System.Threading.Tasks;
using System.Windows;

namespace MainShell.Motion
{
    /// <summary>
    /// 坐标转换实现
    /// 此实现为基础版本，实际应用中应由标定系统提供具体实现
    /// </summary>
    public class CoordinateTransformer : ICoordinateTransformer
    {
        /// <summary>
        /// 将图像坐标转换为轴位置
        /// </summary>
        public async Task<CoordinateTransformResult> TransformAsync(
           Point ruler, Point pixel, string cameraName)
        {
            try
            {
                CoordinateTransformResult result = new CoordinateTransformResult();

                // 模拟异步操作
                await Task.Delay(10).ConfigureAwait(false);

                // 此处应根据机器标定数据进行坐标变换
                // 示例流程：
                // 1. 获取相机标定参数（内参、外参）
                // 2. 获取机械臂/轴的标定数据
                // 3. 将图像坐标(centerX, centerY)转换为各轴的目标位置

                SemiconductorVisionAlgorithm.SemiParams.Point semiRulerPoint = new SemiconductorVisionAlgorithm.SemiParams.Point(ruler.X, ruler.Y);
                SemiconductorVisionAlgorithm.SemiParams.Point semiPixelPoint = new SemiconductorVisionAlgorithm.SemiParams.Point(pixel.X, pixel.Y);


                CalibManager.Instance.get_real_by_pixel(cameraName, 0, semiRulerPoint, semiPixelPoint
                , out SemiconductorVisionAlgorithm.SemiParams.Point point);

                result.TargetPoint = new Point(point.X, point.Y);
                result.Succeeded = true;

                return result;
            }
            catch (Exception ex)
            {
                string.Format("Coordinate transformation failed: {0}", ex.Message).LogSysError();
                return new CoordinateTransformResult
                {
                    Succeeded = false,
                    Message = ex.Message,
                    Exception = ex
                };
            }
        }

      
    }
}
