using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CtrEditor.ObjetosSim;
using System.Collections.ObjectModel;
using System.Windows;
using CtrEditor.ObjetosSim.Extraccion_Datos;


namespace CtrEditor.PopUps
{
    /// <summary>
    /// Interaction logic for AssignImagesWindow.xaml
    /// </summary>
    public partial class AssignImagesWindow : Window
    {
        public AssignImagesWindow()
        {
            InitializeComponent();
        }

    }

    public partial class AssignImagesViewModel : ObservableObject
    {
        private MainViewModel _mainViewModel;
        private Window _window;
        private bool _closeOK;
        [ObservableProperty]
        private ObservableCollection<osBase> objetosSimulables;

        [ObservableProperty]
        private ObservableCollection<string> listaImagenes;

        [ObservableProperty]
        private ObservableCollection<osBase> selectedObjetosSimulables;

        [ObservableProperty]
        private ObservableCollection<string> selectedImagenes;

        public AssignImagesViewModel()
        {
            SelectedImagenes = new ObservableCollection<string>();
            ListaImagenes = new ObservableCollection<string>();
            ObjetosSimulables = new ObservableCollection<osBase>();

            SelectedObjetosSimulables = new ObservableCollection<osBase>();
            SelectedObjetosSimulables.CollectionChanged += SelectedObjetosSimulables_CollectionChanged;
        }

        public void Initialize(MainViewModel mainViewModel, Window window)
        {
            _mainViewModel = mainViewModel;
            _window = window;
            ObjetosSimulables = new ObservableCollection<osBase>(_mainViewModel.ObjetosSimulables.Where(
                o => o.Enable_On_All_Pages && ((o is osExtraccionTag ex && !ex.Cloned) || (o is osBuscarCoincidencias))));

            ListaImagenes = _mainViewModel.ListaImagenes;
        }

        private void SelectedObjetosSimulables_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UpdateSelectedImages();
        }

        private void UpdateSelectedImages()
        {
            var selectedImages = new HashSet<string>();

            foreach (var objeto in SelectedObjetosSimulables)
            {
                foreach (var image in objeto.ShowOnThisPagesList)
                {
                    selectedImages.Add(image);
                }
            }

            SelectedImagenes = new ObservableCollection<string>(selectedImages);
        }

        [RelayCommand]
        private void AssignImages()
        {
            foreach (var objeto in SelectedObjetosSimulables)
            {
                objeto.ShowOnThisPagesList = new List<string>(SelectedImagenes);
                objeto.Show_On_This_Page = objeto.Show_On_This_Page;
            }
            _closeOK = true;
            _window.Close();
        }

        [RelayCommand]
        private void TBSelectAll()
        {
            SelectedObjetosSimulables.Clear();
            foreach (var obj in ObjetosSimulables)
            {
                SelectedObjetosSimulables.Add(obj);
            }
        }

        [RelayCommand]
        private void TBUnSelectAll()
        {
            SelectedObjetosSimulables.Clear();
        }

        [RelayCommand]
        private void TBSelectAllIamges()
        {
            SelectedImagenes.Clear();
            foreach (var img in ListaImagenes)
            {
                SelectedImagenes.Add(img);
            }
        }

        [RelayCommand]
        private void TBUnSelectAllImages()
        {
            SelectedImagenes.Clear();
        }

        public bool CloseOK => _closeOK;
    }



}

