
using CtrEditor.Siemens;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using Newtonsoft.Json.Linq;

namespace CtrEditor.ObjetosSim
{
    /// <summary>
    /// Interaction logic for ucAnalogTag.xaml
    /// </summary>
    /// 

    public partial class osAnalogTag : osBase, IosBase
    {

        // Otros datos y métodos relevantes para la simulación

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

        [ObservableProperty]
        public float tamano;
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(Value))]
        public string tag;
        [ObservableProperty]
        public string descripcion;
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(Value))]
        public float min_IN_Scaled;
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(Value))]
        public float max_IN_Scaled;
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(Value))]
        public float min_OUT_Scaled;
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(Value))]
        public float max_OUT_Scaled;

        [ObservableProperty]
        public float value;

        partial void OnValueChanged(float value)
        {
            EscribirWordTagScaled(Tag, Value, Min_IN_Scaled, Max_IN_Scaled, Min_OUT_Scaled, Max_OUT_Scaled);
        }

        public osAnalogTag()
        {
            Tamano = 0.30f;
            tag = "%MW50.0";
            Descripcion = "Nombre del Tag:";
            Max_OUT_Scaled = 27648;
            Min_OUT_Scaled = 0;
            min_IN_Scaled = 0;
            max_IN_Scaled = 100;

        }

        public override void UpdateGeometryStart()
        {
            // Se llama antes de la simulacion

        }
        public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)
        {
        }

        public override void UpdatePLCPrimerCiclo()
        {
            // Escribimos el valor actual al iniciar la conexion
            OnValueChanged(Value);
        }

        public override void UpdateControl(int elapsedMilliseconds)
        {
            // Calculamos la velocidad

        }

        public override void ucLoaded()
        {
            // El UserControl ya se ha cargado y podemos obtener las coordenadas para 
            // crear el objeto de simulacion
            ActualizarLeftTop();

        }
    }

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

        public ucAnalogTag()
        {
            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) { }
        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) { }
        public void Highlight(bool State) { }
        public int ZIndex()
        {
            return 10;
        }
    }
}
