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
    {
        PasswordEditViewModel viewModel;
        PasswordServiceClient mPasswordService;
        public Window_PasswordEdit()
        {
            InitializeComponent();
        }
        public void Init(IPEndPoint serverep,UInt32 id)
        {
            viewModel = new PasswordEditViewModel();
            
            mPasswordService = new PasswordServiceClient(id);
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mPasswordService.ID);
            viewModel.Init(mPasswordService);
            this.DataContext = viewModel;
        }
        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 (viewModel.Apply())
                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;

        #endregion

    }
    public class PasswordEditViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// 第1次输入
        /// </summary>
        public string PSK { get; set; }
        /// <summary>
        /// 重复输入
        /// </summary>
        public string PSK2 { get; set; }

        public int Level { get; set; }

        public int[] Levels { get; } = new int[] { 0, 1, 2 };
        PasswordServiceClient mPasswordService = null;


        public void Init(PasswordServiceClient passwordService)
        {
            mPasswordService = passwordService;
        }

        public bool Apply()
        {
            if (string.IsNullOrEmpty(PSK))
            {
                FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码为空",
                    TimeSpan.FromSeconds(2));
                return false;
            }
            else if (PSK != PSK2)
            {
                FLY.ControlLibrary.Window_WarningTip.Show("错误", "两次输入不相同",
                         TimeSpan.FromSeconds(2));
                return false;
            }
            else if (mPasswordService.PWs.Exists(pw => pw.Password == PSK))
            {
                //此密码已经存在
                //把它改级别
                PasswordCell pc = mPasswordService.PWs.Find(pw => pw.Password == PSK);
                if (pc != null)
                    pc.Level = Level;
                mPasswordService.Apply();
                return true;
            }
            else
            {
                
                //删除全部level=1的密码
                mPasswordService.PWs.RemoveAll(pw => pw.Level == Level);
                mPasswordService.PWs.Add(new PasswordCell() { Password = PSK, Level = Level });
                mPasswordService.Apply();
            }
            return true;
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
