using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WpfPath = System.Windows.Shapes.Path;

namespace Noctalk.UI.Components.Input;

public sealed class Noctalk_checkbox : CheckBox
{
    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 Check_normal = new SolidColorBrush(Color.FromRgb(73, 217, 255));

    public Noctalk_checkbox()
    {
        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__checkbox__create_template();
    }

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

        FrameworkElementFactory box = new(typeof(Border));
        box.Name = "CheckBoxBorder";
        box.SetValue(Border.WidthProperty, 22.0);
        box.SetValue(Border.HeightProperty, 22.0);
        box.SetValue(Border.MarginProperty, new Thickness(0, 0, 8, 0));
        box.SetValue(Border.BackgroundProperty, Background_normal);
        box.SetValue(Border.BorderBrushProperty, Border_normal);
        box.SetValue(Border.BorderThicknessProperty, new Thickness(1));

        FrameworkElementFactory mark = new(typeof(WpfPath));
        mark.Name = "CheckMark";
        mark.SetValue(WpfPath.DataProperty, Geometry.Parse("M 5 11 L 9 15 L 17 6"));
        mark.SetValue(WpfPath.StrokeProperty, Check_normal);
        mark.SetValue(WpfPath.StrokeThicknessProperty, 2.6);
        mark.SetValue(WpfPath.StrokeStartLineCapProperty, PenLineCap.Round);
        mark.SetValue(WpfPath.StrokeEndLineCapProperty, PenLineCap.Round);
        mark.SetValue(WpfPath.VisibilityProperty, Visibility.Collapsed);
        box.AppendChild(mark);

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

        panel.AppendChild(box);
        panel.AppendChild(content);

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

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

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

        return template;
    }
}
