using CommunityToolkit.Mvvm.Input;
using FLY.Thick.Base.Common;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

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))
                {
                    string tit = (string)Application.Current.TryFindResource("str.PgPwManager.FormatErr");
                    string msg = (string)Application.Current.TryFindResource("str.PgPwManager.PwIsNull");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, msg);
                    return;
                }
                pw.Password = pw.Password.Trim();

                if (pw.Level < 0 || pw.Level > 10)
                {
                    string tit = (string)Application.Current.TryFindResource("str.PgPwManager.FormatErr");
                    string msg = (string)Application.Current.TryFindResource("str.PgPwManager.LvIsOverrange");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, msg);
                    return;
                }

                if ((from _pw in Pws where _pw.Password == pw.Password select _pw).Count() > 1)
                {
                    string tit = (string)Application.Current.TryFindResource("str.PgPwManager.FormatErr");
                    string msg = (string)Application.Current.TryFindResource("str.PgPwManager.PwIsDuplication");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, msg);
                    return;
                }
            }
            foreach (var c in UiLvs)
            {
                if (c.Level < 0 || c.Level > 10)
                {
                    string tit = (string)Application.Current.TryFindResource("str.PgPwManager.FormatErr");
                    string msg = (string)Application.Current.TryFindResource("str.PgPwManager.LvIsOverrange");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, msg);
                    return;
                }
            }

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

            this.passwordAuthorize.Apply(p);
            {
                string tit = (string)Application.Current.TryFindResource("str.PgPwManager.ApplySuccessfully");
                string format = (string)Application.Current.TryFindResource("str.PgPwManager.TotalNPw");
                string msg = string.Format(format, Pws.Count());
                FLY.ControlLibrary.Window_Tip.Show(tit, msg, TimeSpan.FromSeconds(2));
            }
            return;

        }

    }

}
