using ReAct_PME.ViewModels;
using System.Windows;

namespace ReAct_PME
{
    /// <summary>
    /// Logique d'interaction pour ResumeAudio.xaml...
    /// </summary>
    public partial class ResumeAudio : Window
    {
        private ResumeAudio_VM _VM = new();

        public ResumeAudio()
        {
            InitializeComponent();
            this.DataContext = _VM;
        }

        private async void BtnGoTrt_Click(object sender, RoutedEventArgs e)
        {
            BtnGoTrt.IsEnabled = false;
            BtnGoResume.IsEnabled = false;

            TxtTranscribe.Text = "Transcription à venir";
            TxtReponse.Text = "Résumé à venir";

            await Task.Delay(100);
            var (b1, s1) = await _VM.GoTrt_Part1();
            if(b1)
            {
                TxtTranscribe.Text = s1;
                await Task.Delay(1000);
                var (b2, s2) = await _VM.GoTrt_Part2(s1);
                if(b2)
                {
                    TxtReponse.Text = s2;
                }
                else
                {
                    MessageBox.Show(s2, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            BtnGoTrt.IsEnabled = true;
            BtnGoResume.IsEnabled = true;

        }

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

        private async void BtnGoResume_Click(object sender, RoutedEventArgs e)
        {
            BtnGoTrt.IsEnabled = false;
            BtnGoResume.IsEnabled = false;
            await Task.Delay(100);

            if (TxtTranscribe.Text == "" || TxtTranscribe.Text == "Transcription à venir")
            {
                TxtReponse.Text = "Merci de fournir un texte à résumer :)";
                return;
            }

            //TxtTranscribe.Text = "Transcription à venir";
            TxtReponse.Text = "Résumé à venir";

            var (b, s2) = await _VM.GoTrt_Part2(TxtTranscribe.Text);

            if (b)
            {
                //TxtTranscribe.Text = s1;
                TxtReponse.Text = s2;
            }
            else
            {
                MessageBox.Show(s2, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            BtnGoTrt.IsEnabled = true;
            BtnGoResume.IsEnabled = true;
        }

        private void BtnFindFile_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog
            {
                Filter = "Audio et Vidéo (*.mp3;*.wav;*.mp4;*.mkv)|*.mp3;*.wav;*.mp4;*.mkv|Audio (*.mp3;*.wav)|*.mp3;*.wav|Vidéo (*.mp4;*.mkv)|*.mp4;*.mkv",
                Title = "Sélectionner un fichier audio ou vidéo"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                _VM.FullFileName = openFileDialog.FileName;
                this.TxtFullFileName.Text = _VM.FullFileName;

            }
        }
    }
}
