using ReAct_PME.WPF_FrameWork;
using Services.Ollama;
using Services.ReActAgent;

namespace ReAct_PME.ViewModels
{
    public class ParametresOllama_VM : BaseViewModel
    {
        public ParametresOllamaService SelectedItem { get; set; } = new();

        public ParametresOllama_VM()
        {
            LoadData();
        }

        private bool LoadData()
        {
            bool bReturn = false;
            try
            {
                var item = ReActAgent.LoadParametresOllama();
                if (item != null)
                {
                    SelectedItem = (ParametresOllamaService)item;
                    bReturn = true;
                }


                return bReturn;

            }
            catch
            {
                return false;
            }

        }

        public bool SaveData()
        {
            try
            {
                if(SelectedItem.Ollama_Temperature < 0.0 || SelectedItem.Ollama_Temperature > 1.0)
                    SelectedItem.Ollama_Temperature = 0.01;
                if(SelectedItem.Ollama_Top_p < 0.0 || SelectedItem.Ollama_Top_p > 1.0)
                    SelectedItem.Ollama_Top_p = 0.9;

                if(SelectedItem.Ollama_Batch_Temperature < 0.0 || SelectedItem.Ollama_Batch_Temperature > 1.0)
                    SelectedItem.Ollama_Batch_Temperature = 0.01;
                if(SelectedItem.Ollama_Batch_Top_p < 0.0 || SelectedItem.Ollama_Batch_Top_p > 1.0)
                    SelectedItem.Ollama_Batch_Top_p = 0.9;

                SelectedItem.Ollama_URL = SelectedItem.Ollama_URL.Trim();
                if (!SelectedItem.Ollama_URL.StartsWith("http://") && !SelectedItem.Ollama_URL.StartsWith("https://"))
                    SelectedItem.Ollama_URL = "http://" + SelectedItem.Ollama_URL.Trim();

                SelectedItem.Ollama_Batch_URL = SelectedItem.Ollama_Batch_URL.Trim();
                if (!SelectedItem.Ollama_Batch_URL.StartsWith("http://") && !SelectedItem.Ollama_Batch_URL.StartsWith("https://"))
                    SelectedItem.Ollama_Batch_URL = "http://" + SelectedItem.Ollama_Batch_URL.Trim();



                return ReActAgent.SaveParametresOllama(SelectedItem);
            }
            catch
            {
                return false;
            }

        }

    }
}
