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

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

        private static List<RequireRule> CreateArtRequires()
        {
            List<RequireRule> requireRules = new()
            {
                new RequireRule("font",
                    new HashSet<string> { ".asset" },
                    new HashSet<string> { @"^\d{4}_laba$" },null,
                    CheckFont),

                new RequireRule("json",
                   new HashSet<string> { ".txt" },
                   new HashSet<string> { @"^V\d{2}$" },null),
                   //CheckJson),

                new RequireRule("sound",
                   new HashSet<string> { ".ogg" },
                   new HashSet<string> { @"^BGM", @"^A\d{2}$", @"^V\d{2}$", @"^S\d{2}$" }),

                new RequireRule("timeline",
                   new HashSet<string> { ".playable" },null,null,
                   CheckTimeLine),

                new RequireRule("spine",
                   new HashSet<string> { ".png", ".json", ".txt", ".asset", ".mat", ".atlas" }),

                new RequireRule("",
                   new HashSet<string> { ".unity", ".prefab" },
                   new HashSet<string> { @"^V01$", @"^say\d{4}" },
                   new HashSet<string> { "font", "json", "sound", "timeline", "spine", "backdrop", "effects" },
                   CheckArtRoot)
            };
            return requireRules;
        }

        private static void CheckArtRoot(string path, ReadModuleChecker checker)
        {
            List<string> mustHaveFiles = new()
            {
                Path.Combine(path, string.Format("say{0}.unity", checker.CurId)),
                Path.Combine(path, "V01.prefab"),
            };
            checker.CheckFilesExists(mustHaveFiles);
        }

        private static void CheckTimeLine(string path, ReadModuleChecker checker)
        {
            string file = Path.Combine(path, "V01Timeline.playable");
            checker.CheckFileExists(file);
        }

        private static void CheckJson(string path, ReadModuleChecker checker)
        {
            var mustHaveFiles = Enumerable.Range(1, 10).Select(i => Path.Combine(path, string.Format("V{0:D2}.txt", i))).ToList();
            checker.CheckFilesExists(mustHaveFiles);
        }

        private static void CheckFont(string path, ReadModuleChecker checker)
        {
            string file = Path.Combine(path, string.Format("{0}_laba.asset",checker.CurId));
            checker.CheckFileExists(file);
        }

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

        private static void CheckSrcRoot(string path, ReadModuleChecker checker)
        {
            List<string> mustHaveFiles = new()
            {
                Path.Combine(path, "Constant.lua"),
                Path.Combine(path,"json.asset"),
                Path.Combine(path,"json.lua"),
                Path.Combine(path,"LabaCfg.lua"),
                Path.Combine(path, string.Format("Say{0}ResLink.asset", checker.CurId)),
                Path.Combine(path, string.Format("Say{0}ResLink.lua", checker.CurId)),
                Path.Combine(path,"SOUND.asset"),
                Path.Combine(path,"SOUND.lua")
            };
            checker.CheckFilesExists(mustHaveFiles);
        }

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