using System.Drawing;
using System.Windows;

namespace ReAct_PME
{
    /// <summary>
    /// Logique d'interaction pour CapturePhoto.xaml
    /// </summary>
    public partial class CapturePhoto : Window
    {
        //private FilterInfoCollection? _videoDevices;
        //private VideoCaptureDevice? _videoSource;
        private Bitmap? _currentFrame;

        public CapturePhoto()
        {
            InitializeComponent();
        }






        private void CaptureButton_Click(object sender, RoutedEventArgs e)
        {
  
        }


        private void Training_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
/*
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;

namespace ReAct_PME
{
    /// <summary>
    /// Logique d'interaction pour CapturePhoto.xaml
    /// </summary>
    public partial class CapturePhoto : Window
    {
        //private FilterInfoCollection? _videoDevices;
        //private VideoCaptureDevice? _videoSource;
        private Bitmap? _currentFrame;

        public CapturePhoto()
        {
            InitializeComponent();
            Loaded += CapturePhoto_Loaded;
            Closed += CapturePhoto_Closed;

            var parametresMail = Services.EmailService.LoadParametres();
            if (parametresMail != null && !string.IsNullOrWhiteSpace(parametresMail.UserNomPrenom))
            {
                TxtNom.Text = parametresMail.UserNomPrenom;
            }
        }

        private void CapturePhoto_Loaded(object sender, RoutedEventArgs e)
        {
            // Liste des webcams disponibles
            _videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (_videoDevices.Count == 0)
            {
                LblMessage.Content = "Aucune webcam détectée";
                LblMessage.Foreground = System.Windows.Media.Brushes.Red;
                return;
            }

            // Sélectionne la première webcam
            _videoSource = new VideoCaptureDevice(_videoDevices[0].MonikerString);
            _videoSource.NewFrame += Video_NewFrame;
            _videoSource.Start();
        }

        private void Video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // Clone pour éviter les accès concurrents
            _currentFrame = (Bitmap)eventArgs.Frame.Clone();

            // Affiche dans le contrôle Image (via un dispatcher)
            Dispatcher.Invoke(() =>
            {
                CameraPreview.Source = BitmapToImageSource(_currentFrame);
            });
        }

        private BitmapImage BitmapToImageSource(Bitmap bitmap)
        {
            using var memory = new MemoryStream();
            bitmap.Save(memory, ImageFormat.Bmp);
            memory.Position = 0;

            var bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = memory;
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapImage.EndInit();
            return bitmapImage;
        }

        private void CaptureButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_currentFrame == null)
                    return;


                var parametres = FaceRecognition.FaceRecognitionService.LoadParametres();
                if (parametres == null || string.IsNullOrWhiteSpace(parametres.Path_TrainSet))
                {
                    LblMessage.Content = "Le chemin du jeu d'entraînement n'est pas défini dans les paramètres de reconnaissance faciale.";
                    LblMessage.Foreground = System.Windows.Media.Brushes.Red;
                    return;
                }

                var txtNom = TxtNom.Text.Trim().Replace(" ", "_");
                if (string.IsNullOrWhiteSpace(txtNom))
                {
                    LblMessage.Content = "Veuillez entrer un nom avant de capturer la photo.";
                    LblMessage.Foreground = System.Windows.Media.Brushes.Red;
                    return;
                }

                string directory = Path.Combine(parametres.Path_TrainSet, txtNom);
                if (!System.IO.Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                string filePath = Path.Combine(directory, $"{txtNom}_{DateTime.Now:yyyyMMdd_HHmmss}.jpg");
                _currentFrame.Save(filePath, ImageFormat.Jpeg);

                var o = new FaceRecognition.FaceRecognitionService();
                o.CapturePhoto(filePath);

                LblMessage.Content = $"Photo enregistrée : {filePath}";
                LblMessage.Foreground = System.Windows.Media.Brushes.Green;

            }
            catch (Exception ex)
            {
                LblMessage.Content = $"Erreur lors de la capture de la photo : {ex.Message}";
                LblMessage.Foreground = System.Windows.Media.Brushes.Red;
            }
        }
        private void CapturePhoto_Closed(object sender, EventArgs e)
        {
            if (_videoSource != null && _videoSource.IsRunning)
            {
                _videoSource.SignalToStop();
                //_videoSource.WaitForStop(); // bloque jusqu’à l’arrêt complet
            }
        }

        private void Training_Click(object sender, RoutedEventArgs e)
        {
            var o = new FaceRecognition.FaceRecognitionService(true);
            o.Training(false);
            LblMessage.Content = $"Entrainement terminé";
            LblMessage.Foreground = System.Windows.Media.Brushes.Green;
        }
    }
}
*/