using ReAct_PME.WPF_FrameWork;
using Services;
using Services.ReActAgent;
using System.Collections.ObjectModel;

namespace ReAct_PME.ViewModels
{
    public class RechercheCV_VM : BaseViewModel 
    {
        #region Variables
        private readonly IToastNotifier _Notifier;
        #endregion

        #region Propriétés
        private string _FicheMission { get; set; } = "D:\\_RechercheCV\\Offre d'emploi Consultant Senior Full stack.pdf";
        public string FicheMission
        {
            get
            {
                return _FicheMission;
            }
            set
            {
                _FicheMission = value;
                OnPropertyChanged(nameof(FicheMission));
            }
        }

        private string _PathCV = "D:\\_RechercheCV\\CVs";
        public string PathCV {
            get 
            { 
                return _PathCV;
            }
            set 
            {
                _PathCV = value;
                OnPropertyChanged(nameof(PathCV));
            }   
        }

        private string _RechercheString = "";
        public string RechercheString
        {
            get => _RechercheString;
            set
            {
                _RechercheString = value;
                OnPropertyChanged();
            }
        }

        private bool _IsGenererXML = false;
        public bool IsGenererXML
        {
            get
            {
                return _IsGenererXML;
            }
            set
            {
                _IsGenererXML = value;
                OnPropertyChanged(nameof(IsGenererXML));
            }
        }

        public ObservableCollection<ReponseRechercheCV> ListeItems { get; set; } = new();
        
        public ReponseRechercheCV SelectedItem { get; set; } = new();
        #endregion

        #region Constructeurs
        public RechercheCV_VM(IToastNotifier notifier)
        {
            _Notifier = notifier;
            LoadParam();
            // Initialisation si nécessaire
            LoadRechercheCVAsync().ConfigureAwait(false);
        }

        public RechercheCV_VM(ReponseRechercheCV selectedItem, IToastNotifier notifier)
        {
            _Notifier = notifier;
            // Initialisation si nécessaire
            SelectedItem = selectedItem;
        }
        #endregion

        #region Méthodes publiques
        public async Task SearchCV()
        {
            var b = await ReActAgent.IsOllamaActif(true);
            if (!b)
            {
                _Notifier.ShowToast("Ollama n'est pas démarré. Veuillez lancer Ollama Desktop ou le service Ollama.", 5000);
                return;
            }

            ListeItems = new();
            OnPropertyChanged(nameof(ListeItems));

            SaveParam();

            var retours = await RechercheCVService.RechercherCV(FicheMission, PathCV, IsGenererXML);

            if (retours != null)
            {
                retours = retours.OrderByDescending(e => e.Note).ToList();
                ListeItems = new ObservableCollection<ReponseRechercheCV>(retours);
                OnPropertyChanged(nameof(ListeItems));
                await RechercheCVService.SauvegarderRechercheAsync(ListeItems).ConfigureAwait(false);
            }
        }

        public async Task<string> SeekByKeyWord()
        {
            var reponse = await RechercheCVService.SeekByKeyWord(RechercheString);
            return reponse;
        }
        #endregion

        #region Méthodes privées
        private void LoadParam()
        {
            try
            {
                var (FicheMission, PathCV) = RechercheCVService.LoadParametres();

                OnPropertyChanged(nameof(FicheMission));
                OnPropertyChanged(nameof(PathCV));

            }
            catch
            {
                return;
            }
        }

        private bool SaveParam()
        {
            try
            {
                var b = RechercheCVService.SaveParametres(FicheMission, PathCV);

                return b;
            }
            catch
            {
                return false;
            }
        }

        private async Task LoadRechercheCVAsync()
        {
            ListeItems = await RechercheCVService.ChargerCVDepuisJsonAsync(ListeItems!);
            OnPropertyChanged(nameof(ListeItems));
        }
        #endregion

    }
}
