using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Media;

namespace Noctalk.UI.Components.Input;

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

    public Noctalk_dropdown()
    {
        Height = 34;
        MinHeight = 34;
        Margin = new Thickness(4);
        Padding = new Thickness(8, 6, 8, 6);
        Background = Background_normal;
        Foreground = Text_normal;
        BorderBrush = Border_normal;
        BorderThickness = new Thickness(1);
        OverridesDefaultStyle = true;
        Template = UI__dropdown__create_template();
        ItemContainerStyle = UI__dropdown__create_item_style();
    }

    private static ControlTemplate UI__dropdown__create_template()
    {
        FrameworkElementFactory root = new(typeof(Grid));

        FrameworkElementFactory toggle = new(typeof(ToggleButton));
        toggle.Name = "PART_ToggleButton";
        toggle.SetValue(ToggleButton.FocusableProperty, false);
        toggle.SetValue(ContentControl.ContentProperty, new System.Windows.Data.Binding("SelectionBoxItem") { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent });
        toggle.SetValue(ToggleButton.IsCheckedProperty, new System.Windows.Data.Binding("IsDropDownOpen") { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent, Mode = System.Windows.Data.BindingMode.TwoWay });
        toggle.SetValue(ToggleButton.TemplateProperty, UI__dropdown__create_toggle_template());
        root.AppendChild(toggle);

        FrameworkElementFactory popup = new(typeof(System.Windows.Controls.Primitives.Popup));
        popup.Name = "PART_Popup";
        popup.SetValue(System.Windows.Controls.Primitives.Popup.IsOpenProperty, new System.Windows.Data.Binding("IsDropDownOpen") { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent });
        popup.SetValue(System.Windows.Controls.Primitives.Popup.PlacementProperty, System.Windows.Controls.Primitives.PlacementMode.Bottom);
        popup.SetValue(System.Windows.Controls.Primitives.Popup.AllowsTransparencyProperty, true);
        popup.SetValue(System.Windows.Controls.Primitives.Popup.FocusableProperty, false);
        popup.SetValue(System.Windows.Controls.Primitives.Popup.PopupAnimationProperty, System.Windows.Controls.Primitives.PopupAnimation.None);

        FrameworkElementFactory popupBorder = new(typeof(Border));
        popupBorder.SetValue(Border.BackgroundProperty, Background_normal);
        popupBorder.SetValue(Border.BorderBrushProperty, Border_normal);
        popupBorder.SetValue(Border.BorderThicknessProperty, new Thickness(1));
        popupBorder.SetValue(Border.MinWidthProperty, new TemplateBindingExtension(ActualWidthProperty));

        FrameworkElementFactory scrollViewer = new(typeof(ScrollViewer));
        scrollViewer.SetValue(ScrollViewer.MaxHeightProperty, 260.0);
        scrollViewer.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Hidden);
        scrollViewer.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);
        FrameworkElementFactory itemsPresenter = new(typeof(ItemsPresenter));
        scrollViewer.AppendChild(itemsPresenter);
        popupBorder.AppendChild(scrollViewer);
        popup.AppendChild(popupBorder);
        root.AppendChild(popup);

        return new ControlTemplate(typeof(ComboBox)) { VisualTree = root };
    }

    private static ControlTemplate UI__dropdown__create_toggle_template()
    {
        FrameworkElementFactory border = new(typeof(Border));
        border.Name = "ToggleBorder";
        border.SetValue(Border.BackgroundProperty, Background_normal);
        border.SetValue(Border.BorderBrushProperty, Border_normal);
        border.SetValue(Border.BorderThicknessProperty, new Thickness(1));

        FrameworkElementFactory dockPanel = new(typeof(DockPanel));
        dockPanel.SetValue(DockPanel.MarginProperty, new Thickness(8, 0, 8, 0));

        FrameworkElementFactory content = new(typeof(ContentPresenter));
        content.SetValue(ContentPresenter.ContentProperty, new TemplateBindingExtension(ContentControl.ContentProperty));
        content.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
        content.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Left);
        content.SetValue(ContentPresenter.IsHitTestVisibleProperty, false);
        content.SetValue(TextElement.ForegroundProperty, Text_normal);

        FrameworkElementFactory arrow = new(typeof(TextBlock));
        arrow.SetValue(TextBlock.TextProperty, "v");
        arrow.SetValue(TextBlock.ForegroundProperty, Accent);
        arrow.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
        arrow.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
        arrow.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
        arrow.SetValue(TextBlock.WidthProperty, 22.0);
        arrow.SetValue(DockPanel.DockProperty, Dock.Right);

        dockPanel.AppendChild(arrow);
        dockPanel.AppendChild(content);
        border.AppendChild(dockPanel);

        ControlTemplate template = new(typeof(ToggleButton)) { VisualTree = border };
        Trigger hoverTrigger = new() { Property = IsMouseOverProperty, Value = true };
        hoverTrigger.Setters.Add(new Setter(Border.BackgroundProperty, Background_hover, "ToggleBorder"));
        template.Triggers.Add(hoverTrigger);
        return template;
    }

    private static Style UI__dropdown__create_item_style()
    {
        Style style = new(typeof(ComboBoxItem));
        style.Setters.Add(new Setter(Control.BackgroundProperty, Background_normal));
        style.Setters.Add(new Setter(Control.ForegroundProperty, Text_normal));
        style.Setters.Add(new Setter(Control.PaddingProperty, new Thickness(8, 7, 8, 7)));

        ControlTemplate template = new(typeof(ComboBoxItem));
        FrameworkElementFactory border = new(typeof(Border));
        border.Name = "ItemBorder";
        border.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Control.BackgroundProperty));
        FrameworkElementFactory content = new(typeof(ContentPresenter));
        content.SetValue(ContentPresenter.MarginProperty, new TemplateBindingExtension(Control.PaddingProperty));
        border.AppendChild(content);
        template.VisualTree = border;

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

        Trigger selectedTrigger = new() { Property = ComboBoxItem.IsSelectedProperty, Value = true };
        selectedTrigger.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Color.FromRgb(14, 42, 61))));
        template.Triggers.Add(selectedTrigger);

        style.Setters.Add(new Setter(Control.TemplateProperty, template));
        return style;
    }
}
