using ReAct_PME.ViewModels;
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 ParametresCompteMail.xaml
    /// </summary>
    public partial class ParametresCompteMail : Window
    {
        private ParametresCompteMail_VM _VM;
        public ParametresCompteMail()
        {
            InitializeComponent();
            _VM= new();
            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 static readonly Regex _regex = new Regex("[^0-9]+"); // tout sauf chiffres

        private void NumericOnly_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = _regex.IsMatch(e.Text);
        }

        private void NumericOnly_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
                e.Handled = true;

            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control && e.Key == Key.V)
            {
                string clipboard = Clipboard.GetText();
                if (_regex.IsMatch(clipboard))
                    e.Handled = true;
            }
        }

        private void IntTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (int.TryParse(IntTextBox.Text, out int value))
            {
                if (value > int.MaxValue)
                {
                    AlerterDepassement();
                }
            }
            else if (!string.IsNullOrWhiteSpace(IntTextBox.Text))
            {
                AlerterDepassement();
            }
        }

        private void IntTextBox2_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (int.TryParse(IntTextBox2.Text, out int value))
            {
                if (value > int.MaxValue)
                {
                    AlerterDepassement();
                }
            }
            else if (!string.IsNullOrWhiteSpace(IntTextBox2.Text))
            {
                AlerterDepassement();
            }
        }

        private void IntTextBox3_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (int.TryParse(IntTextBox3.Text, out int value))
            {
                if (value > int.MaxValue)
                {
                    AlerterDepassement();
                }
            }
            else if (!string.IsNullOrWhiteSpace(IntTextBox3.Text))
            {
                AlerterDepassement();
            }
        }

        private void IntTextBox4_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (int.TryParse(IntTextBox4.Text, out int value))
            {
                if (value > int.MaxValue)
                {
                    AlerterDepassement();
                }
            }
            else if (!string.IsNullOrWhiteSpace(IntTextBox4.Text))
            {
                AlerterDepassement();
            }
        }

        private void AlerterDepassement()
        {
            System.Media.SystemSounds.Exclamation.Play(); // ou Beep, Hand, etc.
            MessageBox.Show($"⚠️ La valeur ne peut pas dépasser {int.MaxValue}.", "Limite dépassée", MessageBoxButton.OK, MessageBoxImage.Warning);
            IntTextBox.Text = int.MaxValue.ToString();
        }

    }
}
