using ReAct_PME.WPF_FrameWork;
using Services;
using System.Text;
using ToolsServices;
using Services.ReActAgent;

namespace ReAct_PME.ViewModels
{
    public class GenererCV_VM : BaseViewModel
    {
        #region Variables
        private string NomFichierParams = FichiersInternesService.ParamsGenerationCV;// "ParametresGenerationCV.txt";
        private readonly IToastNotifier _Notifier;
        #endregion

        #region Propriétés
        private string _FullnameCV = "D:\\_RechercheCV\\CVs\\GTO_CV_PM_IA.pdf";
        public string FullnameCV
        {
            get
            {
                return _FullnameCV;
            }
            set
            {
                _FullnameCV = value;
                OnPropertyChanged(nameof(FullnameCV));
            }
        }

        private string _FullnameTemplateCV = "D:\\_RechercheCV\\modeles\\modele_cv.docx";
        public string FullnameTemplateCV
        {
            get
            {
                return _FullnameTemplateCV;
            }
            set
            {
                _FullnameTemplateCV = value;
                OnPropertyChanged(nameof(FullnameTemplateCV));
            }
        }

        private string _FullPathOutput = "D:\\_RechercheCV\\output";
        public string FullPathOutput
        {
            get
            {
                return _FullPathOutput;
            }
            set
            {
                _FullPathOutput = value;
                OnPropertyChanged(nameof(FullPathOutput));
            }
        }

        private string _TitreCV = "Expert IA";
        public string TitreCV
        {
            get
            {
                return _TitreCV;
            }
            set
            {
                _TitreCV = value;
                OnPropertyChanged(nameof(TitreCV));
            }
        }

        private int _NbrCompetencesMax = 5;
        public int NbrCompetencesMax
        {
            get
            {
                return _NbrCompetencesMax;
            }
            set
            {
                _NbrCompetencesMax = value;
                OnPropertyChanged(nameof(NbrCompetencesMax));
            }
        }

        private bool _IsCVAnonyme = true;
        public bool IsCVAnonyme
        {
            get
            {
                return _IsCVAnonyme;
            }
            set
            {
                _IsCVAnonyme = value;
                OnPropertyChanged(nameof(IsCVAnonyme));
            }
        }
        
        private bool _IsUsingIA = true;
        public bool IsUsingIA
        {
            get
            {
                return _IsUsingIA;
            }
            set
            {
                _IsUsingIA = value;
                OnPropertyChanged(nameof(IsUsingIA));
            }
        }
        #endregion

        #region Constructeur
        public GenererCV_VM(IToastNotifier notifier)
        {
            _Notifier = notifier;
            LoadParam();

        }
        #endregion

        #region Méthodes publiques
        public async Task<bool> GenerateCV()
        {
            SaveParam();
            if(IsUsingIA)
            {
                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 false;
                }
            }
            var b1 = await GenererCVService.GenerateOneCV(FullnameCV, FullnameTemplateCV, FullPathOutput,TitreCV,5, IsCVAnonyme, IsUsingIA);
            return b1;
        }
        #endregion

        #region Méthodes privées
        private void LoadParam()
        {
            try
            {
                if (System.IO.File.Exists(NomFichierParams))
                {

                    string[] lignes = System.IO.File.ReadAllLines(NomFichierParams);

                    if (lignes.Length > 0)
                        FullnameCV = lignes[0];

                    if (lignes.Length > 1)
                        FullnameTemplateCV = lignes[1];

                    if (lignes.Length > 2)
                        FullPathOutput = lignes[2];

                    if (lignes.Length > 3)
                        TitreCV = lignes[3];

                    if (lignes.Length > 4)
                        NbrCompetencesMax = int.Parse(lignes[4]);

                    if (lignes.Length > 5)
                        IsCVAnonyme = bool.Parse(lignes[5]);

                    if (lignes.Length > 6)
                        IsUsingIA = bool.Parse(lignes[6]);

                    OnPropertyChanged(nameof(FullnameCV));
                    OnPropertyChanged(nameof(FullnameTemplateCV));
                    OnPropertyChanged(nameof(FullPathOutput));
                    OnPropertyChanged(nameof(TitreCV));
                    OnPropertyChanged(nameof(NbrCompetencesMax));

                }
            }
            catch
            {
                return;
            }
        }

        private void SaveParam()
        {
            try
            {
                StringBuilder sb = new();
                sb.AppendLine(FullnameCV);
                sb.AppendLine(FullnameTemplateCV);
                sb.AppendLine(FullPathOutput);
                sb.AppendLine(TitreCV);
                sb.AppendLine(NbrCompetencesMax.ToString());
                sb.AppendLine(IsCVAnonyme.ToString());
                sb.AppendLine(IsUsingIA.ToString());


                System.IO.File.WriteAllText(NomFichierParams, sb.ToString());
            }
            catch
            {
                return;
            }
        }
        #endregion
    }
}
