using MailKit;
using System.Text.Json.Serialization;

namespace Services
{
    public class ThreadItemMini
    {
        public string Id { get; set; } = "";
        public string? FollowUpDraft { get; set; }
    }
    public class ThreadStore
    {
        public List<ThreadItem> Threads { get; set; } = new();
    }

    public class ThreadItem
    {
        #region Constructeurs
        //Necessaire pour la déserialisation en json
        public ThreadItem()
        {
            OverdueDays = 7;
        }

        //Necessaire pour passer le paramètre lors de la création d'objets
        public ThreadItem(int overdueDays=7)
        {
            OverdueDays = overdueDays;
        }
        #endregion

        #region Méthode privée
        private bool IsOverDueThread()
        {
            var t = this;
            if (t.LastFollowUpForMessageId == null || t.FollowUpDraft == null)
            {
                return false;
            }
            var m = t.Messages.Where(m => m.Id == LastFollowUpForMessageId).FirstOrDefault();
            if (m == null)
            {
                return false;
            }
            if (m.Date < DateTimeOffset.UtcNow.AddDays(-1 * OverdueDays))
                return true;
            else
                return false;
        }
        #endregion

        #region Variable
        [JsonIgnore]
        public int OverdueDays { get; set; } = 7;
        #endregion

        #region Propriétés
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string Subject { get; set; } = "";
        public List<MailMessageItem> Messages { get; set; } = new();
        public string? FollowUpDraft { get; set; }
        public string? LastFollowUpForMessageId { get; set; }
        public bool SubjectClosed { get; set; } = false;
        public DateTimeOffset DateMessage { get; set; }
        public DateTimeOffset DateLastMessage { get; set; }
        [JsonIgnore]
        public string DateMessageSTR { get => DateMessage.ToString("dd/MM/yyyy HH:mm"); }
        [JsonIgnore]
        public string DateLastMessageSTR { get => DateLastMessage.ToString("dd/MM/yyyy HH:mm"); }
        [JsonIgnore]
        public IEnumerable<MailMessageItem> MessagesSorted => Messages.OrderBy(m => m.Date.UtcDateTime);
        [JsonIgnore]
        public bool IsOverdue
        {
            get
            {
                return IsOverDueThread();
            }
        }
        #endregion
    }

    public class MailMessageItem
    {
        public string Id { get; set; } = "";
        public string InReplyTo { get; set; } = "";
        /*[JsonIgnore]
        public string UidString
        {
            get
            {
                return Uid.ToString();
            }
            set
            {
                if (value != "0")
                    Uid = UniqueId.Parse(value);
            }
        }*/
        public string Folder { get; set; } = "";
        public string Subject { get; set; } = "";
        public string From { get; set; } = "";
        public string To { get; set; } = "";
        public string Direction { get; set; } = "out"; // "in" ou "out"
        public DateTimeOffset Date { get; set; }
        public string Body { get; set; } = "";
        public bool RequiresResponse { get; set; }
        public bool CoversAllPoints { get; set; }
        //[JsonIgnore]
        //public UniqueId Uid { get; set; }
        [JsonIgnore]
        public string DateSTR { get => $"Le : {Date.DateTime.ToString("dd/MM/yyyy HH:mm")}"; }
        [JsonIgnore]
        public string FromSTR { get => $"De : {From}"; }
        [JsonIgnore]
        public string ToSTR { get => $"Pour : {To}"; }
        [JsonIgnore]
        public string FolderSTR { get => $"Dossier : {Folder}"; }
        [JsonIgnore]
        public string SubjectSTR { get => $"Sujet : {Subject}"; }
    }

}
