using Microsoft.Win32;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Zerolauncher.Manager;

namespace Zerolauncher.dialog
{
    /// <summary>
    /// UseAccDataTextAdd.xaml 的交互逻辑
    /// </summary>
    public partial class UseAccDataTextAdd : UserControl
    {
        public UseAccDataTextAdd()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //创建一个打开文件式的对话框
            OpenFileDialog ofd = new OpenFileDialog();
            //设置这个对话框的起始打开路径
            ofd.InitialDirectory = @"C:\";
            //设置打开的文件的类型，注意过滤器的语法
            ofd.Filter = "账号文本|*.txt|其他格式|*.";
            //调用ShowDialog()方法显示该对话框，该方法的返回值代表用户是否点击了确定按钮
            if (ofd.ShowDialog() == true)
            {
                string[] lines = File.ReadAllLines(ofd.FileName, Encoding.UTF8);
                for (int i = 0; i < lines.Length; i++)
                {
                    var accTexts = lines[i].Split(input_split.Text);
                    if (accTexts.Length != 5)
                    {
                        MessageBox.Show($"文本在{i}行非法！\n{lines[i]}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    var acc = new AccountNew { };
                    if (!int.TryParse(accTexts[0], out acc.providerId))
                    {
                        MessageBox.Show($"输入的服务商代号错误！在{i}行\n错误：[{lines[i]}]无法转换成服务器代号", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    if(0 < acc.providerId || acc.providerId > ServicesStaticInfo.ServicesName.Length) 
                    {
                        MessageBox.Show($"输入的服务商代号错误！在{i}行\n错误：[{acc.providerId}]不是有效的服务器代号", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
					if (!int.TryParse(accTexts[1], out acc.serverId))
					{
						MessageBox.Show($"输入的服务器代号错误！在{i}行\n错误：[{lines[i]}]无法转换成服务器代号", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
						return;
					}
					if (0 < acc.providerId || acc.providerId > ServicesStaticInfo.ServerIds.Length)
					{
						MessageBox.Show($"输入的服务器代号错误！在{i}行\n错误：[{acc.providerId}]不是有效的服务器代号", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
						return;
					}
                    acc.userName = accTexts[2];
                    acc.userPWD = accTexts[3];
                    acc.nickName = accTexts[4];
                    if (!AccountManager.AddAccounts(acc))
                    {
                        MessageBox.Show($"文本在{i}行出错！\n警告：[{acc.nickName}]昵称冲突，将跳过添加此账号", "Warring", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                MessageBox.Show("添加完成！", "提示");
                AccountManager.saveEdit();
                AddMemebersDialog.Close();
            }
            else
            {
                MessageBox.Show("没有选择文件");
            }
        }
    }
}
