using Foresight;
using Foresight.Fleet.Services.Asset;
using IronIntel.Services.Common;
using IronIntel.Services.MapView;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IronIntel.Contractor.MapView
{
    public class MachineViewItem
    {
        public string VIN { get; set; }
        public long ID { get; set; }
        public string Name { get; set; }
        public string Name2 { get; set; }
        public string IconUrl { get; set; }
        public string EmptyIconUrl { get; set; }//不包含机器图标
        public string MachineType { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public int MakeYear { get; set; }

        private double _EngineHours;
        public double EngineHours
        {
            get
            {
                return _EngineHours;
            }
            set
            {
                value = value > 0 ? value : 0;
                _EngineHours = Math.Round(value, 2);
            }
        }
        public DateTime EngineHoursDate { get; set; }
        public int TypeID { get; set; }
        public string AlertTip { get; set; }
        public bool OnSite { get; set; }
        public string JobSiteName { get; set; }//当前所在的JobSiteName
        public double DistanceFromSite { get; set; }//机器与Jobsite之间的距离
        public bool WithinSite { get; set; }//机器是否在JobSite多边形范围内
        public LocationViewItem Location { get; set; }

        private double _Odometer;
        public double Odometer
        {
            get
            {
                return _Odometer;
            }
            set
            {
                value = value > 0 ? value : 0;
                _Odometer = Math.Round(value, 2);
            }
        }
        public string OdometerUOM { get; set; }
        public int Onroad { get; set; }
        public string IconFileName { get; set; }
        public string MoveStatus { get; set; }
        public int Directionalheading { get; set; }
        public int MapAlertLayerPriority { get; set; }
        public Int64 GpsDeviceID { get; set; } //空  -1
        public string AssetGroupNames { get; set; }
        public string ShowName
        {
            get
            {
                //Name取值顺序为Name2,Name,VIN,ID用于前端显示
                string name = Name2;
                if (string.IsNullOrWhiteSpace(name))
                    name = Name;
                if (string.IsNullOrWhiteSpace(name))
                    name = VIN;
                if (string.IsNullOrWhiteSpace(name))
                    name = ID.ToString();
                return name;
            }
        }//由于地图显示及排序的名称

        public string EngineHoursDateText
        {
            get
            {
                if (EngineHoursDate != DateTime.MinValue)
                {
                    return EngineHoursDate.ToString();
                }
                return "";
            }
        }
    }

    public class JobSiteViewItem
    {
        public Int64 ID { get; set; }
        public string Name { get; set; }
        public string[] Types { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Radius { get; set; }
        public string Radius_UOM { get; set; }
        public string ContractorID { get; set; }
        public string ColorString { get; set; }
        public IIColor Color { get; set; }
        public string Notes { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public PostionItem[] Polygon { get; set; }
        public Int64 BaseOnMachineID { get; set; }
        public string Code { get; set; }
        public long[] Assets { get; set; }
        public bool IsDeleted { get; set; }

        public string strStartDate
        {
            get
            {
                if (StartDate == DateTime.MinValue)
                {
                    return "";
                }
                else
                {
                    return StartDate.ToShortDateString();
                }
            }
        }



        public string strEndDate
        {
            get
            {
                if (EndDate == DateTime.MinValue)
                {
                    return "";
                }
                else
                {
                    return EndDate.ToShortDateString();
                }
            }
        }

        public MachineViewItem[] Machines { get; set; }
    }

    public class AssetGroupViewItem
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public long[] Assets { get; set; }
    }

    public class CompanyLocationViewItem
    {
        public int ID { get; set; }
        public string CompanyID { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string LocationName { get; set; }
        public string Notes { get; set; }
        public string IconUrl { get; set; }
    }

    public class MapAlertViewDefinitionItem
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public MapAlertLayerDefinitionItem[] Layers { get; set; }
        public List<LookupDataSourceDataItem> LookupDataSources { get; set; }
    }

    public class LookupDataSourceDataItem
    {
        public string ID { get; set; }

        private List<KeyValuePair<string, string>> _Items = new List<KeyValuePair<string, string>>();
        public List<KeyValuePair<string, string>> Items
        {
            get
            {
                return _Items;
            }
        }
        public string Name { get; set; }
    }

    public class MapAlertLayerDefinitionItem
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string IconColor { get; set; }
        public string AlertLayerType { get; set; }//Primary/Secondary
        public string LegendUrl { get; set; }
        public DbQueryParameterItem[] CriteriaSQLParameters { get; set; }
        public DbQueryParameterItem[] AlertSQLParameters { get; set; }
        public AlertLayerPivotViewItem[] Pivots { get; set; }

    }

    public class DbQueryParameterItem
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Caption { get; set; }
        public string Description { get; set; }
        public DataTypes DataType { get; set; }
        public string DefaultValue { get; set; }
        public string ParameterValue { get; set; }
        public QueryParameterDisplayStyles DisplayStyle { get; set; }
        public string LookupDatasourceID { get; set; }
        public bool DisplayCaptionField { get; set; }//当参数显示为dropdown时,指示显示Key或者text
        public bool IsField { get; set; }//表明该参数名是一个数据库参数或是结果集的字段,如果是结果集的字段,则该定义必须要与lookupdatasource关联。
        public bool IsAllAllowed { get; set; }
        public bool MutipleSelect { get; set; }
    }

    public class QueryParameterSource
    {
        public string ID { get; set; }
        public string Name { get; set; }
    }

    public class LocationViewItem
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public DateTime LocationTime { get; set; }
        public string LocationTimeText
        {
            get
            {
                if (LocationTime != DateTime.MinValue)
                {
                    return LocationTime.ToString();
                }
                return "";
            }
        }
        public double Speed { get; set; } = -1;
        public string SpeedUnit { get; set; }
        public double PostedSpeed { get; set; } = -1;
        public string PostedSpeedUnit { get; set; }
        public string Street { get; set; } = string.Empty;
        public string IconURL { get; set; } = string.Empty;
        public List<KeyValuePair<string, string>> SmartWitnessVideoUrl { get; set; }
        public SpeedingBehaviors SpeedingBehavior { get; set; }
        public HarshDrivingEvents HarshDringEvent { get; set; }
    }

    public class MachineLocationHistoryViewItem
    {
        public MachineViewItem Machine { get; set; }
        public LocationViewItem[] Locations { get; set; }
    }


    public class MachineTypeItem
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    /// <summary>
    /// 供JobSite选中的Mahcine
    /// </summary>
    public class AvailableMachines
    {
        public MachineViewItem[] Assigned { get; set; }
        public MachineViewItem[] Unassigned { get; set; }
    }
    public struct PostionItem
    {
        public double Latitude;
        public double Longitude;

        public PostionItem(double latitude, double longitude)
        {
            Latitude = latitude;
            Longitude = longitude;
        }
    }

    public class ShapeFileItem
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public string Notes { get; set; }
    }
}