using DocumentFormat.OpenXml.InkML;
using DocumentFormat.OpenXml.Spreadsheet;
using Services;
using System.Collections.ObjectModel;


namespace ReAct_PME.Application
{
    public class ListeMailsService : IListeMailsService
    {
        private IEnumerable<EmailMessage> lstIE;
        private IEnumerable<ThreadItem> lstSendIE;

        public async Task<IEnumerable<EmailMessage>> GetAll_Async()
        {
            await Task.Delay(10); // Simule une opération asynchrone
            var lstOC = Services.EmailService.ChargerEmailsDepuisJson();
            lstIE = lstOC;
            return lstIE;
        }

        public async Task<EmailMessage> GetById_Async(string id)
        {
            EmailMessage? item = null;

            await Task.Delay(10); // Simule une opération asynchrone
            if (lstIE == null)
            {
                await GetAll_Async();
            }
            if (lstIE != null)
            {
                item = lstIE.Where(e => e.UidString == id).FirstOrDefault();
            }
            return item ?? new EmailMessage();
        }

        public async Task<IEnumerable<ThreadItem>> GetSendAll_Async(bool IsShowClosedSubjects=false)
        {
            await Task.Delay(10); // Simule une opération asynchrone
            var lstOC = Services.EmailSendService.ChargerDonnees();
            lstSendIE = new ObservableCollection<ThreadItem>(lstOC.Threads.Where(e => e.SubjectClosed == IsShowClosedSubjects).OrderByDescending(e => e.DateMessage).ToList());

            return lstSendIE;
        }

        public async Task<ThreadItem> GetByIdSend_Async(string id)
        {
            ThreadItem? item = null;

            await Task.Delay(10); // Simule une opération asynchrone
            if (lstSendIE == null)
            {
                await GetSendAll_Async();
            }
            if (lstSendIE != null)
            {
                item = lstSendIE.Where(e => e.Id == id).FirstOrDefault();
            }
            return item ?? new ThreadItem();
        }

        public async Task<string> GenerateAgain(string context, string query)
        {
            var (isOk, sRetourGenere) = await Services.EmailService.GenererMailAsync(context, query);
            return sRetourGenere;
        }

        public async Task<bool> SaveItem(Services.EmailMessage item)
        {
            var isOk = await Services.EmailService.SauvegarderReponseEmails(item);
            return isOk;
        }

        public async Task<bool> SaveSendItem(Services.ThreadItemMini item)
        {
            var isOk = await Services.EmailSendService.SaveSendRelanceItem(item);
            return isOk;
        }

        public async Task<bool> MarquerLu(Services.EmailMessage item)
        {
            var isOk = await Services.EmailService.MaquerLu(item.Uid);
            return isOk;
        }

        public async Task<bool> ArchiverRestaurerItem(Services.ThreadItemMini item)
        {
            var isOk = await Services.EmailSendService.ArchiverRestaurerItem(item);
            return isOk;
        }

        public async Task<bool> EnvoyerMailAsync(MessageToSend data)
        {
            var dest = data.DestinataireA;
            var destCc = data.DestinataireCC;
            var sujet = data.Object;
            var message = data.Body;
            var isOk = await Services.EmailService.EnvoyerMailAsync(null, dest, sujet, message, false, "", destCc); ;
            return isOk;
        }


    }
}
