using System.Windows;
using System.Windows.Controls;
using CommunityToolkit.Mvvm.ComponentModel;

using CtrEditor.Siemens;
using CtrEditor.Simulacion;

namespace CtrEditor.ObjetosSim
{
    /// <summary>
    /// Interaction logic for ucGuia.xaml
    /// </summary>
    public partial class osGuia : osBase, IosBase
    {
        private simGuia SimGeometria;

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

        [ObservableProperty]
        public float ancho;

        [ObservableProperty]
        public float altoGuia;

        [ObservableProperty]
        public float angulo;

        private void ActualizarGeometrias()
        {
            if (_visualRepresentation is ucGuia uc)
                UpdateOrCreateLine(SimGeometria, uc.Guia);
        }

        public osGuia()
        {
            Ancho = 1;
            AltoGuia = 0.03f;
        }

        public override void UpdateGeometryStart()
        {
            // Se llama antes de la simulacion
            ActualizarGeometrias();
        }
        public override void UpdateControl(int elapsedMilliseconds)
        {
        }
        public override void UpdateGeometryStep()
        {
        }
        public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds) { }
        public override void ucLoaded()
        {
            // El UserControl ya se ha cargado y podemos obtener las coordenadas para 
            // crear el objeto de simulacion
            ActualizarLeftTop();

            if (_visualRepresentation is ucGuia uc)
                SimGeometria = AddLine(simulationManager, uc.Guia);
        }
        public override void ucUnLoaded()
        {
            // El UserControl se esta eliminando
            // eliminar el objeto de simulacion
            simulationManager?.Remove(SimGeometria);
        }


    }

    public partial class ucGuia : UserControl, IDataContainer
    {
        public osBase? Datos { get; set; }

        public ucGuia()
        {
            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 Resize(float width, float height)
        {
            if (Datos is osGuia datos)
                datos.Ancho = PixelToMeter.Instance.calc.PixelsToMeters(width);
        }
        public void Move(float LeftPixels, float TopPixels)
        {
            if (Datos != null)
            {
                Datos.Left = PixelToMeter.Instance.calc.PixelsToMeters(LeftPixels);
                Datos.Top = PixelToMeter.Instance.calc.PixelsToMeters(TopPixels);
            }
        }
        public void Rotate(float Angle)
        {
            if (Datos != null)
                if (Datos is osGuia datos)
                    datos.Angulo = Angle;
        }
        public void Highlight(bool State) { }
        public int ZIndex()
        {
            return 3;
        }

    }
}
