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

namespace Noctalk.UI.Components.DropDown;

public static class Noctalk_dropdown_menu
{
    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));

    public static ContextMenu Create()
    {
        return new ContextMenu
        {
            Background = Background_normal,
            BorderBrush = Border_normal,
            BorderThickness = new Thickness(1),
            Padding = new Thickness(4),
            HasDropShadow = false,
            Template = UI__dropdown_menu__create_template()
        };
    }

    public static MenuItem Create_item(string The_header)
    {
        MenuItem item = new()
        {
            Header = The_header,
            Foreground = Text_normal,
            Background = Background_normal,
            Padding = new Thickness(12, 7, 18, 7),
            MinWidth = 150
        };
        item.Style = UI__dropdown_menu__item_style();
        return item;
    }

    private static Style UI__dropdown_menu__item_style()
    {
        Style style = new(typeof(MenuItem));
        style.Setters.Add(new Setter(Control.ForegroundProperty, Text_normal));
        style.Setters.Add(new Setter(Control.BackgroundProperty, Background_normal));
        style.Setters.Add(new Setter(Control.PaddingProperty, new Thickness(12, 7, 18, 7)));

        ControlTemplate template = new(typeof(MenuItem));
        FrameworkElementFactory border = new(typeof(Border));
        border.Name = "MenuItemBorder";
        border.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Control.BackgroundProperty));
        border.SetValue(Border.PaddingProperty, new TemplateBindingExtension(Control.PaddingProperty));

        FrameworkElementFactory content = new(typeof(ContentPresenter));
        content.SetValue(ContentPresenter.ContentSourceProperty, "Header");
        content.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
        content.SetValue(ContentPresenter.RecognizesAccessKeyProperty, true);
        border.AppendChild(content);
        template.VisualTree = border;

        Trigger hoverTrigger = new() { Property = MenuItem.IsHighlightedProperty, Value = true };
        hoverTrigger.Setters.Add(new Setter(Control.BackgroundProperty, Background_hover, "MenuItemBorder"));
        template.Triggers.Add(hoverTrigger);
        style.Setters.Add(new Setter(Control.TemplateProperty, template));

        return style;
    }

    private static ControlTemplate UI__dropdown_menu__create_template()
    {
        ControlTemplate template = new(typeof(ContextMenu));

        FrameworkElementFactory border = new(typeof(Border));
        border.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Control.BackgroundProperty));
        border.SetValue(Border.BorderBrushProperty, new TemplateBindingExtension(Control.BorderBrushProperty));
        border.SetValue(Border.BorderThicknessProperty, new TemplateBindingExtension(Control.BorderThicknessProperty));
        border.SetValue(Border.PaddingProperty, new TemplateBindingExtension(Control.PaddingProperty));

        FrameworkElementFactory itemsPresenter = new(typeof(ItemsPresenter));
        itemsPresenter.SetValue(FrameworkElement.MarginProperty, new Thickness(0));
        border.AppendChild(itemsPresenter);
        template.VisualTree = border;

        return template;
    }
}
