using CommunityToolkit.Mvvm.ComponentModel;
using LibS7Adv;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using CtrEditor.FuncionesBase;
using System.ComponentModel;
using Newtonsoft.Json;

namespace CtrEditor.ObjetosSim
{
    /// <summary>
    /// A custom image control that can display an image from a file path
    /// </summary>
    public partial class osCustomImage : osBase, IosBase
    {
        public static string NombreClase()
        {
            return "Custom Image";
        }

        private string nombre = NombreClase();
        public override string Nombre
        {
            get => nombre;
            set => SetProperty(ref nombre, value);
        }


        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(ImageSource_oculta))]
        [property: Description("Path to the image file")]
        [property: Category("Image:")]
        private string imagePath;

        [ObservableProperty]
        [property: Description("Flip the image horizontally")]
        [property: Category("Image:")]
        private bool horizontal_Flip;

        [ObservableProperty]
        [property: Description("Flip the image vertically")]
        [property: Category("Image:")]
        private bool vertical_Flip;


        [JsonIgnore]
        [ObservableProperty]
        public ImageSource imageSource_oculta;

        partial void OnImagePathChanged(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                ImageSource_oculta = ImageFromPath(value);
            }
            else
            {
                // Si no hay path, usar la imagen por defecto
                ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
            }
        }

        partial void OnHorizontal_FlipChanged(bool value)
        {
            // Property changed handler will trigger UI update through binding
        }

        partial void OnVertical_FlipChanged(bool value)
        {
            // Property changed handler will trigger UI update through binding
        }

        public osCustomImage()
        {
            Ancho = 0.30f;
            Alto = 0.30f;
            // Establecer la imagen por defecto al crear el objeto
            ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
        }

        public override void ucLoaded()
        {
            base.ucLoaded();
            if (!string.IsNullOrEmpty(ImagePath))
            {
                ImageSource_oculta = ImageFromPath(ImagePath);
            }
            else
            {
                // Si no hay path al cargar, usar la imagen por defecto
                ImageSource_oculta = ImageFromPath("/Icons/unselect.png");
            }
        }
    }

    public partial class ucCustomImage : UserControl, IDataContainer
    {
        public osBase? Datos { get; set; }
        public int zIndex_fromFrames { get; set; }

        public ucCustomImage()
        {
            InitializeComponent();
            this.Loaded += OnLoaded;
            this.Unloaded += OnUnloaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            Datos?.ucLoaded();
        }

        private void OnUnloaded(object sender, RoutedEventArgs e)
        {
            Datos?.ucUnLoaded();
        }

        public void Highlight(bool State) { }

        public ZIndexEnum ZIndex_Base()
        {
            return ZIndexEnum.Estaticos;
        }
    }
}