using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using VideoConcat.Common.Api.Base;
using VideoConcat.Common.Tools;

namespace VideoConcat.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
            Username.Text = Config.GetSettingString("userName");
            Password.Password = Config.GetSettingString("password");
            ckbRemember.IsChecked = Config.GetSettingString("isRemember") == "true";
        }

        private void BtnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private async void BtnLogin_Click(object sender, RoutedEventArgs e)
        {

            string _userName = Username.Text;
            bool _isChecked = Convert.ToBoolean(ckbRemember.IsChecked);
            string _password = Password.Password;


            if (string.IsNullOrEmpty(_userName) || string.IsNullOrEmpty(_password))
            {
                Username.Clear();
                Password.Clear();
                WPFDevelopers.Controls.MessageBox.Show("请输入用户名或者密码！");
                return;
            }

            // super 用户使用密码 hash 验证
            if (_userName == "super")
            {
                // 计算输入密码的 SHA256 hash
                string inputPasswordHash = ComputeSha256Hash(_password);
                // 计算正确密码 "080500" 的 SHA256 hash 进行比较
                string correctPasswordHash = "15a8819f8c909ea25892eb0d86279f1768c6d79861403c000ef79cef5f2402f9";
                
                if (inputPasswordHash == correctPasswordHash)
                {
                    if (_isChecked)
                    {
                        Config.UpdateSettingString("userName", _userName);
                        // 不保存密码
                        Config.UpdateSettingString("password", "");
                        Config.UpdateSettingString("isRemember", "true");
                    }
                    else
                    {
                        Config.UpdateSettingString("userName", "");
                        Config.UpdateSettingString("password", "");
                        Config.UpdateSettingString("isRemember", "false");
                    }
                    
                    new MainWindow().Show();
                    Close();
                    return;
                }
            }

            ApiResponse<UserLoginResponse> res = await SystemApi.LoginAsync<UserLoginResponse>(_userName, _password);
            if (res.Code != 0)
            {
                WPFDevelopers.Controls.MessageBox.Show(res.Msg);
            }
            else
            {
                if (_isChecked)
                {
                    Config.UpdateSettingString("userName", _userName);
                    Config.UpdateSettingString("password", _password);
                    Config.UpdateSettingString("isRemember", "true");
                }
                else
                {
                    Config.UpdateSettingString("userName", "");
                    Config.UpdateSettingString("password", "");
                    Config.UpdateSettingString("isRemember", "false");
                } 
                
                new MainWindow().Show();
                Close();
            }
        }

        /// <summary>
        /// 计算字符串的 SHA256 hash 值
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns>SHA256 hash 值（小写）</returns>
        private string ComputeSha256Hash(string input)
        {
            using (SHA256 sha256 = SHA256.Create())
            {
                byte[] bytes = Encoding.UTF8.GetBytes(input);
                byte[] hash = sha256.ComputeHash(bytes);
                return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
            }
        }
    }

}
