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

namespace Tool.ReadChecker
{
    public class PracticeChecker    {
        [MenuItem("Tool/ReadChecker/CheckPractice")]
        public static void CheckePracticeModel()
        {
            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("prefab",
               new HashSet<string> { ".prefab" },
               new HashSet<string> { @"^p\d+$" }
               ),

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

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

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

        private static void CheckNode(string path, ReadModuleChecker checker)
        {
            var mustHaveFolders = Enumerable.Range(1, 5).Select(i => Path.Combine(path, string.Format("game{0}", i))).ToList();
            checker.CheckFoldersExists(mustHaveFolders);
        }

        private static void CheckSound(string path, ReadModuleChecker checker)
        {
            var mustHaveFiles = Enumerable.Range(1, 4).Select(i => Path.Combine(path, string.Format("question{0}.ogg", 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 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("Practice{0}Config.lua", checker.CurId)),
                Path.Combine(path, string.Format("Practice{0}resLink.asset", checker.CurId)),
                Path.Combine(path, string.Format("Practice{0}resLink.lua", checker.CurId)),
            };
            checker.CheckFilesExists(mustHaveFiles);      
            
        }
        public static ModuleRule CreateRule()
        {
            var moduleRule = new ModuleRule("art/game/practice", @"^practice\d{4}$", "gamesrc/practice")
            {
                ArtRequireList = CreateArtRules(),
                SrcRequireList = CreateSrcRules()
            };
            return moduleRule;
        }
    }
}