using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using CtrEditor.ObjetosSim;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
using System.Linq;

namespace CtrEditor.Models
{
    public class ImageData : INotifyPropertyChanged
    {
        private string _customName;
        private List<string> _tags;
        private string _etiquetas;

        public string FileName { get; set; }

        [DisplayName("Nombre personalizado")]
        public string CustomName
        {
            get => _customName;
            set
            {
                _customName = value;
                OnPropertyChanged();
            }
        }

        [Browsable(false)]
        public List<string> Tags
        {
            get => _tags ?? new List<string>();
            set
            {
                _tags = value;
                _etiquetas = string.Join(" ", value.Select(tag => tag.StartsWith("#") ? tag : "#" + tag));
                OnPropertyChanged();
                OnPropertyChanged(nameof(Etiquetas));
            }
        }

        [DisplayName("Etiquetas")]
        [property: Editor(typeof(TagPropertyEditor), typeof(TagPropertyEditor))]
        public string Etiquetas
        {
            get => _etiquetas ?? string.Empty;
            set
            {
                _etiquetas = value ?? string.Empty;
                // Convertir string de etiquetas a List<string>
                if (string.IsNullOrWhiteSpace(_etiquetas))
                {
                    _tags = new List<string>();
                }
                else
                {
                    _tags = _etiquetas.Split(' ', StringSplitOptions.RemoveEmptyEntries)
                                    .Select(tag => tag.StartsWith("#") ? tag.Substring(1) : tag)
                                    .Where(tag => !string.IsNullOrWhiteSpace(tag))
                                    .ToList();
                }
                OnPropertyChanged();
                OnPropertyChanged(nameof(Tags));
            }
        }

        /// <summary>
        /// Lista de etiquetas sin el prefijo #, compatible con osBase
        /// </summary>
        [Browsable(false)]
        public List<string> ListaEtiquetas
        {
            get
            {
                if (string.IsNullOrWhiteSpace(Etiquetas))
                    return new List<string>();

                return Etiquetas.Split(' ', StringSplitOptions.RemoveEmptyEntries)
                               .Select(tag => tag.StartsWith("#") ? tag.Substring(1) : tag)
                               .Where(tag => !string.IsNullOrWhiteSpace(tag))
                               .ToList();
            }
        }

        public string DisplayName => !string.IsNullOrEmpty(CustomName) ? CustomName : FileName;

        public ImageData()
        {
            FileName = string.Empty;
            _customName = string.Empty;
            _tags = new List<string>();
            _etiquetas = string.Empty;
        }

        public ImageData(string fileName, string customName = null, List<string> tags = null)
        {
            FileName = fileName;
            _customName = customName ?? string.Empty;
            _tags = tags ?? new List<string>();
            _etiquetas = string.Join(" ", _tags.Select(tag => tag.StartsWith("#") ? tag : "#" + tag));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}