//using BestHTTP.JSON;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace PublicTools.XMLDataBase
{
    /// <summary>
    /// 表格代理
    /// 一个表格的控制器，支持多表同时编辑，提高IO效率
    /// </summary>
    public class XTable : TableInterface
    {
        /// <summary>
        /// 绝对路径
        /// </summary>
        private TableInterface table;
        public XTable(int type)
        {
            if (type == 0)
            {
                table = new XMLDataEditor();
            }
            else if (type == 1)
            {
                //table = new JsonDataEditor();
            }
        }
        public XTable(string path)
        {
            //Open(path);
            string exten = Path.GetExtension(path);
            if (exten == ".xml")
            {
                table = new XMLDataEditor(path);
            }
            else if (exten == ".json")
            {
                //table = new JsonDataEditor(path);
            }
            if (table == null)
            {
                UnityEngine.Debug.LogError("Table is null!" + path);
            }
        }
        public XTable(DataType dataType)
        {
            //Open(path);
            if (dataType == DataType.Xml)
            {
                table = new XMLDataEditor();
            }
            else if (dataType == DataType.Json)
            {
                //table = new JsonDataEditor();
            }
            if (table == null)
            {
                UnityEngine.Debug.LogError("Table is null!" + dataType);
            }
        }

        public XTable()
        {
        }

        /// <summary>
        /// 打开表格
        /// 支持xml，json文件的处理
        /// 后续可能支持sqlite或者其他处理方式
        /// </summary>
        /// <param name="path"></param>
        public bool Open()
        {
            return table.Open();
        }
        /// <summary>
        /// 打开表格
        /// 支持xml，json文件的处理
        /// 后续可能支持sqlite或者其他处理方式
        /// </summary>
        /// <param name="path"></param>
        public bool Open(string path)
        {
            return table.Open(path);
        }
        /// <summary>
        /// 关闭操作
        /// </summary>
        public void Close()
        {
            table.Close();
        }

        /// <summary>
        /// 插入一个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public bool InsertData<T>(T data)
        {
            if (data != null)
            {
                return table.InsertData(data);
            }
            return false;
        }
        /// <summary>
        /// 查找对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T FindData<T>(string key)
        {
            return table.FindData<T>(key);
        }
        /// <summary>
        /// 查找所有对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> FindAllData<T>()
        {
            return table.FindAllData<T>();
        }
        /// <summary>
        /// 查找所有对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> FindAllData<T>(string data)
        {
            return table.FindAllData<T>(data);
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public bool UpdateData<T>(T data)
        {
            if (data != null)
            {
                return table.UpdateData(data);
            }
            return false;
        }
        /// <summary>
        /// 删除元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="suffix">默认使用xml，可以是其他文件如json</param>
        /// <returns></returns>
        public bool DeleteData<T>(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                return table.DeleteData<T>(key);
            }
            return false;
        }
        /// <summary>
        /// 删除所有元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public bool DeleteAllData<T>()
        {
            return table.DeleteAllData<T>();
        }

        public bool Create(string tableName)
        {
            return table.Create(tableName);
        }

        public void Save()
        {
            table.Save();
        }

        public List<T> FindAllData<T>(byte[] data)
        {
            return table.FindAllData<T>(data);
        }

        public bool InsertData<T>(T data, string priKey)
        {
            return table.InsertData<T>(data,priKey);
        }

        public bool UpdateData<T>(T data, string key)
        {
            return table.InsertData<T>(data,key);
        }
    }
}