using Assets.app.Editor.tools.assetCopyer;
using Assets.Native.tools.excel.Model;
using Assets.Native.tools.excel.Util;
using ClosedXML.Excel;
using CommonUtil;
using EditorUtils;
using iHuman.UnitySz.Framework;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using static iHuman.UnitySz.Framework.ResLinkAsset;
using Assets.app.Editor.tools.PoetryConfig.common;

namespace Assets.app.Editor.tools.PoetryConfig.common {
    public class BaseConfigGenNew
    {

        private const string configSvnPath = "https://10.3.254.33/repos/hongen_read/product/周课/配置表/周课-路-带拼音.xlsx";
        const string configPathRoot = "Assets/gamesrc/poetry/poetries";

        [MenuItem("Assets/自定义创建/古诗/common/GenBaseConfig")]
        public static void GenBaseConfig() {
            List<BaseConfigModel> modelList = GenBaseConfigModelList();
            for (int i = 0; i < modelList.Count; i++) {
                CreateBaseConfig(modelList[i]);
            }
        }

        [MenuItem("Assets/自定义创建/古诗/common/GenBaseConfigReslink")]
        public static void GenBaseConfigReslink() {
            List<BaseConfigModel> modelList = GenBaseConfigModelList();
            for (int i = 0; i < modelList.Count; i++) {
                CreateBaseConfigReslink(modelList[i]);
            }
        }

        // 生成Baseconfi.json
        private static void CreateBaseConfig(BaseConfigModel baseConfigModel)
        {
            string id = baseConfigModel.ReadId;
            string configFolder = Path.Combine(configPathRoot, @$"{id}", "config");
            if (!Directory.Exists(configFolder)) {Directory.CreateDirectory(configFolder);}
            string configJsonName = $"BaseConfig.json";
            string jsonOutPutPath = Path.Combine(configFolder, configJsonName);

            string result = JsonConvert.SerializeObject(baseConfigModel, Formatting.Indented);
            File.WriteAllText(jsonOutPutPath, result);
        }

        // 生成Reslink对应 Baseconfi.json 
        private static void CreateBaseConfigReslink(BaseConfigModel baseConfigModel)
        {
            string id = baseConfigModel.ReadId;
            // 生成存储Baseconfig.json路径的reslink
            String configFolder = Path.Join(configPathRoot, id, "config");
            if (!Directory.Exists(configFolder)) {Directory.CreateDirectory(configFolder);}
            String resultPath = Path.Join(configFolder,"BaseConfig.json");
            String resLinkName = "baseconfigreslink.asset";
            String resLinkPath = Path.Combine(configFolder,resLinkName);
            ResLinkAsset resLinkAsset = ScriptableObject.CreateInstance<ResLinkAsset>();
            var obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(resultPath);
            var item = new ResLinkAsset.Item{Key = "BASECONFIG",Val = obj};
            resLinkAsset.assets.Add(item);
            AssetDatabase.CreateAsset(resLinkAsset,resLinkPath);
            ResLinkEditor.Save(resLinkAsset);
        }

        public static List<BaseConfigModel> GenBaseConfigModelList()
        {
            string excelFile = SvnUtil.getFile(configSvnPath);
            List<BaseConfigModel> modelList = ExcelUtil.ReadExcelWithReflection<BaseConfigModel>(excelFile, "古诗");
            Dictionary<string, string> weeklyReadIDDict = getWeeklyReadIDDict();
            for (int i = 0; i < modelList.Count; i++) {
                BaseConfigModel model = modelList[i];
                string weeklyId = model.WeeklyId;
                if (!weeklyReadIDDict.ContainsKey(weeklyId)) continue;
                string readId = weeklyReadIDDict[model.WeeklyId];
                model.ReadId = readId;
                model.init();
            }
            return modelList;
        }

        private static Dictionary<string, string> getWeeklyReadIDDict() {
            string svnPath = "https://10.3.254.33/repos/hongen_read/product/配置/古诗/古诗基础配置.xlsx";
            string excel = SvnUtil.getFile(svnPath);
            var content = ExcelUtil.ReadExcelNoReflection(excel)[0];
            String resultDirRoot = "Assets/gamesrc/poetry/common/constant";
            Dictionary<string, string> weeklyReadIDDict = new Dictionary<string, string>();
            foreach (var row in content) {
                if (row["read_id"] == "") {
                    continue;
                }
                var weeklyId = row["weekly_id"]; 
                var readId = row["read_id"]; 
                weeklyReadIDDict[weeklyId] = readId;
            }
            return weeklyReadIDDict;
        }
    }
}