using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.Common;
using System.ComponentModel;
using Newtonsoft.Json;
using System.IO;

namespace FLY.Thick.Base.Server
{
    [JsonObject(MemberSerialization.OptIn)]
    public class Password : IPasswordService, INotifyPropertyChanged
    {
        /// <summary>
        /// 密码列表。 储存在 服务器
        /// </summary>
        [JsonProperty]
        public List<PasswordCell> PWs { get; set; } = new List<PasswordCell>();

        [JsonProperty]
        public List<UiLvCell> UiLvs { get; set; } = new List<UiLvCell>();

        private string param_path = "password.json";
        public Password() 
        {
            

        }
        public Password(string param_path) 
        {
            if (!string.IsNullOrEmpty(param_path))
                this.param_path = param_path;
            Load();
        }

        public void Apply()
        {
            Save();
            NotifyPropertyChanged("PWs");
            NotifyPropertyChanged("UiLvs");
        }
        void Save() 
        {
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(this);
            try
            {
                File.WriteAllText(param_path, json);
            }
            catch
            {
            
            }
        }

        void Load()
        {
            if (!File.Exists(param_path)) {
                return;
            }
            try
            {
                string json = File.ReadAllText(param_path);
                Newtonsoft.Json.JsonConvert.PopulateObject(json, this);
            }
            catch { 
            
            }
        }
        #region INotifyPropertyChanged 成员

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

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

        #endregion
    }


}
