using ProjectBase.Data.Logs;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIASUN.Autopilot.GNSS
{
    /// <summary>
    /// WGS84和平面坐标系转换
    /// </summary>
    public class CoordinateConversion
    {
        // minLongitude, maxLatitude
        private WGS84Points point0;

        // maxLongitude, minLatitude
        private WGS84Points point1;

        //// minLongitude, maxLatitude
        //private WGS84Points point2;

        //// 显示区域的宽度
        //private double screenx;

        //// 显示区域的高度
        //private double screeny;

        private PointF imagePoint0;

        private PointF imagePoint1;

        // x轴比例因子
        private double scalex;

        // y轴比例因子
        private double scaley;

        //public CoordinateConversion(double imageWidth,double imageHeight, WGS84Points point0, WGS84Points point1, WGS84Points point2)
        //{
        //    this.screenx = imageWidth;
        //    this.screeny = imageHeight;
        //    this.point0 = point0;
        //    this.point1 = point1;
        //    this.point2 = point2;
        //}

        public CoordinateConversion(PointF imagePoint0, PointF imagePoint1, WGS84Points point0, WGS84Points point1)
        {
            this.imagePoint0 = imagePoint0;
            this.imagePoint1 = imagePoint1;
            this.point0 = point0;
            this.point1 = point1;
        }

        /// <summary>
        /// 获取WGS84转换到屏幕坐标的x轴比例因子
        /// </summary>
        private void GetScaleX()
        {
            try
            {
                double lon = 0.0;
                float x = 0.0F;
                lon = point0.x - point1.x;
                x = imagePoint0.X - imagePoint1.X;
                //scalex = ((point1.x - point0.x) * 3600) / screenx;
                scalex = (lon * 3600) / x;
            }
            catch(Exception ex)
            {
                LogHelper.log.Error("计算x比例因子出现错误", ex);
            }
        }

        /// <summary>
        /// 获取WGS84转换到屏幕坐标的Y轴比例因子
        /// </summary>
        private void GetScaleY() 
        {
            try
            {
                double lat = 0.0;
                float y = 0.0F;
                lat = point0.y - point1.y;
                y = imagePoint0.Y - imagePoint1.Y;
                scaley = (lat * 3600) / y;
                //scaley = ((point0.y - point2.y) * 3600) / screeny;
            }
            catch (Exception ex)
            {
                LogHelper.log.Error("计算y比例因子出现错误", ex);
            }
        }

        /// <summary>
        /// 从WGS84转换成屏幕坐标
        /// </summary>
        /// <param name="lon"></param>
        /// <param name="lat"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void GetWGS84ToGauss(double lon,  double lat, out double x, out double y)
        {
            x = 0.0;
            y = 0.0;
            try
            {
                GetScaleX();
                GetScaleY();
                //x = (lon - point0.x) * 3600 / scalex;
                //y = (point0.y - lat) * 3600 / scaley;
                x = imagePoint0.X -( (point0.x - lon) * 3600 / scalex);
                y = imagePoint0.Y - ((point0.y - lat) * 3600 / scaley);
            }
            catch (Exception ex)
            {
                LogHelper.log.Error("从WGS84转换成屏幕坐标出现错误", ex);
            }
        }

        /// <summary>
        /// 从屏幕坐标转换成WGS84
        /// </summary>
        /// <param name="lon"></param>
        /// <param name="lat"></param> 
        /// <param name="x"></param> 
        /// <param name="y"></param>  
        public void GetGaussToWGS84(double x, double y, out double lon, out double lat)
        {
            lon = 0.0;
            lat = 0.0;
            try
            {
                GetScaleX();
                GetScaleY();
                //lon = x * scalex /3600 + point0.x;
                //lat = point0.y - y * scaley / 3600;
                lon = point0.x - ((imagePoint0.X - x) * scalex / 3600);
                lat = point0.y - ((imagePoint0.Y - y) * scaley / 3600);
            }
            catch (Exception ex)
            {
                LogHelper.log.Error("从屏幕坐标转换成WGS84出现错误", ex);
            }
        }
    }
}
