using System.Collections.Generic;

namespace WS
{
    ///<summary>Model管理</summary>
    public class ModelManager : WSComponent
    {
        ///<summary>Model容器</summary>
        private List<Model> _AllModel = new List<Model>();

        ///<summary>注册/创建Model</summary>
        public T CreateModel<T>() where T : Model
        {
            T model = ComponentFactory.Create<T>();
            model.InitProperty();
            _AllModel.Add(model);
            return model;
        }

        ///<summary>删除Model</summary>
        public void Remove(Model model)
        {
            if (_AllModel.Contains(model))
            {
                model.Dispose();
                _AllModel.Remove(model);
            }
        }

        ///<summary>删除所有Model</summary>
        public void RemoveAll()
        {
            for (int i = 0; i < _AllModel.Count; i++)
            {
                _AllModel[i]?.Dispose();
            }
            _AllModel.Clear();
        }

        public override void Dispose()
        {
            base.Dispose();
            RemoveAll();
        }
    }
}