using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// PgPwManager.xaml 的交互逻辑
    /// </summary>
    public partial class PgPwManager : Page
    {
        PgPwManagerVm viewModel;
        public PgPwManager()
        {
            InitializeComponent();

        }
        [Unity.InjectionMethod]
        public void Init(PasswordAuthorize passwordAuthorize) 
        {
            viewModel = new PgPwManagerVm();
            viewModel.Init(passwordAuthorize);
            this.DataContext = viewModel;
        }
    }
    public class PgPwManagerVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<PasswordCell> Pws { get; } = new ObservableCollection<PasswordCell>();
        public ObservableCollection<UiLvCell> UiLvs { get; } = new ObservableCollection<UiLvCell>();
        public RelayCommand ApplyCmd { get; }

        PasswordAuthorize passwordAuthorize;
        public PgPwManagerVm() 
        {
            ApplyCmd = new RelayCommand(Apply);
        }


        public void Init(PasswordAuthorize passwordAuthorize)
        {
            this.passwordAuthorize = passwordAuthorize;
            this.passwordAuthorize.PropertyChanged += PasswordAuthorize_PropertyChanged;
            update_list();

        }
        void update_list() 
        {
            Pws.Clear();
            UiLvs.Clear();
            if (this.passwordAuthorize.Db.Pws != null)
            {
                
                foreach (var c in this.passwordAuthorize.Db.Pws)
                {
                    Pws.Add(new PasswordCell() { Password = c.Password, Level = c.Level, Description = c.Description });
                }
            }
            if (this.passwordAuthorize.Db.UiLvs != null)
            {
                
                foreach (var c in this.passwordAuthorize.Db.UiLvs)
                {
                    UiLvs.Add(new UiLvCell() { UiName = c.UiName, Description = c.Description, Level = c.Level });
                }
            }
        }
        private void PasswordAuthorize_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Db") 
            {
                update_list();
            }
        }

        private void Apply()
        {
            //检测
            foreach (var pw in Pws)
            {
                if (string.IsNullOrEmpty(pw.Password)) 
                {
                    FLY.ControlLibrary.Window_WarningTip.Show(
                        "格式错误",
                        $"不能有空密码");
                    return;
                }
                pw.Password = pw.Password.Trim();

                if (pw.Level < 0 || pw.Level > 10)
                {
                    FLY.ControlLibrary.Window_WarningTip.Show(
                        "格式错误",
                        $"{pw.Password} 级别 应该在 0-10 之间");
                    return;
                }

                if ((from _pw in Pws where _pw.Password == pw.Password select _pw).Count() > 1) 
                {
                    FLY.ControlLibrary.Window_WarningTip.Show(
                    "格式错误",
                    $"{pw.Password} 重复");
                    return;
                }
            }
            foreach(var c in UiLvs)
            {
                if (c.Level < 0 || c.Level > 10)
                {
                    FLY.ControlLibrary.Window_WarningTip.Show(
                        "格式错误",
                        $"{c.UiName} 级别 应该在 0-10 之间");
                    return;
                }
            }

            var p = new PasswordJsonDb();
            p.Pws.AddRange(Pws);
            p.UiLvs.AddRange(UiLvs);

            this.passwordAuthorize.Apply(p);

            FLY.ControlLibrary.Window_Tip.ShowShortTime(
                "修改成功",
                $"一共 {Pws.Count()}个密码 保存成功");
            return;

        }

    }

}
