using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using log4net;
using SparkClient.Model.Helper;
using SparkClient.Views.Grading;

namespace SparkClient.Views.Dialog;

public partial class MessageBox : Window
{
    private static readonly ILog Logger = LogManager.GetLogger(typeof(GradingResult));
    public MessageBox()
    {
        InitializeComponent();
        WindowStartupLocation = WindowStartupLocation.CenterScreen; 
        
    }
    private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
        {
            this.DragMove();
        }
    }

    private int _showType = -1;
    private MessageBoxResult _result = MessageBoxResult.None;
    public void Show(String message, String btnStr = "")
    {
        BorderInput.Visibility = Visibility.Hidden;
        Grid.SetRow(GridButtons, 2);
        CancelButton.Visibility = Visibility.Hidden;
        Grid.SetColumn(ConfirmButton, 2);
        TextBlockCenterBox.Text = message;
        System.Media.SystemSounds.Asterisk.Play();
        _showType = 0;
        this.Height = 200;
        if (!string.IsNullOrWhiteSpace(btnStr))
        {
            ConfirmButtonText.Text = btnStr;
        }
        else
        {
            ConfirmButtonText.Text = MultilingualHelper.getString("DialogConfirmDefaultTitle");
        }
        this.ShowDialog();
        
    }
    public MessageBoxResult ShowAsk(String message, String confirmButtonTitle = "", String cancelButtonTitle ="")
    {
        BorderInput.Visibility = Visibility.Hidden;
        Grid.SetRow(GridButtons, 2);
        _showType = 1;
        this.Height = 250;
        System.Media.SystemSounds.Asterisk.Play();
        TextBlockCenterBox.Text = message;
        if (!string.IsNullOrWhiteSpace(confirmButtonTitle) && !string.IsNullOrWhiteSpace(cancelButtonTitle))
        {
            ConfirmButtonText.Text = confirmButtonTitle;
            CancelButtonText.Text = cancelButtonTitle;
        }
        else
        {
            ConfirmButtonText.Text = MultilingualHelper.getString("DialogConfirmDefaultTitle");
            CancelButtonText.Text = MultilingualHelper.getString("DialogCancelDefaultTitle");
        }
        this.ShowDialog();

        return _result;
    }
    //
    public  MessageBoxResult ShowInput(String message, out String inputStr , String confirmButtonTitle = "", String cancelButtonTitle ="", bool EnterConfirm = true)
    {
        _showType = 2;
        this.Height = 300;
        TextBlockCenterBox.Text = message;
        TextBoxInput.Focus();
        if (EnterConfirm)
        {
            TextBoxInput.KeyDown += TextBoxInput_OnKeyDown;
        }

        if (!string.IsNullOrWhiteSpace(confirmButtonTitle) && !string.IsNullOrWhiteSpace(cancelButtonTitle))
        {
            ConfirmButtonText.Text = confirmButtonTitle;
            CancelButtonText.Text = cancelButtonTitle;
        }
        else
        {
            ConfirmButtonText.Text = MultilingualHelper.getString("DialogConfirmDefaultTitle");
            CancelButtonText.Text = MultilingualHelper.getString("DialogCancelDefaultTitle");
        }
        this.ShowDialog();
        
        inputStr = TextBoxInput.Text;
        return _result;
    }
    private void CloseButton_OnClick(object sender, RoutedEventArgs e)
    {
        _result = MessageBoxResult.None;
        this.Close();
    }

    private void ConfirmButton_OnClick(object sender, RoutedEventArgs e)
    {
        if (_showType == 0)
        {
            this.Close();
        }else if (_showType == 1)
        {
            _result = MessageBoxResult.OK;
            this.Close();
        }else if (_showType == 2)
        {
            if (string.IsNullOrWhiteSpace(TextBoxInput.Text))
            {
                BorderInput.BorderBrush = Brushes.Red;
                return;
            }
            else
            {
                _result = MessageBoxResult.OK;
                this.Close();
            }
        }
    }

    private void CancelButton_OnClick(object sender, RoutedEventArgs e)
    {
        _result = MessageBoxResult.Cancel;
        this.Close();
    }

    private void TextBlockCenterBox_OnSizeChanged(object sender, SizeChangedEventArgs e)
    {
        double singleLineHeight = TextBlockCenterBox.FontSize * 1.4;

        if (TextBlockCenterBox.ActualHeight > singleLineHeight)
        {
            // 发生换行
            this.Height += 40;
        }
        
    }

    private void TextBoxInput_OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter) // 检测是否按下回车键
        {
            ConfirmButton_OnClick(sender, e); // 调用按钮的点击事件
        }
    }
}