using BHJD.DEMdll.Entity;
using BHJD.DEMdll.Public;
using System.Diagnostics;
using static BHJD.DEMdll.Public.IHttpHelper;
using Unity.Mathematics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MuShiApp
{
    public class EquationHelper
    {
        //数据库访问类,dll有
        //IDbHelper _db = null;
        //http请求类,dll有
        IHttpHelper m_HttpHelper = null;

        public string baseUrl;
        
        public double x;
        public EquationHelper(string baseUrl )
        {
            this.baseUrl = baseUrl;
            m_HttpHelper = new HttpHelper();
            //this._db = db;
            //m_HttpHelper = Factory.Load();
        }
         /// <summary>
        /// 
        /// </summary>
        /// <param name="aircraftPoint">飞机坐标</param>
        /// <param name="targetPoint">目标点坐标</param>
        /// <param name="course">航向(度数)</param>
        /// <param name="currentDate">(当前日期)</param>
        /// <param name="windspeed">风速(km/h)</param>
        /// <param name="wavehigh">浪高(m)</param>
        /// <param name="targetpye">遇险目标类型</param>
        /// <param name="typee">救援场景类型</param>
        /// <returns></returns>
        public double getProbability(double3 aircraftPoint, double3 targetPoint, double course,double windspeed, double wavehigh, string targetpye, string type)
        {
            try
            {
                double Cw = 1;
                
                if (windspeed > 46 && wavehigh > 1.5)
                {
                    if (targetpye == "落水人员")
                    {
                        Cw = 0.25;
                    }
                    else
                    {
                        Cw = 0.6;
                    }
                }
                else if (28 < windspeed && windspeed < 46 && 1 < wavehigh && wavehigh < 1.5)
                {
                    if (targetpye == "船")
                    {
                        Cw = 0.5;
                    }
                    else
                    {
                        Cw = 0.9;
                    }
                }
                else if (0 < windspeed && windspeed < 28 && 0 < wavehigh && wavehigh < 1)
                {
                    Cw = 1;
                }
                double Cv = 1;
                
                System.Random ran = new System.Random();
                double x = getx(aircraftPoint, targetPoint, course);
                
                double V = getVisibility(getCityName(targetPoint.x, targetPoint.y), DateTime.Now.ToString("yyyy-MM-dd HH"));
                
                
                if (V < 6)
                {
                    Cv = 0.4;
                }
                else if (6 < V && V < 9)
                {
                    Cv = 0.6;
                }
                else if (9 < V && V < 19)
                {
                    Cv = 0.8;
                }
                else if (19 < V && V < 28)
                {
                    Cv = 0.9;
                }
                else if (37 < V)
                {
                    Cv = 1;
                }

              
                double pt = this.nextDouble(ran, 0.800, 0.999, 8);
                double pb = getPb(targetPoint.x, targetPoint.y);
                double D = 0;
                double C = Math.Round((double)((pt - pb) / pt), 8);
                if(type =="陆地")
                {
                    D = (V / 3.912) * Math.Log10(C / 0.02);
                }
                else if(type =="海上")
                {
                    if(targetpye == "船")
                    { D = 31*Cv*Cw; }
                    else if(targetpye == "落水人员")
                    { D =0.2 * Cv * Cw; }
                }
                double px = 1 - Math.Exp((-1 * D * D) / (4 * Math.PI * x * x));
                return px;
            }
            catch (Exception ex)
            {
                Debug.Print("error!!!!!getProbability:" + ex.ToString());
                return -1;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aircraftPoint">飞机坐标</param>
        /// <param name="targetPoint">目标点坐标</param>
        /// <param name="course">航向(度数)</param>
        /// <param name="currentDate">(当前日期)</param>
        /// <returns></returns>
        public double getProbability(double3 aircraftPoint, double3 targetPoint, double course)
        {
            try
            {
                x = getx(aircraftPoint, targetPoint, course);
              
                double V = getVisibility(getCityName(targetPoint.x, targetPoint.y), DateTime.Now.ToString("yyyy-MM-dd HH"));
               
                System.Random ran = new System.Random();
                double pt = this.nextDouble(ran, 0.800, 0.999, 8);

                double pb = getPb(targetPoint.x, targetPoint.y);

                double C = Math.Round((double)((pt - pb) / pt), 8);
                double D = (V / 3.912) * Math.Log10(C / 0.02);

                double px = 1 - Math.Exp((-1 * D * D) / (4 * Math.PI * x * x));
                return px;
            }
            catch (Exception ex)
            {
                Console.WriteLine("error!!!!!getProbability:" + ex.ToString());
                return -1;
            }
        }

        public double getX(double3 targetPoint)
        {
            try
            {
                double result = -1;

                double V = getVisibility(getCityName(targetPoint.x, targetPoint.y), DateTime.Now.ToString("yyyy-MM-dd HH"));

                double pt = 0.85;

                double pb = getPb(targetPoint.x, targetPoint.y);

                double C = Math.Round((double)((pt - pb) / pt), 3);
                double D = (V / 3.912) * Math.Log10(C / 0.02);

                double x = Math.Sqrt(-1 * D * D / Math.Log(1 - 0.85) / 4 / Math.PI);
                return x;
            }
            catch (Exception ex)
            {
                Debug.Print("error!!!!!getX:" + ex.ToString());
                return -1;
            }
        }

        private double nextDouble(System.Random ran, double minValue, double maxValue, int decimalPlace)
        {
            double randNum = ran.NextDouble() * (maxValue - minValue) + minValue;
            return Convert.ToDouble(randNum.ToString("f" + decimalPlace));
        }

        private double getVisibility(string city_name, string weather_date)
        {
            try
            {
                //调在线气象接口,封装到服务中了
                HttpCmd cmd = new HttpCmd
                {
                    m_RequestType = HttpRequestType.GET,
                    m_Addr = $"{baseUrl}rescue-platform-service/api/v1/tbMeteorology/getInfosByNowApi",
                    m_Args = new List<string> { "city_name", "weather_date" }
                };
                string response = m_HttpHelper.Request(cmd, new List<string> { city_name, weather_date });
                R data = JsonConvert.DeserializeObject<R>(response);
                if (data != null && data.code == 200)
                {
                    NAdtListDto rt = JsonConvert.DeserializeObject<NAdtListDto>(data.data.ToString());
                    double fl = Convert.ToDouble(rt.wtVisibility);
                    if (fl.Equals(0))
                    {
                        fl = 15;
                    }
                    return fl;
                }
                return 15;
            }
            catch (Exception ex)
            {
                Debug.Print("error!!!!!getVisibility");
                Debug.Print(ex.ToString());
                return 15;
            }
        }

        //rescue-platform-service/api/v1/dem/getCityName
        
        // {
        //     "msg": "success",
        //     "code": 200,
        //     "cityName": "承德市"
        // }
        private string getCityName(double lon, double lat)
        {
            string cityName = "北京";
            // try
            // {
            //     string sql = string.Format("select a.f_xzqdm cityCode, a.f_xzqmc cityName from xzq_ds a where ST_Contains(shape,st_geomfromtext('POINT({0} {1})',4326)) limit 1", lon, lat);
            //     DataTable dt = this._db.DoQueryEx(sql);
            //     if (dt != null && dt.Rows.Count > 0)
            //     {
            //         cityName = dt.Rows[0][1].ToString().Replace("市", "").Replace("自治区", "");
            //     }
            // }
            // catch (Exception ex)
            // {
            //     Debug.Print("error!!!!!getCityName");
            //     Debug.Print(ex.ToString());
            // }
            
            try
            {
                //调在线气象接口,封装到服务中了
                HttpCmd cmd = new HttpCmd
                {
                    m_RequestType = HttpRequestType.GET,
                    m_Addr = $"{baseUrl}rescue-platform-service/api/v1/dem/getCityName",
                    m_Args = new List<string> { "lon", "lat" }
                };
                string response = m_HttpHelper.Request(cmd, new List<string> { lon.ToString(), lat.ToString() });
                JObject jObject = JObject.Parse(response);
                cityName = jObject["cityName"].ToString().Replace("市", "").Replace("自治区", "");

            }
            catch (Exception ex)
            {
                Debug.Print("error!!!!!getVisibility");
                Debug.Print(ex.ToString());
            }
            return cityName;
        }

        //rescue-platform-service/api/v1/dem/getCategory
        // {
        //     "msg": "success",
        //     "code": 200,
        //     "category": "7 草丛"
        // }
        private double getPb(double lon, double lat)
        {
            double pb = 0.13;
            try
            {
                // string sql_zb = string.Format("select f_category from ly_vegetation where  ST_Contains(st_geomfromtext(st_astext(shape) ),st_geomfromtext('POINT({0} {1})'))", lon, lat);
                // DataTable dt_zb = _db.DoQueryEx(sql_zb);
                
                //调在线气象接口,封装到服务中了
                HttpCmd cmd = new HttpCmd
                {
                    m_RequestType = HttpRequestType.GET,
                    m_Addr = $"{baseUrl}rescue-platform-service/api/v1/dem/getCategory",
                    m_Args = new List<string> { "lon", "lat" }
                };
                string response = m_HttpHelper.Request(cmd, new List<string> { lon.ToString(), lat.ToString() });

                JObject jObject = JObject.Parse(response);
                string fcategory = jObject["category"].ToString();
                switch (fcategory)
                {
                    case "1 针叶林":
                        pb = 0.13;
                        break;
                    case "10 高山植被":
                        pb = 0.13;
                        break;
                    case "11 栽培植被":
                        pb = 0.13;
                        break;
                    case "2 针阔叶混交林":
                        pb = 0.13;
                        break;
                    case "3 阔叶林":
                        pb = 0.13;
                        break;
                    case "4 灌丛":
                        pb = 0.13;
                        break;
                    case "5 荒漠":
                        pb = 0.13;
                        break;
                    case "6 草原":
                        pb = 0.13;
                        break;
                    case "7 草丛":
                        pb = 0.13;
                        break;
                    case "8 草甸":
                        pb = 0.13;
                        break;
                    case "9 沼泽":
                        pb = 0.13;
                        break;
                    default:
                        pb = 0.13;
                        break;
                }
            }
            catch (Exception ex)
            {
                Debug.Print("error!!!!!getPb");
                Debug.Print(ex.ToString());
            }
            return pb;
        }

        //rescue-platform-service/api/v1/dem/getDistance
        
        // {
        //     "msg": "success",
        //     "code": 200,
        //     "data": {
        //         "differ": 328.31408076,
        //         "azimuth": 2.400469679065642
        //     }
        // }
        private double getx(double3 aircraftPoint, double3 targetPoint, double course)
        {
            double result = -1;
            // string sql = string.Format("select ST_Distance( ST_SetSRID(ST_MakePoint({0},{1}),4326)::geography, ST_SetSRID(ST_MakePoint({2},{3}),4326)::geography) as differ,ST_azimuth(st_geomfromtext('POINT({0} {1})'),st_geomfromtext('POINT({2} {3})')) as azimuth", aircraftPoint.x, aircraftPoint.y, targetPoint.x, targetPoint.y);
            // DataTable dt = _db.DoQueryEx(sql);
            HttpCmd cmd = new HttpCmd
            {
                m_RequestType = HttpRequestType.GET,
                m_Addr = $"{baseUrl}rescue-platform-service/api/v1/dem/getDistance",
                m_Args = new List<string> { "lon_air", "lat_air","lon_target","lat_target" }
            };
            string response = m_HttpHelper.Request(cmd, new List<string> {aircraftPoint.x.ToString(), aircraftPoint.y.ToString(), targetPoint.x.ToString(), targetPoint.y.ToString()});

            JObject jObject = JObject.Parse(response);
            
            
            double differ = double.Parse(jObject["data"]["differ"].ToString());
            double azimuth = double.Parse(jObject["data"]["azimuth"].ToString());
           
            // differ = Convert.ToDouble(dt.Rows[0][0].ToString());
            azimuth = 180 / Math.PI * azimuth;//Convert.ToDouble(dt.Rows[0][1].ToString());
            double angle = course - azimuth;
            if (angle < 0)
            {
                angle = -1 * angle;
            }
            double x = differ * Math.Sin(angle * Math.PI / 180) / 1000;
            return x;
        }
    }
}