﻿using System;
using UnityEngine;

namespace JLGames.RocketDriver.Actions.ExcelExporter
{
    /// <summary>
    /// Used to explain the json data file generated by the tool "ExcelExporter"
    /// The tool link is: https://github.com/xuzhuoxi/ExcelExporter
    /// 用于解释工具《ExcelExporter》生成的json数据文件
    /// 工具链接为：https://github.com/xuzhuoxi/ExcelExporter
    /// </summary>
    public static class DataParsingUtil
    {
        [Serializable]
        public class RawDataTable<TCfgTitle>
        {
            public int count;
            public TCfgTitle[] data;
        }

        internal static class JsonParser
        {
            public static RawDataTable<TCfgTitle> ParseConfig<TCfgTitle>(string context)
            {
                var config = JsonUtility.FromJson<RawDataTable<TCfgTitle>>(context);
                return config;
            }

            public static TCfg[] ParseConfigArray<TCfg>(string content)
            {
                return ParseConfig<TCfg>(content).data;
            }
        }

        /// <summary>
        /// Parse into object
        /// 解释成结构
        /// </summary>
        /// <param name="context"></param>
        /// <typeparam name="TCfgTitle"></typeparam>
        /// <returns></returns>
        public static RawDataTable<TCfgTitle> ParseConfig<TCfgTitle>(string context)
        {
            return JsonParser.ParseConfig<TCfgTitle>(context);
        }

        /// <summary>
        /// Parse into object array
        /// 解释成数组
        /// </summary>
        /// <param name="content"></param>
        /// <typeparam name="TCfgTitle"></typeparam>
        /// <returns></returns>
        public static TCfgTitle[] ParseConfigArray<TCfgTitle>(string content)
        {
            return JsonParser.ParseConfigArray<TCfgTitle>(content);
        }
    }
}