using ReAct_PME.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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 ParametresLogs.xaml
    /// </summary>
    public partial class ParametresLogs : Window
    {
        ParametresLogs_VM _VM;
        public ParametresLogs()
        {
            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 des chiffres

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

        // Bloque aussi le collage de texte non numérique
        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)
            {
                // vérifie le presse-papier avant de coller
                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 < 0 || value > 3)
                {
                    AlerterDepassement();
                }
            }
            else if (!string.IsNullOrWhiteSpace(IntTextBox.Text))
            {
                AlerterDepassement();
            }
        }

        private void AlerterDepassement()
        {
            System.Media.SystemSounds.Exclamation.Play(); // ou Beep, Hand, etc.
            MessageBox.Show($"⚠️ La valeur doit être compris entre 0 et 3.", "Limite dépassée", MessageBoxButton.OK, MessageBoxImage.Warning);
            IntTextBox.Text = "0";
        }

    }
}
