using Noctalk.UI.Components.Button;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Noctalk.UI.Components.Chat;

public sealed class Chat__message_image_view : Grid
{
    private readonly Image image = new();
    private readonly Noctalk_button copyButton = new();

    public event EventHandler<ImageSource>? Image_clicked;

    public Chat__message_image_view(BitmapSource The_image_source, double The_width)
    {
        Background = Brushes.Transparent;
        image.Source = The_image_source;
        image.Width = The_width;
        image.Stretch = Stretch.Uniform;
        image.Cursor = Cursors.Hand;
        image.MouseLeftButtonDown += Image__mouse_left_button_down;

        copyButton.Icon = Noctalk_button_icon.Copy;
        copyButton.Color = Noctalk_button_color.Grey_R__Grey_R;
        copyButton.Size = Noctalk_button_size.Custom;
        copyButton.Width = 28;
        copyButton.Height = 28;
        copyButton.MinHeight = 28;
        copyButton.Padding = new Thickness(5);
        copyButton.Margin = new Thickness(6);
        copyButton.HorizontalAlignment = HorizontalAlignment.Left;
        copyButton.VerticalAlignment = VerticalAlignment.Bottom;
        copyButton.Visibility = Visibility.Collapsed;
        copyButton.bRounded_corners = true;
        copyButton.Click += Copy_button__click;

        Children.Add(image);
        Children.Add(copyButton);

        MouseEnter += (_, _) => copyButton.Visibility = Visibility.Visible;
        MouseLeave += (_, _) => copyButton.Visibility = Visibility.Collapsed;
    }

    private void Image__mouse_left_button_down(object sender, MouseButtonEventArgs e)
    {
        if (image.Source is not null)
        {
            Image_clicked?.Invoke(this, image.Source);
            e.Handled = true;
        }
    }

    private void Copy_button__click(object sender, RoutedEventArgs e)
    {
        e.Handled = true;
        if (image.Source is not BitmapSource bitmapSource)
        {
            return;
        }

        Clipboard.SetImage(bitmapSource);
        copyButton.Make_button_blink_for_1_second();
    }
}
