using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Tool.ReadChecker
{
    public class Poetry_SayChecker
    {
        [MenuItem("Tool/ReadChecker/Poetry/CheckSay")]
        public static void CheckeSayModel()
        {
            var moduleRule = CreateRule();
            ReadModuleChecker readAssetChecker = new(moduleRule);
            readAssetChecker.CheckModelByRule();
        }

        private static List<RequireRule> CreateSrcRequires()
        {
            List < RequireRule > requireRules = new()
            {
                new RequireRule("",null,null,null,CheckPoetrySay)
            };
            return requireRules;
        }

        private static void CheckPoetrySay(string path, ReadModuleChecker checker)
        {
            {
                List<string> mustHaveFiles = new()
                {
                    Path.Combine(path, "rootreslink.asset"),
                    Path.Combine(path, "rootreslink.lua")
                };
                checker.CheckFilesExists(mustHaveFiles);
            }

            {
                //这里额外检测下poetrys文件夹下文件
                string poetryFolderPath = Path.Combine(Application.dataPath, "gamesrc/poetry/poetries", $"{checker.CurId}");
                if (!Directory.Exists(poetryFolderPath))
                {
                    checker.Logger.AddFolderMissingError(poetryFolderPath);
                    return;
                }

                string configPath = Path.Combine(poetryFolderPath, "config");
                if (!Directory.Exists(configPath))
                {
                    checker.Logger.AddFolderMissingError(configPath);
                    return;
                }

                List<string> mustHaveFiles = new()
                {
                    Path.Combine(configPath, "BaseConfig.json"),
                    Path.Combine(configPath, "BaseConfigreslink.asset"),
                    Path.Combine(configPath, "BaseConfigreslink.lua"),
                };
                checker.CheckFilesExists(mustHaveFiles);
            }
        }

        private static List<RequireRule> CreateArtRequires()
        {
            List<RequireRule> requireRules = new()
            {
                new RequireRule("config",
                   new HashSet<string> { ".json" },
                   new HashSet<string> { @"^BaseConfig$" },null,
                   CheckConfig),

                new RequireRule("image",
                  new HashSet<string> { ".png" },
                  new HashSet<string> { @"^\d{4}$" }),

                new RequireRule("sound",
                  new HashSet<string> { ".ogg" },
                  new HashSet<string> { @"^poetry\d{1}", @"^poetryAuthor$", @"^poetryTitle$" }),

                new RequireRule("",
                  null, null,
                  new HashSet<string> { "config", "image", "sound" }
              )
            };
            return requireRules;
        }

        private static void CheckConfig(string path, ReadModuleChecker checker)
        {
            string mustHaveFile = Path.Combine(path, "BaseConfig.json");
            checker.CheckFileExists(mustHaveFile);
        }

        public static ModuleRule CreateRule()
        {
            var moduleRule = new ModuleRule("art/game/gushi/say", @"^\d{4}$","gamesrc/poetry/say")
            {
                ArtRequireList = CreateArtRequires(),
                SrcRequireList = CreateSrcRequires()
            };
            return moduleRule;
        }
    }
}