using Common.system;

using System;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;

//using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;

using XHWK.WKTool.DAL;

namespace XHWK.WKTool
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        /// <summary>
        /// 调用接口
        /// </summary>
        private readonly XHApi xhapi = new XHApi();

        public LoginWindow()
        {
            InitializeComponent();

            Initialize();
        }

        public void Initialize()
        {
            if (FileToolsCommon.IsExistFile(App.DataPath + "accountNumber.txt"))
            {
                string accountNumber = System.IO.File.ReadAllText(App.DataPath + "accountNumber.txt", Encoding.Default);
                if (!string.IsNullOrWhiteSpace(accountNumber))
                {
                    TxbAccountNumber.Text = accountNumber;
                    CkSaveName.IsChecked = true;
                }
            }
            DataContext = App.PageContextData;
        }

        /// <summary>
        /// 关闭事件
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void BtnDown_Click(object sender, RoutedEventArgs e)
        {
            TxbAccountNumber.Text = string.Empty;
            pobPassword.Password = string.Empty;
            Hide();
        }

        /// <summary>
        /// 登录事件
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            #region 获取服务接口地址 --2021年7月9日赵耀

            if (App.ServiceAddress == null)
            {
                MessageWindow.Show("首次使用需设置服务地址。");
                BtnServiceAddress_Click(null, null);
                return;
            }

            #endregion 获取服务接口地址 --2021年7月9日赵耀

            if (string.IsNullOrEmpty(TxbAccountNumber.Text))
            {
                MessageWindow.Show("账号未输入");
                return;
            }
            if (TxbAccountNumber.Text.Length > 18)
            {
                MessageWindow.Show("账号长度不能高于18位");
                return;
            }
            if (string.IsNullOrEmpty(pobPassword.Password))
            {
                MessageWindow.Show("密码未输入");
                return;
            }
            if (pobPassword.Password.Length > 16)
            {
                MessageWindow.Show("密码长度不能高于16位");
                return;
            }
            App.myloading.Show();
            Login();
        }

        /// <summary>
        /// 登录
        /// </summary>
        private void Login()
        {
            string accountNumber = string.Empty;
            string password = string.Empty;
            accountNumber = TxbAccountNumber.Text.Replace(" ", "").Trim();
            password = pobPassword.Password.Replace(" ", "").Trim();

            int code = xhapi.Login(accountNumber, password);
            if (code == 0)
            {
                FileToolsCommon.CreateDirectory(App.DataPath);
                string applicationData = App.DataPath + "accountNumber.txt";
                if (CkSaveName.IsChecked == true)
                {
                    string accountNumbers = TxbAccountNumber.Text;
                    System.IO.File.WriteAllText
                    (
                        applicationData,
                        accountNumbers,
                        Encoding.Default
                    ); //存放账号
                }
                else
                {
                    string accountNumbers = "";
                    System.IO.File.WriteAllText
                    (
                        applicationData,
                        accountNumbers,
                        Encoding.Default
                    ); //存放账号
                }
                App.myloading.Hide();
                //UpdateSettingString("userName", txbAccountNumber.Text);
                App.IsLoginType = true;
                TxbAccountNumber.Text = string.Empty;
                pobPassword.Password = string.Empty;

                #region 下载头像

                string HeadpicPath = FileToolsCommon.GetFileAbsolutePath("/Data/" + App.UserInfo.Username + ".png");
                if (!string.IsNullOrWhiteSpace(App.UserInfo.Headpic))
                {
                    //FileToolsCommon.
                    xhapi.DownloadAvatar(App.UserInfo.Headpic, HeadpicPath);
                }

                #endregion 下载头像

                Hide();
            }
            else
            {
                App.IsLoginType = false;
                App.myloading.Hide();
                MessageWindow.Show(App.ServerMsg);
            }
        }

        /// <summary>
        /// 记住账号
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void CkSaveName_Click(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// 读取客户设置
        /// </summary>
        /// <param name="settingName">
        /// </param>
        /// <returns>
        /// </returns>
        public static string GetSettingString(string settingName)
        {
            try
            {
                string settingString = ConfigurationManager.AppSettings[settingName].ToString();
                return settingString;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 更新设置
        /// </summary>
        /// <param name="settingName">
        /// </param>
        /// <param name="valueName">
        /// </param>
        public static void UpdateSettingString(string settingName, string valueName)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (ConfigurationManager.AppSettings[settingName] != null)
            {
                config.AppSettings.Settings.Remove(settingName);
            }
            config.AppSettings.Settings.Add(settingName, valueName);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 限制输入特殊字符
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void txbAccountNumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Regex re = new Regex("[^0-9\\-]+");
                Regex re1 = new Regex("[^A-Za-z\\-]+");
                if (!re.IsMatch(e.Text))
                {
                    e.Handled = re.IsMatch(e.Text);
                }
                else if (!re1.IsMatch(e.Text))
                {
                    e.Handled = re1.IsMatch(e.Text);
                }
                else
                {
                    e.Handled = true;
                }
            }
            catch (Exception)
            {
            }
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        #region 服务地址

        /// <summary>
        /// 打开服务地址
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void BtnServiceAddress_Click(object sender, RoutedEventArgs e)
        {
            GridLoginPage.Visibility = Visibility.Collapsed;
            GridServiceAddress.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            GridLoginPage.Visibility = Visibility.Visible;
            GridServiceAddress.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 服务地址设置
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void BtnSetServiceAddress_Click(object sender, RoutedEventArgs e)
        {
            #region 获取服务地址 -- 2021年7月8日赵耀

            if (!string.IsNullOrWhiteSpace(TbxServiceAddress.Text))
            {
                if (xhapi.GetServiceAddress(TbxServiceAddress.Text, out string Message))
                {
                    App.SaveServiceAddressData();
                    App.SwitchAddress();
                    MessageWindow.Show("设置成功！");
                    BtnCancel_Click(null, null);
                }
                else
                {
                    MessageWindow.Show(Message);
                    return;
                }
            }

            #endregion 获取服务地址 -- 2021年7月8日赵耀
        }

        #region 切换校内外

        /// <summary>
        /// 校外
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void RbnIsOutsideSchool_Checked(object sender, RoutedEventArgs e)
        {
            App.IsOutsideSchool = true;
            FileToolsCommon.SetConfigValue("IsOutsideSchool", "1");
            if (App.ServiceAddress != null)
            {
                App.SwitchAddress();
            }
        }

        /// <summary>
        /// 校内
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void RbnSchool_Checked(object sender, RoutedEventArgs e)
        {
            App.IsOutsideSchool = false;
            FileToolsCommon.SetConfigValue("IsOutsideSchool", "0");
            if (App.ServiceAddress != null)
            {
                App.SwitchAddress();
            }
        }

        #endregion 切换校内外

        #endregion 服务地址
    }
}
