using DocumentFormat.OpenXml.InkML;
using ReAct_PME.WPF_FrameWork;
using Services;
using ToolsServices;

namespace ReAct_PME.ViewModels
{
    public class DetailEmailSentRelance_VM : BaseViewModel
    {
        public ThreadItem SelectedItem { get; set; } = new();
        public MailMessageItem? MessageOrigineRelance { get; set; } = new();

        public DetailEmailSentRelance_VM(ThreadItem selectedItem)
        {
            SelectedItem = selectedItem;
            MessageOrigineRelance = SelectedItem.Messages.Where(m => m.Id == selectedItem.LastFollowUpForMessageId).FirstOrDefault();
        }

        public async Task<bool> GenerateAgainReponse()
        {
            await Task.Delay(1);
            var context = new List<ContextGeneration>();
            context.Add(new ContextGeneration { Name = "Type de mail", Description = "mail de relance" });
            context.Add(new ContextGeneration{ Name="Mail à l'origine de la relance", Description= MessageOrigineRelance!.Body });
            var f = new _ModalGenerateAgain(SelectedItem.FollowUpDraft!, context);
            f.ShowDialog();
            if (f.IsOk)
            {
                SelectedItem.FollowUpDraft = f.ReponseNew;
                OnPropertyChanged(nameof(SelectedItem));
                return true;
            }
            return false;
        }

        public async Task<bool> EnvoyerReponse()
        {
            try
            {
                string messageBody = $"""
                    {SelectedItem!.FollowUpDraft}

                    ....

                    {MessageOrigineRelance!.Body}
                    """;

                var dest = MessageOrigineRelance!.To;
                //var dest2 = "guillaume.topenot@gmail.com"; // "guillaume.topenot@gmail.com; guillaume.topenot@allin-it.fr"; //MessageOrigineRelance!.To;   

                var b = await Services.EmailService.EnvoyerMailAsync(
                    null,
                    dest,
                    SelectedItem!.Subject,
                    messageBody,
                    true,
                    MessageOrigineRelance.Id
                );

                return b;
            }
            catch (Exception ex)
            {
                LoggerService.LogError(ex.ToString());
                return false;
            }
        }
    }
}
