using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using FLY.Thick.Base.Client;
using System.Net;
using FLY.Thick.Base.Common;

namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Window_PasswordEdit.xaml 的交互逻辑
    /// </summary>
    public partial class Window_PasswordEdit : Window, INotifyPropertyChanged
    {
        PasswordService mPasswordService = null;
        private string psk;
        public string PSK
        {
            get
            {
                return psk;
            }
            set
            {
                psk = value;
                NotifyPropertyChanged("PSK");
            }
        }
        private string psk2;
        public string PSK2
        {
            get
            {
                return psk2;
            }
            set
            {
                psk2 = value;
                NotifyPropertyChanged("PSK2");
            }
        }
        public Window_PasswordEdit()
        {
            InitializeComponent();
        }
        public void Init(IPEndPoint serverep) 
        {
            mPasswordService = new PasswordService();
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mPasswordService.ID);
            this.DataContext = this;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            mPasswordService.Dispose();
        }
        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(PSK))
            {
                FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码为空",
                    TimeSpan.FromSeconds(2));
                return;
            }
            else if (PSK != PSK2)
            {
                FLY.ControlLibrary.Window_WarningTip.Show("错误", "两次输入不相同",
                         TimeSpan.FromSeconds(2));
                return;
            }
            else if(mPasswordService.PWs.Exists(pw=>pw.Password == PSK))
            {
                FLY.ControlLibrary.Window_WarningTip.Show("错误", "此密码已经存在",
                     TimeSpan.FromSeconds(2));
                            return;
            }
            else 
            {
                //删除全部level=1的密码
                mPasswordService.PWs.RemoveAll(pw => pw.Level <= 1);
                mPasswordService.PWs.Add(new PasswordCell() { Password = PSK, Level = 1 });
                mPasswordService.Apply();
            }
            this.Close();
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion

    }
}
