using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assets.Scripts.Apis.Models
{
    public class MapDataModel
    {
        public string Type { get; set; }

        public double TotalDistance { get; set; }

        //private double? _TotalClimb;
        ///// <summary>
        ///// 累计爬升
        ///// </summary>
        //public double TotalClimb
        //{
        //    get
        //    {
        //        if(_TotalClimb == null)
        //        {
        //            var total = 0D;
        //            for (int i = 0; i < List.Count; i++)
        //            {
        //                if(i < List.Count - 1)
        //                {
        //                    var item = List[i];
        //                    var next = List[i + 1];
        //                    var value = next.Elevation - item.Elevation;
        //                    if(value > 0)
        //                    {
        //                        total += value;
        //                    }
        //                }
        //            }
        //            _TotalClimb = total;
        //        }

        //        return _TotalClimb.GetValueOrDefault(0);
        //    }
        //}

        //public double AverageGrade
        //{
        //    get
        //    {
        //    }
        //}

        private List<Item> _List;
        public List<Item> List
        {
            get
            {
                return _List;
            }
            set
            {
                _List = value;
                if (_List == null) return;
                this.CalcDistance();
                this.CalcGrade();
            }
        }

        private void CalcDistance()
        {
            //TODO:需要加上turfHelper
            //for (int i = 0; i < _List.Count - 1; i++)
            //{
            //    var value = TurfHelper.GetDistances(new double[] { _List[i].Point[1], _List[i].Point[0] }, new double[] { _List[i + 1].Point[1], _List[i + 1].Point[0] });
            //    _List[i].Distance = value;
            //}
        }
        /// <summary>
        /// 计算坡度数据
        /// </summary>
        private void CalcGrade()
        {
            for (int i = 0; i < this._List.Count - 1; i++)
            {
                var a = _List[i + 1].Elevation - _List[i].Elevation;
                if (a == 0)
                {
                    //grade.Add(0);
                    _List[i].Grade = 0;
                    continue;
                }
                //勾股定理
                var c = _List[i].Distance;//如果车停了下来，c为0
                //如果当前点到下一个点的距离是0，并且不是第一条记录，则坡度用上一个点的坡度
                if (c == 0 && i > 0)
                {
                    _List[i].Grade = _List[i - 1].Grade;
                    continue;
                }

                var b = Math.Sqrt(c * c - a * a);
                if (double.IsNaN(b))
                {
                    _List[i].Grade = 0;
                }
                else if (b == 0)//如果b等于，这就是垂直的墙壁
                {
                    _List[i].Grade = _List[i - 1].Grade;
                }
                else
                {
                    _List[i].Grade = Math.Round(a / b * 100, 4);
                }

            }
        }

        public MapDataModel()
        {
            //List = new List<Item>();
        }

        public class Item
        {
            /// <summary>
            /// 坐标(lat,lon)
            /// </summary>
            public double[] Point { get; set; }
            /// <summary>
            /// 距离(单位米)
            /// </summary>
            public double Distance { get; set; }
            /// <summary>
            /// 海拔
            /// </summary>
            public double Elevation { get; set; }

            public double Grade { get; set; }
        }

        public override string ToString()
        {
            //return base.ToString();
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }
    }
}
