using ReAct_PME.ViewModels;
using Services;
using System.IO;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using ToolsServices;

namespace ReAct_PME
{
    /// <summary>
    /// Logique d'interaction pour ListeMailsSend.xaml
    /// </summary>
    public partial class ListeEmailsSend : Window, IToastNotifier
    {
        #region Variables
        private GridViewColumnHeader? _lastClickedHeader = null;
        //private Services.ThreadStore store = new();
        private ListeEmailsSend_VM _VM;
        #endregion

        #region Constructeur
        public ListeEmailsSend()
        {
            InitializeComponent();
            
            _VM = new ListeEmailsSend_VM(this);
            this.DataContext = _VM;
        }
        #endregion

        #region Actions

        private async void CheckNewMails_Click(object sender, RoutedEventArgs e)
        {
            BtnCheckNewMails.IsEnabled = false;
            await Task.Delay(100); // Permet de laisser le temps au bouton de se désactiver visuellement avant de lancer le traitement
            await _VM.CheckForNewEmails();
            BtnCheckNewMails.IsEnabled = true;
        }

        private void CopyFollowUp_Click(object sender, RoutedEventArgs e)
        {
            if ((sender as Button)?.DataContext is ThreadItem thread && !string.IsNullOrEmpty(thread.FollowUpDraft))
            {
                Clipboard.SetText(thread.FollowUpDraft);
                //MessageBox.Show(thread.FollowUpDraft, "Relance copiée dans le presse-papiers !");
                var f = new DetailEmailSentRelance(thread);
                f.ShowDialog();
                _VM.SaveThreadItem(thread);
            }
        }

        private void ShowMessages_Click(object sender, RoutedEventArgs e)
        {
            if ((sender as Button)?.DataContext is ThreadItem thread)
            {
                _VM.SelectedItem = thread;
                var f = new DetailEmailSend(thread);
                f.ShowDialog();
            }
        }

        private void CloseSuject_Click(object sender, RoutedEventArgs e)
        {
            if ((sender as Button)?.DataContext is ThreadItem thread)
            {
                _VM.SelectedItem = thread;
                _VM.CloseConversation();
            }
        }


        private void ShowClosed_Click(object sender, RoutedEventArgs e)
        {
            _VM.IsShowClosedSubjects = !_VM.IsShowClosedSubjects;
            _VM.ShowConversation();

            //ChangeContentBtnShowConversations();
        }
        #endregion

        #region Tri des colonnes
        private void ColumnHeader_Click(object sender, RoutedEventArgs e)
        {
            if (_lastClickedHeader != null)
            {
                // Réinitialiser le style de l'en-tête précédent
                _lastClickedHeader.Style = (Style)Resources["DefaultColumnHeaderStyle"];
            }

            var clickedHeader = e.OriginalSource as GridViewColumnHeader;
            if (clickedHeader != null)
            {
                // Appliquer le style de la colonne cliquée
                clickedHeader.Style = (Style)Resources["ClickedColumnHeaderStyle"];
                _lastClickedHeader = clickedHeader;
            }
        }
        #endregion

        #region Fermeture fenêtre
        private void Fermer_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            LoggerService.LogInfo("ListeEmailsSend : fenêtre fermée");
            //_VM.Quit();
        }
        #endregion

        #region Implémentation de IToastNotifier
        public void ShowToast(string message, int durationMs = 10000)
        {
            Dispatcher.Invoke(() =>
            {
                ToastMessage.Text = message;
                ToastPopup.IsOpen = true;
            });

            Task.Delay(durationMs).ContinueWith(_ =>
            {
                Dispatcher.Invoke(() => ToastPopup.IsOpen = false);
            });
        }

        #endregion


    }
}
