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

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


    [ObservableObject]
    public partial class ThreeLinesControl : UserControl
    {
        private Rectangle _liz;
        private Rectangle _lc;
        private Rectangle _lde;

        public ThreeLinesControl()
        {
            InitializeComponent();
            this.Loaded += ThreeLinesControl_Loaded;
            //Color = Colors.Gray;
        }

        private void ThreeLinesControl_Loaded(object sender, RoutedEventArgs e)
        {
            CreateOrUpdateRectangles();
        }

        public Color Color
        {
            get { return (Color)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }

        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register("Color", typeof(Color), typeof(ThreeLinesControl), new PropertyMetadata(Colors.Red, OnDimensionsChanged));

        public double AnchoRecto
        {
            get { return (double)GetValue(AnchoRectoProperty); }
            set { SetValue(AnchoRectoProperty, value); }
        }

        public static readonly DependencyProperty AnchoRectoProperty =
            DependencyProperty.Register("AnchoRecto", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));

        public double AnchoCentro
        {
            get { return (double)GetValue(AnchoCentroProperty); }
            set { SetValue(AnchoCentroProperty, value); }
        }

        public static readonly DependencyProperty AnchoCentroProperty =
            DependencyProperty.Register("AnchoCentro", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));

        public double Altura
        {
            get { return (double)GetValue(AlturaProperty); }
            set { SetValue(AlturaProperty, value); }
        }

        public static readonly DependencyProperty AlturaProperty =
            DependencyProperty.Register("Altura", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(100.0, OnDimensionsChanged));

        public double AltoGuia
        {
            get { return (double)GetValue(AltoGuiaProperty); }
            set { SetValue(AltoGuiaProperty, value); }
        }

        public static readonly DependencyProperty AltoGuiaProperty =
            DependencyProperty.Register("AltoGuia", typeof(double), typeof(ThreeLinesControl), new PropertyMetadata(10.0, OnDimensionsChanged));

        private static void OnDimensionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as ThreeLinesControl;
            control?.CreateOrUpdateRectangles();
        }

        private void CreateOrUpdateRectangles()
        {
            if (_liz == null || _lc == null || _lde == null)
            {
                // Crear Liz
                _liz = new Rectangle
                {
                    Width = AnchoRecto,
                    Height = AltoGuia,
                    Fill = new SolidColorBrush(Color)
                };
                Canvas.SetLeft(_liz, 0);
                Canvas.SetTop(_liz, -AltoGuia / 2);
                Canvas.Children.Add(_liz);

                // Crear Lc
                _lc = new Rectangle
                {
                    Width = CalculateLcWidth(),
                    Height = AltoGuia,
                    Fill = new SolidColorBrush(Color),
                    RenderTransformOrigin = new Point(0, 0.5)
                };
                _lc.RenderTransform = new RotateTransform(GetRotationAngle(AnchoCentro, Altura));
                Canvas.SetLeft(_lc, AnchoRecto);
                Canvas.SetTop(_lc, -AltoGuia / 2);
                Canvas.Children.Add(_lc);

                // Crear Lde
                _lde = new Rectangle
                {
                    Width = AnchoRecto,
                    Height = AltoGuia,
                    Fill = new SolidColorBrush(Color)
                };
                Canvas.SetLeft(_lde, AnchoRecto + AnchoCentro);
                Canvas.SetTop(_lde, Altura - AltoGuia / 2);
                Canvas.Children.Add(_lde);
            }
            else
            {
                // Actualizar Liz
                _liz.Width = AnchoRecto;
                _liz.Height = AltoGuia;
                _liz.Fill = new SolidColorBrush(Color);
                Canvas.SetLeft(_liz, 0);
                Canvas.SetTop(_liz, -AltoGuia / 2);

                // Actualizar Lc
                _lc.Width = CalculateLcWidth();
                _lc.Height = AltoGuia;
                _lc.Fill = new SolidColorBrush(Color);
                _lc.RenderTransform = new RotateTransform(GetRotationAngle(AnchoCentro, Altura));
                Canvas.SetLeft(_lc, AnchoRecto);
                Canvas.SetTop(_lc, -AltoGuia / 2);

                // Actualizar Lde
                _lde.Width = AnchoRecto;
                _lde.Height = AltoGuia;
                _lde.Fill = new SolidColorBrush(Color);
                Canvas.SetLeft(_lde, AnchoRecto + AnchoCentro);
                Canvas.SetTop(_lde, Altura - AltoGuia / 2);
            }
        }

        // Método auxiliar para calcular la hipotenusa
        private double CalculateLcWidth()
        {
            return Math.Sqrt(Math.Pow(AnchoCentro, 2) + Math.Pow(Altura, 2));
        }

        private double GetRotationAngle(double anchoCentro, double altura)
        {
            return Math.Atan2(altura, anchoCentro) * 180 / Math.PI;
        }
    }
}

