using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CtrEditor.ObjetosSim.UserControls
{
    /// <summary>
    /// Interaction logic for CircularSegment.xaml
    /// </summary>
    public partial class CircularSegment : UserControl
    {
        public double Angle
        {
            get { return (double)GetValue(AngleProperty); }
            set { SetValue(AngleProperty, value); }
        }

        public static readonly DependencyProperty AngleProperty =
            DependencyProperty.Register("Angle", typeof(double), typeof(CircularSegment), new PropertyMetadata(0.0));

        public double OuterRadius
        {
            get { return (double)GetValue(OuterRadiusProperty); }
            set { SetValue(OuterRadiusProperty, value); }
        }

        public static readonly DependencyProperty OuterRadiusProperty =
            DependencyProperty.Register("OuterRadius", typeof(double), typeof(CircularSegment), new PropertyMetadata(100.0, OnPropertyChanged));

        public double InnerRadius
        {
            get { return (double)GetValue(InnerRadiusProperty); }
            set { SetValue(InnerRadiusProperty, value); }
        }

        public static readonly DependencyProperty InnerRadiusProperty =
            DependencyProperty.Register("InnerRadius", typeof(double), typeof(CircularSegment), new PropertyMetadata(80.0, OnPropertyChanged));

        public double StartAngle
        {
            get { return (double)GetValue(StartAngleProperty); }
            set { SetValue(StartAngleProperty, value); }
        }

        public static readonly DependencyProperty StartAngleProperty =
            DependencyProperty.Register("StartAngle", typeof(double), typeof(CircularSegment), new PropertyMetadata(0.0, OnPropertyChanged));

        public double EndAngle
        {
            get { return (double)GetValue(EndAngleProperty); }
            set { SetValue(EndAngleProperty, value); }
        }

        public static readonly DependencyProperty EndAngleProperty =
            DependencyProperty.Register("EndAngle", typeof(double), typeof(CircularSegment), new PropertyMetadata(90.0, OnPropertyChanged));

        public CircularSegment()
        {
            InitializeComponent();
        }

        // Propiedad pública para acceder al Path desde código externo
        public System.Windows.Shapes.Path TransportePath => path;

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircularSegment)?.DrawSegment();
        }

        private void DrawSegment()
        {
            if (OuterRadius <= 0 || InnerRadius <= 0 || StartAngle == EndAngle)
                return;

            PathGeometry geometry = new PathGeometry();
            PathFigure figure = new PathFigure();

            // Calcula el centro del Canvas (suponemos que el Canvas es lo suficientemente grande)
            Point center = new Point(OuterRadius, OuterRadius);

            // Calcular los puntos de inicio y fin del arco externo
            Point startPoint = new Point(center.X + Math.Cos(StartAngle * Math.PI / 180) * OuterRadius,
                                         center.Y + Math.Sin(StartAngle * Math.PI / 180) * OuterRadius);
            Point endPoint = new Point(center.X + Math.Cos(EndAngle * Math.PI / 180) * OuterRadius,
                                       center.Y + Math.Sin(EndAngle * Math.PI / 180) * OuterRadius);

            // Crear arco externo
            bool largeArc = Math.Abs(EndAngle - StartAngle) > 180.0;
            figure.StartPoint = startPoint;
            figure.Segments.Add(new ArcSegment(endPoint, new Size(OuterRadius, OuterRadius), 0, largeArc, SweepDirection.Clockwise, true));

            // Crear línea hacia el arco interno
            figure.Segments.Add(new LineSegment(new Point(center.X + Math.Cos(EndAngle * Math.PI / 180) * InnerRadius,
                                                         center.Y + Math.Sin(EndAngle * Math.PI / 180) * InnerRadius), true));

            // Crear arco interno
            startPoint = new Point(center.X + Math.Cos(StartAngle * Math.PI / 180) * InnerRadius,
                                   center.Y + Math.Sin(StartAngle * Math.PI / 180) * InnerRadius);
            figure.Segments.Add(new ArcSegment(startPoint, new Size(InnerRadius, InnerRadius), 0, largeArc, SweepDirection.Counterclockwise, true));

            // Cerrar la figura
            figure.IsClosed = true;
            geometry.Figures.Add(figure);
            path.Data = geometry;
        }
    }
}
