using Foresight.Fleet.Services.AssetHealth;
using Foresight.Standard;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IronIntel.Contractor.Maintenance
{
    public class AlertInfo
    {
        private const char SPLITCHAR = (char)175;
        private const char SPLITCHAR1 = (char)181;
        public long AlertID { get; set; }
        public long WorkOrderID { get; set; }
        public string WorkOrderStatus { get; set; }
        public string AlertType { get; set; }
        public DateTime AlertTime_UTC { get; set; }
        public string AlertTime_UTCStr { get { return AlertTime_UTC == DateTime.MinValue ? "" : AlertTime_UTC.ToString(); } }
        public DateTime AlertLocalTime { get; set; }
        public string AlertLocalTimeStr { get { return AlertLocalTime == DateTime.MinValue ? "" : AlertLocalTime.ToString(); } }
        public bool Completed { get; set; }
        public long MachineID { get; set; }
        public int ModelID { get; set; }
        public string Model { get; set; }
        public int MakeID { get; set; }
        public string Make { get; set; }
        public string VIN { get; set; }
        public string MachineName { get; set; }

        private double _EngineHours;
        public double EngineHours
        {
            get
            {
                return _EngineHours;
            }
            set
            {
                value = value > 0 ? value : 0;
                _EngineHours = Math.Round(value, 2);
            }
        }
        private double _CurrentHours;
        public double CurrentHours
        {
            get
            {
                return _CurrentHours;
            }
            set
            {
                value = value > 0 ? value : 0;
                _CurrentHours = Math.Round(value, 2);
            }
        }
        public string Description { get; set; }
        public string FormatDescription(string desc)
        {
            if (!string.IsNullOrEmpty(desc))
            {
                if (desc.IndexOf("\r\n") > 0)
                    desc = desc.Substring(0, desc.IndexOf("\r\n"));
                if (desc.IndexOf("\n") > 0)
                    desc = desc.Substring(0, desc.IndexOf("\n"));
            }
            return desc;
        }
        public string ServiceDescription { get; set; } = "";
        public string ScheduleID { get; set; }
        public string IntervalID { get; set; }
        public bool Recurring { get; set; }
        public int Priority { get; set; }
        public double ExpectedCost { get; set; }
        public int AlertCount { get; set; }
        public List<long> RepeatedAlerts { get; set; }
        public int OpenWorkOrderCount { get; set; }//针对Alert对应的机器
        public string PMType { get; set; }
        public string AcknowledgedBy { get; set; }
        public string AcknowledgedByName { get; set; }
        public DateTime AcknowledgedTime_UTC { get; set; }
        public string AcknowledgedTime_UTCStr { get { return AcknowledgedTime_UTC == DateTime.MinValue ? "" : AcknowledgedTime_UTC.ToString(); } }
        public DateTime AcknowledgedTime_Local { get; set; }
        public string AcknowledgedTime_LocalStr { get { return AcknowledgedTime_Local == DateTime.MinValue ? "" : AcknowledgedTime_Local.ToString(); } }

        public string AcknowledgedComment { get; set; }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            TextableDTO.Append(sb, AlertID);
            TextableDTO.Append(sb, SPLITCHAR, WorkOrderID);
            TextableDTO.Append(sb, SPLITCHAR, WorkOrderStatus);
            TextableDTO.Append(sb, SPLITCHAR, AlertType);
            TextableDTO.Append(sb, SPLITCHAR, AlertTime_UTC);
            TextableDTO.Append(sb, SPLITCHAR, AlertTime_UTCStr);
            TextableDTO.Append(sb, SPLITCHAR, AlertLocalTime);
            TextableDTO.Append(sb, SPLITCHAR, AlertLocalTimeStr);
            TextableDTO.Append(sb, SPLITCHAR, Completed ? "1" : "0");
            TextableDTO.Append(sb, SPLITCHAR, MachineID);
            TextableDTO.Append(sb, SPLITCHAR, ModelID);
            TextableDTO.Append(sb, SPLITCHAR, Model);
            TextableDTO.Append(sb, SPLITCHAR, MakeID);
            TextableDTO.Append(sb, SPLITCHAR, Make);
            TextableDTO.Append(sb, SPLITCHAR, VIN);
            TextableDTO.Append(sb, SPLITCHAR, MachineName);
            TextableDTO.Append(sb, SPLITCHAR, EngineHours);
            TextableDTO.Append(sb, SPLITCHAR, CurrentHours);
            TextableDTO.Append(sb, SPLITCHAR, Description);
            TextableDTO.Append(sb, SPLITCHAR, ServiceDescription);
            TextableDTO.Append(sb, SPLITCHAR, ScheduleID);
            TextableDTO.Append(sb, SPLITCHAR, IntervalID);
            TextableDTO.Append(sb, SPLITCHAR, Recurring ? "1" : "0");
            TextableDTO.Append(sb, SPLITCHAR, Priority);
            TextableDTO.Append(sb, SPLITCHAR, ExpectedCost);
            TextableDTO.Append(sb, SPLITCHAR, AlertCount);
            if (RepeatedAlerts != null && RepeatedAlerts.Count > 0)
            {
                string repeatedalertsstr = string.Join(SPLITCHAR1.ToString(), RepeatedAlerts);
                TextableDTO.Append(sb, SPLITCHAR, repeatedalertsstr);
            }
            else
                TextableDTO.Append(sb, SPLITCHAR, "");
            TextableDTO.Append(sb, SPLITCHAR, OpenWorkOrderCount);
            TextableDTO.Append(sb, SPLITCHAR, PMType);
            TextableDTO.Append(sb, SPLITCHAR, AcknowledgedBy);
            TextableDTO.Append(sb, SPLITCHAR, AcknowledgedByName);
            TextableDTO.Append(sb, SPLITCHAR, AcknowledgedTime_UTC);
            TextableDTO.Append(sb, SPLITCHAR, AcknowledgedTime_UTCStr);
            TextableDTO.Append(sb, SPLITCHAR, AcknowledgedTime_Local);
            TextableDTO.Append(sb, SPLITCHAR, AcknowledgedTime_LocalStr);
            TextableDTO.Append(sb, SPLITCHAR, AcknowledgedComment);

            return sb.ToString();
        }
    }

    public class MachineInfoForAlert
    {
        private const char SPLITCHAR = (char)182;
        private const char SPLITCHAR1 = (char)180;
        public long MachineID { get; set; }
        public string VIN { get; set; }
        public string MachineName { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }

        private double _EngineHours;
        public double EngineHours
        {
            get
            {
                return _EngineHours;
            }
            set
            {
                value = value > 0 ? value : 0;
                _EngineHours = Math.Round(value, 2);
            }
        }
        public int DTCAlertCount { get; set; }
        public int PMAlertCount { get; set; }
        public int InspectAlertCount { get; set; }
        public int OpenWorkOrders { get; set; }
        public DateTime LatestAlertDateTime { get; set; }
        public string LatestAlertDateTimeStr { get { return LatestAlertDateTime == DateTime.MinValue ? "" : LatestAlertDateTime.ToString(); } }

        public List<AlertInfo> Alerts { get; } = new List<AlertInfo>();

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            TextableDTO.Append(sb, MachineID);
            TextableDTO.Append(sb, SPLITCHAR, VIN);
            TextableDTO.Append(sb, SPLITCHAR, MachineName);
            TextableDTO.Append(sb, SPLITCHAR, Make);
            TextableDTO.Append(sb, SPLITCHAR, Model);
            TextableDTO.Append(sb, SPLITCHAR, EngineHours);
            TextableDTO.Append(sb, SPLITCHAR, DTCAlertCount);
            TextableDTO.Append(sb, SPLITCHAR, PMAlertCount);
            TextableDTO.Append(sb, SPLITCHAR, InspectAlertCount);
            TextableDTO.Append(sb, SPLITCHAR, OpenWorkOrders);
            TextableDTO.Append(sb, SPLITCHAR, LatestAlertDateTime);
            TextableDTO.Append(sb, SPLITCHAR, LatestAlertDateTimeStr);
            if (Alerts != null && Alerts.Count > 0)
            {
                StringBuilder sb1 = new StringBuilder();
                foreach (AlertInfo ai in Alerts)
                {
                    if (sb1.Length > 0)
                        sb1.Append(SPLITCHAR1 + ai.ToString());
                    else
                        sb1.Append(ai.ToString());
                }
                TextableDTO.Append(sb, SPLITCHAR, sb1.ToString());
            }
            else
                TextableDTO.Append(sb, SPLITCHAR, "");

            return sb.ToString();
        }
    }

    public class AssetAlertInfo
    {
        public long ID { get; set; }
        public DateTime AlertTime { get; set; }
        public string AlertTimeStr { get { return AlertTime == DateTime.MinValue ? "" : AlertTime.ToString(); } }
        public DateTime AlertLocalTime { get; set; }
        public string AlertLocalTimeStr { get { return AlertLocalTime == DateTime.MinValue ? "" : AlertLocalTime.ToString(); } }
        public string AlertType { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string FormatDescription(string desc)
        {
            if (!string.IsNullOrEmpty(desc))
            {
                if (desc.IndexOf("\r\n") > 0)
                    desc = desc.Substring(0, desc.IndexOf("\r\n"));
                if (desc.IndexOf("\n") > 0)
                    desc = desc.Substring(0, desc.IndexOf("\n"));
            }
            return desc;
        }
        public long AssetID { get; set; }
        public string VIN { get; set; }
        public string AssetName { get; set; }
        public string ModelName { get; set; }
        public string MakeName { get; set; }
        public string AssetTypeName { get; set; }

        private double _EngineHours;
        public double EngineHours
        {
            get
            {
                return _EngineHours;
            }
            set
            {
                value = value > 0 ? value : 0;
                _EngineHours = Math.Round(value, 2);
            }
        }
        public bool Completed { get; set; }
        public DateTime? CompletedDate { get; set; }
        public string CompletedDateStr { get { return CompletedDate == null ? "" : CompletedDate.ToString(); } }

        public AssetAlertCategory Category { get; set; }
        public string ServiceDescription { get; set; }
    }
}
