using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
namespace Tool.ReadChecker
{
    public class BookChecker    {
        [MenuItem("Tool/ReadChecker/CheckBook")]
        public static void CheckBookAssets()
        {
            var moduleRule = CreateRule(); 
            ReadModuleChecker readAssetChecker = new(moduleRule);
            string result = readAssetChecker.CheckModelByRule();
            //Debug.Log(result);
        }
        public static ModuleRule CreateRule()
        {
            var moduleRule = new ModuleRule("art/book", @"^\d{4}$")
            {
                ArtRequireList = CreateArtRules()
            };
            return moduleRule;
        }
        private static List<RequireRule> CreateArtRules()
        {
            List<RequireRule> requireRules = new()
            {
                new RequireRule("audio",
                   new HashSet<string> { ".ogg", ".mp3" },
                   new HashSet<string>{
                                @"^\d{4}_P\d{2}_\d{1}$",
                                @"^amb_p\d+(_p\d+)*$",
                                @"^bgm_p\d+(_p\d+)*$",
                                @"^\d{4}_title",
                                @"^\d{4}_words_\d{1}$",
                                @"^p\d+_fx_[a-zA-Z]+",
                                @"^p\d+_idle(_\d+)*"
                   }, null, BookAudioExtraCheck),

                new RequireRule("config",
                   new HashSet<string> { ".json" },
                   new HashSet<string> { @"^\d{4}_basecfg$", @"^\d{4}_cfg$", @"^\d{4}_text$" },null,
                   BookConfigExtraCheck
                   ),

                new RequireRule("head",
                   new HashSet<string> { ".png", ".json" },
                   new HashSet<string> { @"^all_head$", @"^picture_[a-zA-Z]+" }
                   ),

                new RequireRule("prefab",
                   new HashSet<string> { ".prefab" },
                   new HashSet<string> { @"^p\d+$" },null,
                   CheckPrefab
                   ),

                new RequireRule("reslink",
                   new HashSet<string> { ".asset", ".lua" },
                   new HashSet<string> { @"^d{4}reslink" }
                   ),

                new RequireRule("spine",
                   new HashSet<string> { ".png", ".json", ".txt", ".asset", ".mat" },
                   null,
                   new HashSet<string> { @"^p([1-9]|1[0-2])$" }
                   ),

                new RequireRule("thumbnails",
                   new HashSet<string> { ".png" },
                   new HashSet<string> { @"^[1-9]|1[0-2]$" },null,
                   CheckThumbnails
                   ),

                new RequireRule("timeline",
                   new HashSet<string> { ".playable" },
                   new HashSet<string> { @"^sp_p[1-9]|1[0-2]Timeline$" }
                   ),

                new RequireRule("titlepage",
                   new HashSet<string> { ".png" },
                   new HashSet<string> { @"^titlepage$" },null,
                   CheckTitlePage
                   ),

                new RequireRule("video",
                   new HashSet<string> { ".mp4", ".txt" },
                   new HashSet<string> { @"^video_book_\d{4}$", @"^video_book_\d{4}_srt$" },null,
                   CheckVideo
                   ),
            };
            return requireRules;
        }

        private static void CheckThumbnails(string path, ReadModuleChecker checker)
        {
            //只考虑前10页  页数不固定
            int fileCount = 10;
            string pattern = "{0}.png";
            var mustHaveFiles = Enumerable.Range(1, fileCount).Select(i => Path.Combine(path, string.Format(pattern, i))).ToList();
            checker.CheckFilesExists(mustHaveFiles);
        }

        private static void CheckPrefab(string path, ReadModuleChecker checker)
        {
            //只考虑前10页  页数不固定
            int fileCount = 10;
            string pattern = "p{0}.prefab";
            var mustHaveFiles = Enumerable.Range(1, fileCount).Select(i => Path.Combine(path, string.Format(pattern, i))).ToList();
            checker.CheckFilesExists(mustHaveFiles);
        }

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

        private static void CheckTitlePage(string path, ReadModuleChecker checker)
        {
            string mustHaveFile = Path.Combine(path,"titlepage.png");
            checker.CheckFileExists(mustHaveFile);
        }

        private static void BookConfigExtraCheck(string path, ReadModuleChecker checker)
        {
            List<string> mustHaveFiles = new()
            {
                Path.Combine(path, string.Format("{0}_basecfg.json", checker.CurId)),
                Path.Combine(path, string.Format("{0}_cfg.json", checker.CurId)),
                Path.Combine(path, string.Format("{0}_text.json", checker.CurId))
            };
            checker.CheckFilesExists(mustHaveFiles);
        }
        private static void BookAudioExtraCheck(string path, ReadModuleChecker checker)
        {

        }
    }
}