using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using CommunityToolkit.Mvvm.ComponentModel;
using CtrEditor.Simulacion;
using Newtonsoft.Json;


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

    public partial class osExtraccionTag : osBase, IosBase
    {
        private osBase _osMotor = null;

        private simTransporte SimGeometria;

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

        [ObservableProperty]
        bool extraer;

        public override void TopChanged(float value)
        {
            base.TopChanged(value);
            ResetTimer();           
        }

        public override void LeftChanged(float value)
        {
            base.LeftChanged(value);
            ResetTimer();
        }

        partial void OnExtraerChanged(bool value)
        {
            ResetTimer();
        }

        [ObservableProperty]
        public float ancho;

        partial void OnAnchoChanged(float value)
        {
            ResetTimer();
        }

        [ObservableProperty]
        public float alto;

        partial void OnAltoChanged(float value)
        {
            ResetTimer();
        }

        public override void OnTimerAfterMovement() {
            if (Extraer)
                CaptureImageAreaAndDoOCR();
        }

        [ObservableProperty]
        osBuscarCoincidencias search_Templates;

        [ObservableProperty]
        public float angulo;

        [ObservableProperty]
        string tag_extract;

        [ObservableProperty]
        string clase;

        [ObservableProperty]
        string tag_name;

        [ObservableProperty]
        float opacity_oculto;

        public osExtraccionTag()
        {
            Ancho = 1;
            Alto = 1;
            Angulo = 0;
            Opacity_oculto = 0.1f;
        }

        public void CaptureImageAreaAndDoOCR()
        {
            Tag_extract = CaptureImageAreaAndDoOCR(Left, Top, Ancho, Alto);
        }

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

    }

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

        public ucExtraccionTag()
        {
            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 osExtraccionTag datos)
            {
                datos.Ancho = PixelToMeter.Instance.calc.PixelsToMeters(width);
                datos.Alto = PixelToMeter.Instance.calc.PixelsToMeters(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)
        {
            if (Datos != null)
                if (Datos is osExtraccionTag datos)
                    datos.Angulo = Angle;
        }
        public void Highlight(bool State) { }
        public int ZIndex()
        {
            return 1;
        }

    }
}



