using ReAct_PME.WPF_FrameWork;
using Services;
using System.Collections.ObjectModel;
using ToolsServices;
using Services.Fooocus;

namespace ReAct_PME.ViewModels
{
    public class ParametresFooocus_VM : BaseViewModel
    {
        private ObservableCollection<string> _listeSelectedsStylesFooocus = new();
        public ObservableCollection<string> ListeSelectedsStylesFooocus
        {
            get => _listeSelectedsStylesFooocus;
            set
            {
                _listeSelectedsStylesFooocus = value;
                OnPropertyChanged(nameof(ListeSelectedsStylesFooocus));
            }
        }

        public ObservableCollection<string> ListeStylesFooocusFiltered { get; set; } = new();

        public ObservableCollection<string> ListeStylesFooocus { get; set; } = new();

        private FooocusRequest_Text_to_Image _SelectedItem = new();
        public FooocusRequest_Text_to_Image SelectedItem
        {
            get => _SelectedItem;
            set
            {
                _SelectedItem = value;
                OnPropertyChanged(nameof(SelectedItem));
            }
        }

        public ParametresFooocus_VM()
        {
            SelectedItem = ChatService.LoadParametresFooocus();
            _ = GetStylesFooocusAsync();
            ListeSelectedsStylesFooocus = new ObservableCollection<string>(SelectedItem.style_selections.ToList().Select(s => s.Trim()));
            OnPropertyChanged(nameof(ListeSelectedsStylesFooocus));
        }

        public bool SaveData()
        {
            return ChatService.SaveParametresFooocus(SelectedItem, ListeSelectedsStylesFooocus.ToList());
        }

        public void FilterStyles(string filter)
        {
            if (string.IsNullOrWhiteSpace(filter))
            {
                ListeStylesFooocusFiltered = new ObservableCollection<string>(ListeStylesFooocus.OrderBy(s => s));
            }
            else
            {
                var filtered = ListeStylesFooocus.Where(s => s.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0).OrderBy(s => s);
                ListeStylesFooocusFiltered = new ObservableCollection<string>(filtered);
            }
            OnPropertyChanged(nameof(ListeStylesFooocusFiltered));
        }

        private async Task GetStylesFooocusAsync()
        {
            try
            {
                var styles = await ChatService.GetFooocusStylesAsync();
                foreach (var style in styles)
                    ListeStylesFooocus.Add(style);
                OnPropertyChanged(nameof(ListeStylesFooocus));
                ListeStylesFooocusFiltered = new ObservableCollection<string>(ListeStylesFooocus.OrderBy(s => s));
                OnPropertyChanged(nameof(ListeStylesFooocusFiltered));
            }
            catch (Exception ex)
            {
                LoggerService.LogError($"Erreur lors de la récupération des styles de Fooocus : {ex.Message}");
            }
        }

        public void ToggleSelection(string style, bool isSelected)
        {
            if (isSelected)
            {
                if (!ListeSelectedsStylesFooocus.Contains(style))
                    ListeSelectedsStylesFooocus.Add(style);
            }
            else
            {
                if (ListeSelectedsStylesFooocus.Contains(style))
                    ListeSelectedsStylesFooocus.Remove(style);
            }
        }

    }
}
