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

namespace Noctalk.UI.Components.Input;

public sealed class Noctalk_textbox : TextBox
{
    private static readonly Brush Background_normal = UI__brush(8, 16, 29);
    private static readonly Brush Foreground_normal = UI__brush(185, 243, 255);
    private static readonly Brush Border_normal = UI__brush(20, 107, 147);
    private static readonly Brush Blink_background_red = UI__brush(54, 10, 18);
    private static readonly Brush Blink_border_red = UI__brush(178, 50, 72);
    private bool isBlinking;

    public Noctalk_textbox()
    {
        MinHeight = 34;
        Padding = new Thickness(8, 6, 8, 6);
        Margin = new Thickness(4);
        Background = Background_normal;
        Foreground = Foreground_normal;
        CaretBrush = UI__brush(73, 217, 255);
        BorderBrush = Border_normal;
        SelectionBrush = UI__brush(20, 90, 119);
        BorderThickness = new Thickness(1);
        VerticalContentAlignment = VerticalAlignment.Center;
    }

    public async void Make_textbox_blink_red_for_1_second()
    {
        if (isBlinking)
        {
            return;
        }

        isBlinking = true;

        try
        {
            for (int index = 0; index < 3; index++)
            {
                Background = Blink_background_red;
                BorderBrush = Blink_border_red;
                await Task.Delay(83);
                Background = Background_normal;
                BorderBrush = Border_normal;
                await Task.Delay(83);
            }
        }
        finally
        {
            isBlinking = false;
            Background = Background_normal;
            BorderBrush = Border_normal;
        }
    }

    private static SolidColorBrush UI__brush(byte Red, byte Green, byte Blue)
    {
        return new SolidColorBrush(Color.FromRgb(Red, Green, Blue));
    }
}
