using Foresight.Fleet.Services.AssetHealth.WorkOrder;
using Foresight.Fleet.Services.Customer;
using Foresight.ServiceModel;
using IronIntel.Contractor.Machines;
using IronIntel.Contractor.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IronIntel.Contractor.Maintenance
{
    public class WorkOrderInfoClient : WorkOrderInfo
    {
        public string AssetName { get; set; }
        public string VIN { get; set; }
        public string MakeName { get; set; }
        public string ModelName { get; set; }
        public string TypeName { get; set; }
        public string DueDateStr { get { return DueDate == null ? "" : DueDate.Value.ToShortDateString(); } }
        public string CompleteDateStr { get { return CompleteDate == null ? "" : CompleteDate.Value.ToShortDateString(); } }
        public string NextFollowUpDateStr { get { return NextFollowUpDate == null ? "" : NextFollowUpDate.Value.ToShortDateString(); } }
        public string PartsExpectedDateStr { get { return PartsExpectedDate == null ? "" : PartsExpectedDate.Value.ToShortDateString(); } }
        public string LastLaborDateStr { get { return LastLaborDate == null ? "" : LastLaborDate.Value.ToShortDateString(); } }
        public ContactInfoClient[] ContactsClient { get; set; }
        public WorkOrderFollowerInfo[] Followers { get; set; }
    }

    public class ContactInfoClient : ContactInfo
    {
        public string ContactPreferenceStr { get; set; }
        public int SaveToCustomer { get; set; }
    }

    public class WorkOrderListItemClient : WorkOrderListItem
    {
        const char SPLITCHAR = (char)170;
        const char SPLIT_CHAR182 = (char)182;
        const char SPLIT_CHAR183 = (char)183;
        public string DueDateStr { get { return DueDate == null ? "" : DueDate.Value.ToShortDateString(); } }
        public string CompleteDateStr { get { return CompleteDate == null ? "" : CompleteDate.Value.ToShortDateString(); } }
        public string NextFollowUpDateStr { get { return NextFollowUpDate == null ? "" : NextFollowUpDate.Value.ToShortDateString(); } }
        public string CreateDateStr { get { return CreateDate == null ? "" : CreateDate.Value.ToShortDateString(); } }
        public string CreationDateStr { get { return CreationDate == null ? "" : CreationDate.Value.ToShortDateString(); } }
        public string LastCommunicationDateStr { get { return (LastCommunicationDate == null || LastCommunicationDate == DateTime.MinValue) ? "" : LastCommunicationDate.Value.ToShortDateString(); } }
        public string LastInternalCommunicationDateStr { get { return (LastInternalCommunicationDate == null || LastInternalCommunicationDate == DateTime.MinValue) ? "" : LastInternalCommunicationDate.Value.ToShortDateString(); } }
        public string PartsExpectedDateStr { get { return PartsExpectedDate == null ? "" : PartsExpectedDate.Value.ToShortDateString(); } }
        public string LastLaborDateStr { get { return LastLaborDate == null ? "" : LastLaborDate.Value.ToShortDateString(); } }

        public string MaintenanceID { get; set; }
        public WorkOrderStatus[] WorkOrderStatus { get; set; }
        public UserInfo[] AssignedToUsers { get; set; }
        public DepartmentInfo[] Departments { get; set; }
        public CustomerLocation[] Locations { get; set; }
        public StringKeyValue[] Salespersons { get; set; }
        public string ContactsStr
        {
            get
            {
                var rst = "";
                if (Contacts != null && Contacts.Count > 0)
                {
                    for (var i = 0; i < Contacts.Count; i++)
                    {
                        var contact = Contacts[i];
                        var ptext = contact.Name;
                        if ((int)contact.ContactPreference == 0)
                        {
                            ptext += " – T";
                            if (contact.MobilePhoneDisplayText != "")
                                ptext += " – " + contact.MobilePhoneDisplayText;
                        }
                        else if ((int)contact.ContactPreference == 1)
                        {
                            if (contact.Email != "")
                                ptext += " - " + contact.Email;
                        }
                        else if ((int)contact.ContactPreference == 2)
                        {
                            ptext += " – P";
                            if (contact.MobilePhoneDisplayText != "")
                                ptext += " – " + contact.MobilePhoneDisplayText;
                        }

                        if (string.IsNullOrEmpty(rst))
                            rst += ptext;
                        else
                            rst += ("\n" + ptext);
                    }
                }
                return rst;
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(base.ToString());
            sb.Append(SPLITCHAR + DueDateStr);
            sb.Append(SPLITCHAR + CompleteDateStr);
            sb.Append(SPLITCHAR + NextFollowUpDateStr);
            sb.Append(SPLITCHAR + CreateDateStr);
            sb.Append(SPLITCHAR + CreationDateStr);
            sb.Append(SPLITCHAR + LastCommunicationDateStr);
            sb.Append(SPLITCHAR + LastInternalCommunicationDateStr);
            sb.Append(SPLITCHAR + PartsExpectedDateStr);
            sb.Append(SPLITCHAR + LastLaborDateStr);
            sb.Append(SPLITCHAR + MaintenanceID);
            sb.Append(SPLITCHAR + ((WorkOrderStatus != null && WorkOrderStatus.Length > 0) ? WorkOrderStatus[0].ToString() : ""));
            if (AssignedToUsers != null && AssignedToUsers.Length > 0)
            {
                StringBuilder sb1 = new StringBuilder();
                foreach (UserInfo user in AssignedToUsers)
                {
                    string str = user.IID + SPLIT_CHAR183 + user.DisplayName;
                    if (sb1.Length == 0)
                        sb1.Append(str);
                    else
                        sb1.Append(SPLIT_CHAR182 + str);
                }
                sb.Append(SPLITCHAR + sb1.ToString());
            }
            else
            {
                sb.Append(SPLITCHAR + "");
            }

            if (Departments != null && Departments.Length > 0)
            {
                StringBuilder sb1 = new StringBuilder();
                foreach (DepartmentInfo dept in Departments)
                {
                    string str = dept.Id.ToString() + SPLIT_CHAR183 + dept.Name;
                    if (sb1.Length == 0)
                        sb1.Append(str);
                    else
                        sb1.Append(SPLIT_CHAR182 + str);
                }
                sb.Append(SPLITCHAR + sb1.ToString());
            }
            else
            {
                sb.Append(SPLITCHAR + "");
            }

            if (Locations != null && Locations.Length > 0)
            {
                StringBuilder sb1 = new StringBuilder();
                foreach (CustomerLocation loc in Locations)
                {
                    string str = loc.ID.ToString() + SPLIT_CHAR183 + loc.Name;
                    if (sb1.Length == 0)
                        sb1.Append(str);
                    else
                        sb1.Append(SPLIT_CHAR182 + str);
                }
                sb.Append(SPLITCHAR + sb1.ToString());
            }
            else
            {
                sb.Append(SPLITCHAR + "");
            }

            if (Salespersons != null && Salespersons.Length > 0)
            {
                StringBuilder sb1 = new StringBuilder();
                foreach (StringKeyValue sale in Salespersons)
                {
                    string str = sale.Key + SPLIT_CHAR183 + sale.Value;
                    if (sb1.Length == 0)
                        sb1.Append(str);
                    else
                        sb1.Append(SPLIT_CHAR182 + str);
                }
                sb.Append(SPLITCHAR + sb1.ToString());
            }
            else
            {
                sb.Append(SPLITCHAR + "");
            }
            sb.Append(SPLITCHAR + ContactsStr);
            sb.Append(SPLITCHAR + (Completed ? "1" : "0"));

            return sb.ToString();
        }
    }

    public class TextMessageClient : TextMessage
    {
        public bool StatusIncorrect
        {//6219, 2022-09-20 17:50:00 以前的状态可能存在问题，界面上不显示状态
            get
            {
                return SystemParams.CustomerDetail.CustomerTimeToUtc(Time) < DateTime.Parse("2022-09-20 17:50:00");
            }
        }

        public string FormatSender
        {
            get
            {
                var rst = "";
                if (IsReply)
                {
                    if (Helper.IsEmail(Sender) || !Helper.IsNumber(Sender))
                        rst = Sender;
                    else
                        rst = Foresight.Standard.PhoneNumber.FormatPhoneNumber(Sender);
                }
                return rst;
            }
        }

        public string[] OriPhoneNumbers
        {
            get
            {
                List<string> ls = new List<string>();
                if (Participator != null && Participator.Count > 0)
                {
                    foreach (var p in Participator)
                    {
                        if (Helper.IsEmail(p.CustomerNumber))
                            ls.Add(p.CustomerNumber);
                        else
                        {
                            p.CustomerNumber = Foresight.Standard.PhoneNumber.FormatPhoneNumber(p.CustomerNumber);
                            ls.Add(Foresight.Standard.PhoneNumber.PreparePhonenumber(p.CustomerNumber));
                        }
                    }
                }
                return ls.ToArray();
            }
        }
        public string TimeStr { get { return Time == DateTime.MinValue ? "" : Time.ToString("M/d/yyyy h:mm tt"); } }
    }
}
