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

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

        private static List<RequireRule> CreateArtRules()
        {
            List<RequireRule> requireRules = new()
            {
                new RequireRule("effects"),

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

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

                new RequireRule("video",
                    new HashSet<string> { ".mp4", ".txt" },
                    new HashSet<string> { @"^video_expand_\d{4}_\d{1}(_srt)*$" },null,
                    CheckVideo
                    ),

                new RequireRule("",
                    new HashSet<string> { ".unity" },
                    new HashSet<string> { @"^expand\d{4}" },
                    new HashSet<string> { "effects", "sound", "spine", "video", "backdrop", "effects" },
                    CheckArtRoot
                    )
            };

            return requireRules;
        }

        private static void CheckArtRoot(string path, ReadModuleChecker checker)
        {
            string mustFile = Path.Combine(path, string.Format("expand{0}.unity", checker.CurId));
            checker.CheckFileExists(mustFile);
        }

        private static void CheckVideo(string path, ReadModuleChecker checker)
        {
            List<string> mustHaveFiles = new()
            {
                Path.Combine(path, string.Format("video_expand_{0}_1.mp4", checker.CurId)),
                Path.Combine(path, string.Format("video_expand_{0}_1_srt.txt", checker.CurId)),
                Path.Combine(path, string.Format("video_expand_{0}_2.mp4", checker.CurId)),
                Path.Combine(path, string.Format("video_expand_{0}_2_srt.txt", checker.CurId)),
            };
            checker.CheckFilesExists(mustHaveFiles);
        }

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

        private static void CheckSrcRoot(string path, ReadModuleChecker checker)
        {
            List<string> mustHaveFiles = new()
            {
                Path.Combine(path, string.Format("Expand{0}.lua", checker.CurId)),
                Path.Combine(path, string.Format("Expand{0}register.lua", checker.CurId)),
                Path.Combine(path, string.Format("Expand{0}resLink.asset", checker.CurId)),
                Path.Combine(path, string.Format("Expand{0}resLink.lua", checker.CurId)),
            };
            checker.CheckFilesExists(mustHaveFiles);
        }

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