using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

namespace MultiLayout.UiModule
{
    /// <summary>
    /// 
    /// </summary>
    public class UIModuleParam : INotifyPropertyChanged
    {
        /// <summary>
        /// 控件唯一标示
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        /// <summary>
        /// 
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class UIModuleParams<T> : INotifyPropertyChanged
        where T : UIModuleParam
    {

        /// <summary>
        /// 
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string FilePath;
        /// <summary>
        /// 
        /// </summary>
        public List<T> Items { get; set; } = new List<T>();

        /// <summary>
        /// 只保存 IDs 的控件
        /// </summary>
        /// <param name="IDs"></param>
        public virtual void MatchParam(int[] IDs)
        {
            if (Items.RemoveAll(i => { return !IDs.Contains(i.ID); }) > 0)
            {
                Save();
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public virtual bool Save()
        {
            try
            {
                string dirPath = System.IO.Path.GetDirectoryName(FilePath);
                if (!System.IO.Directory.Exists(dirPath))
                {
                    System.IO.Directory.CreateDirectory(dirPath);
                }


                string json = Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);

                File.WriteAllText(FilePath, json);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 读取
        /// </summary>
        /// <returns></returns>
        public virtual bool Load()
        {
            try
            {
                string json = File.ReadAllText(FilePath);
                Newtonsoft.Json.JsonConvert.PopulateObject(json, this);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        protected void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        /// <summary>
        /// 
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
