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

namespace Noctalk.UI.Components.Chat;

internal sealed class Chat__send_message__image_preview : Grid
{
    private static readonly Brush Preview_hover_overlay = new SolidColorBrush(Color.FromArgb(55, 255, 255, 255));
    private static readonly Brush Preview_remove_background = new SolidColorBrush(Color.FromArgb(210, 5, 9, 18));
    private static readonly Brush Preview_remove_foreground = new SolidColorBrush(Color.FromRgb(225, 252, 255));
    private readonly Border previewHover = new();
    private readonly System.Windows.Controls.Button previewRemoveButton = new();

    public Chat__send_message__image_preview(BitmapSource The_image, Action The_remove)
    {
        Width = 82;
        Height = 62;
        ClipToBounds = true;
        Cursor = System.Windows.Input.Cursors.Arrow;
        Margin = new Thickness(0, 0, 6, 0);

        Image previewImage = new()
        {
            Source = The_image,
            Width = 82,
            Height = 62,
            Stretch = Stretch.UniformToFill,
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };

        previewHover.CornerRadius = new CornerRadius(10);
        previewHover.Background = Preview_hover_overlay;
        previewHover.Visibility = Visibility.Collapsed;
        previewHover.IsHitTestVisible = false;

        previewRemoveButton.Content = "x";
        previewRemoveButton.Width = 22;
        previewRemoveButton.Height = 22;
        previewRemoveButton.Padding = new Thickness(0);
        previewRemoveButton.Margin = new Thickness(0, 4, 4, 0);
        previewRemoveButton.HorizontalAlignment = HorizontalAlignment.Right;
        previewRemoveButton.VerticalAlignment = VerticalAlignment.Top;
        previewRemoveButton.Background = Preview_remove_background;
        previewRemoveButton.Foreground = Preview_remove_foreground;
        previewRemoveButton.BorderThickness = new Thickness(0);
        previewRemoveButton.FontWeight = FontWeights.Bold;
        previewRemoveButton.Cursor = System.Windows.Input.Cursors.Hand;
        previewRemoveButton.Visibility = Visibility.Collapsed;
        previewRemoveButton.Template = Create_remove_button_template();
        previewRemoveButton.Click += (_, _) => The_remove();

        MouseEnter += (_, _) =>
        {
            previewHover.Visibility = Visibility.Visible;
            previewRemoveButton.Visibility = Visibility.Visible;
        };
        MouseLeave += (_, _) =>
        {
            previewHover.Visibility = Visibility.Collapsed;
            previewRemoveButton.Visibility = Visibility.Collapsed;
        };

        Children.Add(previewImage);
        Children.Add(previewHover);
        Children.Add(previewRemoveButton);
    }

    private static ControlTemplate Create_remove_button_template()
    {
        FrameworkElementFactory border = new(typeof(Border));
        border.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Control.BackgroundProperty));
        border.SetValue(Border.CornerRadiusProperty, new CornerRadius(11));

        FrameworkElementFactory content = new(typeof(ContentPresenter));
        content.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
        content.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
        border.AppendChild(content);

        return new ControlTemplate(typeof(System.Windows.Controls.Button)) { VisualTree = border };
    }
}
