using ReAct_PME.Helpers;
using ReAct_PME.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ReAct_PME
{
    /// <summary>
    /// Logique d'interaction pour ParametresFactures.xaml
    /// </summary>
    public partial class ParametresFactures : Window, IToastNotifier
    {
        private ParametresFactures_VM _VM;

        public ParametresFactures(bool isTraitement)
        {
            InitializeComponent();
            if (isTraitement)
            {
                this.Title = "Traitement des factures";
                Btn_Traitement.Visibility = Visibility.Visible;
                ChkApiExterne.Visibility = Visibility.Visible;
                Btn_Save.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.Title = "Paramètres du traitement des factures";
                Btn_Traitement.Visibility = Visibility.Collapsed;
                ChkApiExterne.Visibility = Visibility.Collapsed;
                Btn_Save.Visibility = Visibility.Visible;
            }
            _VM = new(this);
            this.DataContext = _VM;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var b = _VM.SaveData();
            if (b)
            {
                Message.Text = "Données sauvegardées...";
                Message.Foreground = Brushes.Green;
            }
            else
            {
                Message.Text = "Erreur dans la sauvegarde des données...";
                Message.Foreground = Brushes.Red;

            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Btn_Traitement.IsEnabled = false;
            Message.Text = "Traitement en cours...";
            Message.Foreground = Brushes.Black;

            var (b,s) = await _VM.Traitement();
            if (b && s == "")
            {
                Message.Text = "Traitement terminé avec succès...";
                Message.Foreground = Brushes.Green;
            }
            else if(b && s != "")
            {
                Message.Text = $"Des erreur dans le traitement";
                Message.Foreground = Brushes.Orange;
                NotificationManager.ShowNotification($"Les erreurs recontrées : \n{s}", "Traitement des factures");
            }
            else if(!b && s != "")
            {
                Message.Text = "Le traitement a échoué...";
                Message.Foreground = Brushes.Red;
                NotificationManager.ShowNotification($"Les erreurs recontrées : \n{s}", "Traitement des factures");
            }
            else
            {
                Message.Text = "Le traitement a échoué...";
                Message.Foreground = Brushes.Red;
            }
            Btn_Traitement.IsEnabled = true;
        }

        #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
    }
}
