//=============================================================================
// RPG Maker MZ - FontDictionary
//
// Copyright 2024 Panzer-IV. All rights reserved.
// This source code or any portion thereof must not be
// reproduced or used without licensed in any manner whatsoever.
//=============================================================================
/*:
 * @target MZ
 * @plugindesc フォント保持クラス
 * @author 四号戦車
 * @base PluginCommonBase
 * @orderAfter PluginCommonBase
 *
 * @param fontName
 * @text フォントのファイル名
 * @desc ロードするフォント名
 * @type string[]
 * @default []
 *
 * @help FontDictionary.js
 *
 * Version: 0.0.2
 *
 * 各プラグインでフォントを扱うための拡張クラスです。
 *
 */
(() => {
	"use strict";

    const pluginName = "FontDictionary";
    const script = document.currentScript;
    const param  = PluginManagerEx.createParameter(script);

    /********************************************************************************************************
     * 共有基盤
     ********************************************************************************************************/
    // デバッグ判定
    const log = (text) => {
        if (DebugSwitch.isEnable()) {
            console.log(text);
        }
    };

    //========================================================================
    // Fontのロード
    //========================================================================
    const _Game_System_initialize = Game_System.prototype.initialize;
    Game_System.prototype.initialize = function() {
        _Game_System_initialize.apply(this, arguments);
        this.fontDictionary = new FontDictionary();
    };

    //========================================================================
    // FontManager拡張
    //========================================================================
    const load = async (family, filename, callback) => {
        const url = FontManager.makeUrl(filename);
        const source = "url(" + url + ")";
        const font = new FontFace(family, source);
        FontManager._urls[family] = url;
        FontManager._states[family] = "loading";
        font.load()
            .then(() => {
                document.fonts.add(font);
                FontManager._states[family] = "loaded";
                !!callback && callback();
                return 0;
            })
            .catch(() => {
                FontManager._states[family] = "error";
            });
    };

    const extractExtension = (path) => {
        const fileName = Utils.extractFileName(path) ;
        const segments = fileName.split('.');
        if (segments.length > 3) {
            return segments.slice(segments.length - 1).join('.');
        } else {
            return segments[0];
        }
    }

    class FontDictionary {
        constructor() {
            this.isReady = false;
            param.fontName.forEach(file => {
                const family = extractExtension(file);

                if (FontManager._states[family] !== "loaded") {
                    load(family, file, () => {
                        log(`Font Loaded: ${family}`);
                    });
                }
            });
        }

        getFonts() {
            return Object.keys(FontManager._states).filter(family => {
                return FontManager._states[family] === 'loaded';
            });
        }
    };
    window.FontDictionary = FontDictionary;
})();
