using ReAct_PME.ViewModels;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace ReAct_PME
{
    /// <summary>
    /// Logique d'interaction pour ParametresFooocus.xaml
    /// </summary>
    public partial class ParametresFooocus : Window
    {
        ParametresFooocus_VM _VM;

        public ParametresFooocus()
        {
            InitializeComponent();
            this.Message.Text = "Chargement des données, veuillez patienter";
            _VM = new();
            this.DataContext = _VM;
            this.Message.Text = "Données prêtes...";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (VerifData())
            {
                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;

                }
            }
            else 
            {
                Message.Foreground = Brushes.Red;
            }

        }

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

        private bool VerifData()
        {             
            bool bReturn = true;
            Message.Text = "";

            if (_VM.SelectedItem.guidance_scale < 1 || _VM.SelectedItem.guidance_scale > 30)
            {
                Message.Text += "L'influence du prompt (Guidance Scale) doit être comprise entre 1 et 30.\n";
                //_VM.SelectedItem.GuidanceScale = 7.5;
                bReturn = false;
            }
            if (_VM.SelectedItem.refiner_switch < 0 || _VM.SelectedItem.refiner_switch > 1)
            {
                Message.Text += "Le Refiner Switch doit être compris entre 0 et 1.\n";
                //_VM.SelectedItem.RefinerSwitch = 0.5;
                bReturn = false;
            }
            if (_VM.SelectedItem.sharpness < 0 || _VM.SelectedItem.sharpness > 30)
            {
                Message.Text += "Le Sharpness (netteté appliquée à l’image finale) doit être compris entre 0 et 30. (Valeurs : 0.0 (douce) à 30.0 (très nette))\n";
                //_VM.SelectedItem.Sharpness= 2.0;
                bReturn = false;
            }
            if (string.IsNullOrWhiteSpace(_VM.SelectedItem.performance_selection) ||
                (_VM.SelectedItem.performance_selection != "Speed" &&
                 _VM.SelectedItem.performance_selection != "Quality" &&
                 _VM.SelectedItem.performance_selection != "Extreme Speed"))
            {
                Message.Text += "Le mode de performance doit être 'Speed', 'Quality' ou 'Extreme Speed'.\n";
                //_VM.SelectedItem.PerformanceSelection = "Speed";
                bReturn = false;
            }

            return bReturn;
        }

        private void ButtonAddStyle_Click(object sender, RoutedEventArgs e)
        {
            if (_VM.ListeSelectedsStylesFooocus != null && LstBoxStyles != null && LstBoxStyles.SelectedValue != null)
                _VM.ListeSelectedsStylesFooocus.Add(LstBoxStyles!.SelectedValue!.ToString());

        }

        private void ButtonRemoveStyle_Click(object sender, RoutedEventArgs e)
        {
            if(_VM.ListeSelectedsStylesFooocus != null && LstBoxSelectedsStyles != null && LstBoxSelectedsStyles.SelectedValue != null)
                _VM.ListeSelectedsStylesFooocus.Remove(LstBoxSelectedsStyles!.SelectedValue!.ToString());
        }

        private void TxtSeekStyle_TextChanged(object sender, TextChangedEventArgs e)
        {
            _VM.FilterStyles(TxtSeekStyle.Text);
        }
    }
}
