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

namespace Noctalk.UI.Components.Input;

public sealed class Noctalk_radiobutton : RadioButton
{
    private static readonly Brush Background_normal = new SolidColorBrush(Color.FromRgb(8, 16, 29));
    private static readonly Brush Background_hover = new SolidColorBrush(Color.FromRgb(10, 42, 68));
    private static readonly Brush Border_normal = new SolidColorBrush(Color.FromRgb(20, 107, 147));
    private static readonly Brush Text_normal = new SolidColorBrush(Color.FromRgb(185, 243, 255));
    private static readonly Brush Dot_normal = new SolidColorBrush(Color.FromRgb(73, 217, 255));

    public Noctalk_radiobutton()
    {
        MinHeight = 34;
        Margin = new Thickness(4);
        Padding = new Thickness(0);
        Foreground = Text_normal;
        VerticalContentAlignment = VerticalAlignment.Center;
        Cursor = System.Windows.Input.Cursors.Hand;
        OverridesDefaultStyle = true;
        Template = UI__radiobutton__create_template();
    }

    private static ControlTemplate UI__radiobutton__create_template()
    {
        FrameworkElementFactory panel = new(typeof(StackPanel));
        panel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
        panel.SetValue(StackPanel.VerticalAlignmentProperty, VerticalAlignment.Center);

        FrameworkElementFactory outerCircle = new(typeof(Border));
        outerCircle.Name = "OuterCircle";
        outerCircle.SetValue(Border.WidthProperty, 22.0);
        outerCircle.SetValue(Border.HeightProperty, 22.0);
        outerCircle.SetValue(Border.CornerRadiusProperty, new CornerRadius(11));
        outerCircle.SetValue(Border.MarginProperty, new Thickness(0, 0, 8, 0));
        outerCircle.SetValue(Border.BackgroundProperty, Background_normal);
        outerCircle.SetValue(Border.BorderBrushProperty, Border_normal);
        outerCircle.SetValue(Border.BorderThicknessProperty, new Thickness(1));

        FrameworkElementFactory dot = new(typeof(Ellipse));
        dot.Name = "SelectedDot";
        dot.SetValue(Ellipse.WidthProperty, 10.0);
        dot.SetValue(Ellipse.HeightProperty, 10.0);
        dot.SetValue(Ellipse.FillProperty, Dot_normal);
        dot.SetValue(Ellipse.HorizontalAlignmentProperty, HorizontalAlignment.Center);
        dot.SetValue(Ellipse.VerticalAlignmentProperty, VerticalAlignment.Center);
        dot.SetValue(Ellipse.VisibilityProperty, Visibility.Collapsed);
        outerCircle.AppendChild(dot);

        FrameworkElementFactory content = new(typeof(ContentPresenter));
        content.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
        content.SetValue(ContentPresenter.RecognizesAccessKeyProperty, true);

        panel.AppendChild(outerCircle);
        panel.AppendChild(content);

        ControlTemplate template = new(typeof(RadioButton)) { VisualTree = panel };

        Trigger checkedTrigger = new() { Property = IsCheckedProperty, Value = true };
        checkedTrigger.Setters.Add(new Setter(VisibilityProperty, Visibility.Visible, "SelectedDot"));
        template.Triggers.Add(checkedTrigger);

        Trigger hoverTrigger = new() { Property = IsMouseOverProperty, Value = true };
        hoverTrigger.Setters.Add(new Setter(Border.BackgroundProperty, Background_hover, "OuterCircle"));
        template.Triggers.Add(hoverTrigger);

        return template;
    }
}
