using PublicTools.XMLDataBase;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XRTool.Util;
namespace XRTool.UI
{
    /// <summary>
    /// 数据访问单例
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DataConfMgr<T> : Singleton<DataConfMgr<T>>
    {
        private TableHelper<T> tableHelper;
        public TableHelper<T> TableHelper { get => tableHelper; set => tableHelper = value; }
        public bool IsInit()
        {
            if (TableHelper == null)
            {
                return false;
            }
            return TableHelper.isInit;
        }
        /// <summary>
        /// 按照主键打开表格
        /// </summary>
        /// <param name="priKey"></param>
        public void OpenTable(string priKey = null)
        {
            TableHelper.Open(priKey);
        }
        /// <summary>
        /// 打开某路径下的资源
        /// </summary>
        /// <param name="path"></param>
        public void OpenData(string path, bool isForce = false)
        {
            if (TableHelper == null || isForce)
            {
                if (TableHelper != null)
                {
                    TableHelper.Reset(!isForce);
                }
                TableHelper = new TableHelper<T>(path);
            }
        }
        /// <summary>
        /// 清除所有
        /// </summary>
        public void ClearAll()
        {
            TableHelper?.DelAll();
        }
        /// <summary>
        /// 删除某一数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isSave"></param>
        public void DelConf(string key, bool isSave = true)
        {
            TableHelper?.DelConf(key, isSave);
        }
        /// <summary>
        /// 按照指定的key值查找
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get(string key)
        {
            if (TableHelper == null)
            {
                return default(T);
            }
            return TableHelper.FindData(key);
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public List<T> GetAll()
        {
            if (TableHelper == null)
            {
                return null;
            }
            return TableHelper.DataList;
        }
        /// <summary>
        /// 加入一条数据
        /// </summary>
        /// <param name="t"></param>
        /// <param name="isSave"></param>
        public void Insert(T t, bool isSave = true)
        {
            TableHelper.AddData(t, isSave);
        }
        public void Close()
        {
            TableHelper.Close();
        }
        public void Save()
        {
            TableHelper.SaveData();
        }
    }
}