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

namespace ThickTcpUiInWindow.UIModule
{
    public class UIModuleParam : INotifyPropertyChanged, Misc.ISaveToXml
    {
        private int id = 0;
        /// <summary>
        /// 控件唯一标示
        /// </summary>
        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                if (id != value)
                {
                    id = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

        protected void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public virtual string[] GetSavePropertyNames()
        {
            return new string[]{
                "ID"
            };
        }
    }
    public abstract class UIModuleParams<T> : INotifyPropertyChanged, Misc.ISaveToXml
        where T : UIModuleParam
    {
        public string FilePath;
        private List<T> items = new List<T>();
        /// <summary>
        /// 
        /// </summary>
        public List<T> Items
        {
            get
            {
                return items;
            }
        }

        static UIModuleParams()
        {
            Misc.SaveToXmlHepler.Regist(typeof(T));
        }
        public void MatchParam(int[] IDs)
        {
            if (Items.RemoveAll(i => { return !IDs.Contains(i.ID); }) > 0)
            {
                Save();
            }
        }
        
        public virtual bool Save()
        {
            return Misc.SaveToXmlHepler.Save(FilePath, this);
        }
        public virtual bool Load()
        {
            return Misc.SaveToXmlHepler.Load(FilePath, this);
        }

        protected void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public virtual string[] GetSavePropertyNames()
        {
            return new string[]{
                "Items"
            };
        }
    }
}
