
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using iHuman.UnitySz.Editor;
using System;
using System.IO;
using System.Linq;

public class BookTool{

    public static string AssetsRoot = Application.dataPath.Replace("\\", "/") + "/"; //F:/repos/ihuman-art/app/Assets/
    public static string bookRoot = "art/book";

    [MenuItem("Tool/Book.GenAllResLink", false)]
    [Tooltip("为art/book自动生成reslink")]
    public static void GenResLink()
    {

        string root = AssetsRoot+bookRoot;
        string[] subDirectoryNames = Directory.GetDirectories(root).Select(d => Path.GetFileName(d)).ToArray();
        foreach (string bookId in subDirectoryNames)
        {

            GenBook(bookId);
        }
    }

    [MenuItem("Tool/Book.GenOneResLink", false)]
    [Tooltip("自动生成一本书的reslink")]
    public static void GenOneResLink()
    {
        // 已配置字音同步
        // string[] bookIds = {"0005","0006","0007","0009","0010","0011","0013","0014","0015","0017","0018","0019","0022",
            // '0021',"0023","0025","0027","0031","0034","0037","0038","0039","0041","0042","0043","0045","0046",
            // "0047","0049","0050", "0053", "0054", "0055","0057", "0058", "0061","0062", "0065", "0066", "0067",
            // "0071", "0078", "0079", "0097"};
        // 只有动画
        // string[] bookIds = {"0051", "0053", "0055", "0057", "0059", "0061", "0069", "0070", "0071", "0073", "0186", "0187"};
        string[] bookIds = {"0062", "0075", "0083", "0087"};

        foreach (string bookId in bookIds)
        {

            GenBook(bookId);
        }
    }

    public static void GenBook(string bookId)
    {
        Debug.Log($"bookId:{bookId}");
        string root = AssetsRoot + "art/book";
        string bookRoot = $"{root}/{bookId}/";
        var allFiles = new List<(string, string, System.Type)>();
        string[] filePaths = Directory.GetFiles(bookRoot, "*.*", SearchOption.AllDirectories);

        foreach (string filePath in filePaths)
        {
            string ffilePath = filePath.Replace("\\", "/");
            //过滤掉spine目录和reslink目录 和font目录
            if (ffilePath.Contains("/spine/") || ffilePath.Contains("/reslink/") || ffilePath.Contains("/font/"))
            {
                continue;
            }

            string fileExtension = Path.GetExtension(filePath).ToLower();
            string ffilePathNoExt = ffilePath.Substring(0, ffilePath.Length - fileExtension.Length);

            if (fileExtension == ".png" || fileExtension == ".json" || fileExtension == ".mp3" || fileExtension == ".ogg" || fileExtension == ".prefab" || fileExtension == ".asset" || fileExtension == ".mp4" || fileExtension == ".txt")
            {
                string key = ffilePathNoExt.Replace(bookRoot, "").Replace("/", "_");
                string relativePath = "Assets/" + ffilePath.Replace(AssetsRoot, "");
                Debug.Log($"filePath: {filePath} key: {key} relativePath: {relativePath}");
                System.Type type = null;
                if (fileExtension == ".png")
                    type = typeof(UnityEngine.Sprite);
                allFiles.Add((key, relativePath, type));
            }
        }

        string reslinkFullPath = "Assets/" + bookRoot.Replace(AssetsRoot, "") + $"reslink/{bookId}reslink.asset";
        AutoReslink.genOne(reslinkFullPath, allFiles);
    }
}