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

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

        private static List<RequireRule> CreateArtRules()
        {
            List<RequireRule> requireRules = new()
            {
                new RequireRule("analysis",
                     new HashSet<string> { ".png" },
                     new HashSet<string> { @"analysis_\d{1}_\d{1}$" }
                     ),

                new RequireRule("config",
                    new HashSet<string> { ".json" },
                    new HashSet<string> { @"^BaseConfig$" },null,
                    CheckConfig
                    ),

                new RequireRule("node",
                    new HashSet<string> { ".png", ".asset", ".prefab" },
                    null,
                    new HashSet<string> { @"^game\d{1}" },
                    CheckNode
                    ),

                new RequireRule("res",
                    new HashSet<string> { ".png", ".json", ".txt", ".asset", ".mat", ".prefab" },
                    null,
                    new HashSet<string> { @"^game\d{1}" }
                    ),

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

                //又有res 又有 spine 文件路径不统一 暂时认为res为标准
                //new RequireRule("spine",
                //    new HashSet<string> { ".png", ".json", ".txt", ".asset", ".mat" },
                //    null,
                //    new HashSet<string> { "bg"}
                //    ),

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

                new RequireRule("",
                    null, null,
                    new HashSet<string> { "analysis", "node", "video", "res", "sound", "spine", "config" }
                )
            };
            return requireRules;
        }

        private static void CheckVideo(string path, ReadModuleChecker checker)
        {
           
            var mustHaveFiles = Enumerable.Range(1, 5).Select(i => Path.Combine(path, string.Format("video_{0}.mp4", i))).ToList();
            mustHaveFiles.AddRange(Enumerable.Range(1, 5).Select(i => Path.Combine(path, string.Format("video_srt_{0}.txt", i))).ToList());
            checker.CheckFilesExists(mustHaveFiles);
        }

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

        private static void CheckSound(string path, ReadModuleChecker checker)
        {
            List<string> mustHaveFiles = new()
            {
                Path.Combine(path, "poetryAuthor.ogg"),
                Path.Combine(path, "poetryTitle.ogg"),
            };
            mustHaveFiles.AddRange(Enumerable.Range(1, 6).Select(i => Path.Combine(path, string.Format("question{0}.ogg", i))).ToList());
            mustHaveFiles.AddRange(Enumerable.Range(1, 4).Select(i => Path.Combine(path, string.Format("poetry{0}.ogg", i))).ToList());
            checker.CheckFilesExists(mustHaveFiles);
        }
        private static void CheckNode(string path, ReadModuleChecker checker)
        {
            var mustHaveFolders = Enumerable.Range(1, 6).Select(i => Path.Combine(path, string.Format("game{0}", i))).ToList();
            checker.CheckFoldersExists(mustHaveFolders);
        }

        private static List<RequireRule> CreateSrcRules()
        {
            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, string.Format("Weekly{0}resLink.asset", checker.CurId)),
                Path.Combine(path, string.Format("Weekly{0}resLink.lua", checker.CurId))
            };
            checker.CheckFilesExists(mustHaveFiles);
        }

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