using Foresight.Fleet.Services.Asset;
using IronIntel.Contractor.Machines;
using IronIntel.Contractor.Maintenance;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace IronIntel.Contractor.Site
{
    public class SingleAssetViewBasePage : ContractorBasePage
    {
        protected void ProcessRequest(string method)
        {
            object result = null;
            try
            {
                string methodName = Request.Params["MethodName"];
                if (methodName != null)
                {
                    switch (methodName.ToUpper())
                    {
                        case "GETASSETDETAILINFO":
                            result = GetAssetDetailInfo();
                            break;
                        case "GETPMINFO":
                            result = GetPMInfo();
                            break;
                        case "GETASSETRENTAL":
                            result = GetAssetRental();
                            break;
                        case "GETASSETDETAILWORKSPACECONFIG":
                            result = GetAssetDetailWorkspaceConfig();
                            break;
                        case "GETASSETONOFFTIMELINE":
                            result = GetAssetOnOffTimeline();
                            break;
                        case "ISCALAMPPRIMARYLOCATION":
                            result = IsCalampPrimaryLocation();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                SystemParams.WriteLog("error", "SingleAssetViewBasePage", ex.Message, ex.ToString());
                throw ex;
            }
            string json = JsonConvert.SerializeObject(result);
            Response.Write(json);
            Response.End();
        }

        private object GetAssetDetailInfo()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    var clientdata = Request.Form["ClientData"].Split((char)170);
                    var custid = HttpUtility.HtmlDecode(clientdata[0]);
                    var assetidstr = HttpUtility.HtmlDecode(clientdata[1]);
                    long assetid = -1;
                    long.TryParse(assetidstr, out assetid);
                    if (string.IsNullOrWhiteSpace(custid))
                        custid = SystemParams.CompanyID;

                    AssetDetailInfo info = CreateClient<AssetQueryClient>(custid).GetAssetDetailInfo(custid, assetid);

                    AssetDetailItem assetdetail = new AssetDetailItem();
                    Helper.CloneProperty(assetdetail, info);
                    if (info.CurrentLocation != null)
                    {
                        assetdetail.CurrentLocation = new AssetAddressItem();
                        Helper.CloneProperty(assetdetail.CurrentLocation, info.CurrentLocation);
                    }
                    if (info.CurrentHours != null)
                    {
                        assetdetail.CurrentHours = new AssetEngineHoursItem();
                        Helper.CloneProperty(assetdetail.CurrentHours, info.CurrentHours);
                    }
                    if (info.CurrentIdleHours != null)
                    {
                        assetdetail.CurrentIdleHours = new AssetIdlehoursItem();
                        Helper.CloneProperty(assetdetail.CurrentIdleHours, info.CurrentIdleHours);
                    }
                    if (info.CurrentOdometer != null)
                    {
                        assetdetail.CurrentOdometer = new AssetOdometerItem();
                        Helper.CloneProperty(assetdetail.CurrentOdometer, info.CurrentOdometer);
                    }

                    return assetdetail;
                }
                else
                    return new AssetDetailItem();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        private object GetPMInfo()
        {
            try
            {
                var session = GetCurrentLoginSession();
                PMInfo ominfo = new PMInfo();
                if (session != null)
                {
                    var clientdata = Request.Form["ClientData"].Split((char)170);
                    var custid = HttpUtility.HtmlDecode(clientdata[0]);
                    var assetidstr = HttpUtility.HtmlDecode(clientdata[1]);
                    long assetid = -1;
                    long.TryParse(assetidstr, out assetid);
                    if (string.IsNullOrWhiteSpace(custid))
                        custid = SystemParams.CompanyID;

                    var client = CreateClient<AssetQueryClient>(custid);
                    var names = client.GetAssetPMScheduleNames(custid, assetid);
                    ominfo.ScheduleNames = string.Join(", ", names);
                    ominfo.AlertMessages = client.GetAssetPMAlertMessagess(custid, assetid);
                }
                return ominfo;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        private object GetAssetRental()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    var clientdata = Request.Form["ClientData"].Split((char)170);
                    var custid = HttpUtility.HtmlDecode(clientdata[0]);
                    var assetidstr = HttpUtility.HtmlDecode(clientdata[1]);
                    long assetid = -1;
                    long.TryParse(assetidstr, out assetid);
                    if (string.IsNullOrWhiteSpace(custid))
                        custid = SystemParams.CompanyID;

                    var temp = CreateClient<AssetQueryClient>(custid).GetAssetCurrentRentalSimpleInfo(custid, assetid);
                    if (temp != null)
                    {
                        MachineRentalInfo rental = new MachineRentalInfo();
                        rental.RentalID = temp.ID;
                        rental.ProjectReturnDate = temp.PrjReturnDate;
                        rental.RentalRate = (decimal)temp.Rate;
                        rental.RentalDate = temp.RentalDate;
                        rental.ReturnDate = temp.ReturnDate;
                        rental.Term = temp.Term;
                        rental.TermUnit = temp.TermUnit;
                        return rental;
                    }
                    return null;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object GetAssetOnOffTimeline()
        {
            try
            {
                var clientdata = Request.Form["ClientData"].Split((char)170);
                var companyid = HttpUtility.HtmlDecode(clientdata[0]);
                if (string.IsNullOrEmpty(companyid))
                {
                    companyid = SystemParams.CompanyID;
                }
                var assetid = long.Parse(clientdata[1]);
                var date = DateTime.Parse(clientdata[2]);
                return CreateClient<AssetQueryClient>(companyid).GetAssetOnOffTimeline(companyid, assetid, date).Select(s =>
                {
                    double off;
                    if (s.Off != null)
                    {
                        off = s.Off.Value.TimeOfDay.TotalSeconds;
                    }
                    else
                    {
                        var now = CreateClient<Foresight.Fleet.Services.SystemUtil>().GetCustomerDateTimeNow(companyid);
                        if (now.Date == date.Date && (s.On == null || now > s.On.Value))
                        {
                            off = now.TimeOfDay.TotalSeconds;
                        }
                        else
                        {
                            // 23:59:59
                            off = 24 * 60 * 60 - 1;
                        }
                    }
                    return new
                    {
                        HasOn = s.On != null,
                        Start = s.On?.TimeOfDay.TotalSeconds ?? 0,
                        HasOff = s.Off != null,
                        End = off
                    };
                });
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private string IsCalampPrimaryLocation()
        {
            try
            {
                var clientdata = Request.Form["ClientData"].Split((char)170);
                var companyid = HttpUtility.HtmlDecode(clientdata[0]);
                if (string.IsNullOrEmpty(companyid))
                {
                    companyid = SystemParams.CompanyID;
                }
                var assetid = long.Parse(clientdata[1]);
                return CreateClient<AssetQueryClient>(companyid).IsCalampPrimaryLocation(companyid, assetid) ? "1" : "0";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object GetAssetDetailWorkspaceConfig()
        {
            return MachineDetailWorkspace.GetConfig();
        }

        class AssetDetailItem
        {
            public long ID { get; set; }
            public string Name { get; set; }
            public string Name2 { get; set; }
            public string VIN { get; set; }
            public int MakeID { get; set; }
            public int TypeID { get; set; }
            public int ModelID { get; set; }
            public string MakeName { get; set; }
            public string ModelName { get; set; }
            public string TypeName { get; set; }
            public int MakeYear { get; set; }
            public string Description { get; set; }
            public string AquisitionType { get; set; }
            public string CostCenter { get; set; }
            public DateTime? DateAddedUtc { get; set; }
            public string DateAddedUtcStr { get { return DateAddedUtc.ToString(); } }
            public DateTime? DateAddedLocal { get; set; }
            public string DateAddedLocalStr { get { return DateAddedLocal.ToString(); } }
            public string GroupNames { get; set; }
            public string CurrentJobSiteNames { get; set; }
            public string LastForeman { get; set; }
            public string AssetIconUrl { get; set; }
            public string MapViewIconUrl { get; set; }
            public bool OnRoad { get; set; }
            public bool TelematicsEnabled { get; set; }

            public AssetAddressItem CurrentLocation { get; set; }
            public AssetEngineHoursItem CurrentHours { get; set; }
            public AssetIdlehoursItem CurrentIdleHours { get; set; }
            public AssetOdometerItem CurrentOdometer { get; set; }
            public string DisplayName
            {
                get
                {
                    //DisplayName取值顺序为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;
                }
            }
        }

        class AssetAttributeItemBase
        {
            public long AssetID { get; set; }
            public DateTime AsofTime { get; set; }//utc time
            public string AsofTimeStr { get { return AsofTime.ToString(); } }
            public DateTime AsofTimeLocal { get; set; }//AsOftime的用户本地时间表示
            public string AsofTimeLocalStr { get { return AsofTimeLocal.ToString(); } }
            public string DataSource { get; set; }
            public string SubSource { get; set; }
            public string DataSourceName { get; set; }
        }

        class AssetAddressItem : AssetAttributeItemBase
        {
            public double Latitude { get; set; }
            public double Longitude { get; set; }
            public string Street { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string PostalCode { get; set; }
            public string Country { get; set; }
            public bool IsPrimary { get; set; }
            public string Address { get; set; }
        }

        class AssetEngineHoursItem : AssetAttributeItemBase
        {
            public double Hours { get; set; }

            private double _Corrected;
            public double Corrected
            {
                get
                {
                    return _Corrected;
                }
                set
                {
                    value = value > 0 ? value : 0;
                    _Corrected = Math.Round(value, 2);
                }
            }
            public bool IsPrimary { get; set; }
        }
        class AssetIdlehoursItem : AssetAttributeItemBase
        {
            public double Hours { get; set; }
            public bool IsPrimary { get; set; }
        }
        class AssetOdometerItem : AssetAttributeItemBase
        {
            public string UOM { get; set; }

            private double _Odometer;
            public double Odometer
            {
                get
                {
                    return _Odometer;
                }
                set
                {
                    value = value > 0 ? value : 0;
                    _Odometer = Math.Round(value, 2);
                }
            }
            private double _Corrected;
            public double Corrected
            {
                get
                {
                    return _Corrected;
                }
                set
                {
                    value = value > 0 ? value : 0;
                    _Corrected = Math.Round(value, 2);
                }
            }
            public bool IsPrimary { get; set; }
        }

        class PMInfo
        {
            public string ScheduleNames { get; set; }
            public string[] AlertMessages { get; set; }
        }

    }
}