using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Zerolauncher.Manager;

namespace Zerolauncher.dialog
{
    /// <summary>
    /// EditMember.xaml 的交互逻辑
    /// </summary>
    public partial class EditMember : UserControl
    {

        private Dictionary<int, string> _services;

        public EditMember()
        {
            _services = new Dictionary<int, string>();
            int i = 0;
            foreach (string serviceName in ServicesStaticInfo.ServicesName)
            {
                _services[i++] = serviceName;
            }
            InitializeComponent();
            cb_pid.ItemsSource = _services;
            cb_sid.ItemsSource = ServicesStaticInfo.ServerNames;

			if (EditMemberDialog.member != null)
            {
                int index = (int)EditMemberDialog.member;
                var acc = AccountManager.accountsList[index];
				cb_pid.SelectedIndex = acc.providerId;
                cb_sid.SelectedIndex = acc.serverId;
                edit_acc.Text = acc.userName;
                edit_pwd.Password = acc.userPWD;
                edit_nick.Text = acc.nickName;
            }

			cb_pid.SelectedIndex = 0;
			cb_pid.IsEditable = false;
			
		}

        private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;
            VisualBrush helpBrush = passwordBox.Resources["HelpBrush"] as VisualBrush;
            if (passwordBox.Password.Length > 0)
            {
                passwordBox.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
            }
            else
            {
                passwordBox.Background = helpBrush;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(cb_pid.Text) || cb_sid.SelectedIndex == 0 || string.IsNullOrEmpty(edit_acc.Text) || string.IsNullOrEmpty(edit_pwd.Password) || string.IsNullOrEmpty(edit_nick.Text))
            {
                MessageBox.Show("所有选项均不能为空！", "提示");
                return;
            }
            if (EditMemberDialog.member == null)
            {
                AccountManager.AddAccount(new AccountNew { providerId = cb_pid.SelectedIndex, serverId = cb_sid.SelectedIndex, userName = edit_acc.Text, userPWD = edit_pwd.Password, nickName = edit_nick.Text });

                EditMemberDialog.Close();
                return;
            }
            int index = (int)EditMemberDialog.member;
            var acc = AccountManager.accountsList[index];
            acc.providerId = cb_pid.SelectedIndex;
            acc.serverId = cb_sid.SelectedIndex;
            acc.userName = edit_acc.Text;
            acc.userPWD = edit_pwd.Password;
            acc.nickName = edit_nick.Text;
            AccountManager.saveEdit();
            EditMemberDialog.Close();
            return;
        }
    }
}

