/*:ja
 * @plugindesc 
 * @author 
 *
 * @param id
 * @text 変数
 * @desc グローバルセーブに保存する変数を設定します
 * @default 1
 * @type variable
 * 
 * @command SAVE_GLOBAL
 * @text グローバルセーブを保存
 * @desc グローバルセーブを保存します
 *
 * @command LOAD_GLOBAL
 * @text グローバルセーブ読み込み
 * @desc グローバルセーブを読み込みます
 * 
 * @help
 */

(() => {
    const script = document.currentScript;
	const param  = PluginManagerEx.createParameter(script);
	
	PluginManagerEx.registerCommand(script, 'SAVE_GLOBAL', function(args) {
		$gameVariables.saveGlobalValue();
    });
    
    PluginManagerEx.registerCommand(script, 'LOAD_GLOBAL', function(args) {
        $gameVariables.setGlobalValue();
	});

    DataManager.makeSavefileInfo = function() {
        const info = {};
        info.title = $dataSystem.gameTitle;
        info.characters = $gameParty.charactersForSavefile();
        info.faces = $gameParty.facesForSavefile();
        info.playtime = $gameSystem.playtimeText();
        info.playtime = $gameSystem.playtimeText();
        info.extend = $gameVariables.value(param.id);
        info.haerts = $gameVariables.value(7);//@add
        info.achive = $gameSwitches.achiveList();//@add
        return info;
    };

    DataManager.removeInvalidGlobalInfo = function() {
        const globalInfo = this._globalInfo;
        for (const info of globalInfo) {
            const savefileId = globalInfo.indexOf(info);
            if (savefileId !== 0 && !this.savefileExists(savefileId)) {
                delete globalInfo[savefileId];
            }
        }
    };

    DataManager.loadGlobalSaveExtend = function() {
        let result = 0;
        const globalInfo = this._globalInfo;
        for (const info of globalInfo) {
            if(info && info.extend > result){
                result = info.extend;
            }
        }
        return result;
    };

    DataManager.loadGlobalSaveAchive = function() {
        let result = [];
        const globalInfo = this._globalInfo;
        for (const info of globalInfo) {
            if(info && info.achive){
                result = this.checkAchive(info.achive, result);
            }
        }
        return result;
    };

    DataManager.checkAchive = function(achive, base) {
        let result = base;
        for(let i = 0; i < achive.length; i++){
            if(result[i] === undefined) result[i] = false;
            if(!!achive[i]) result[i] = true;
        }
        return result;
    };

    DataManager.saveGlobalInfoExtend = function(savefileId) {
        this._globalInfo[savefileId] = this.makeSavefileInfo();
        this.saveGlobalInfo();
    };

    Game_Variables.prototype.setGlobalValue = function() {
        const value = DataManager.loadGlobalSaveExtend();
        this._data[param.id] = value;
    };

    Game_Variables.prototype.saveGlobalValue = function() {
        const savefileId = 0;
        DataManager.saveGlobalInfoExtend(savefileId);
    };

    Game_Switches.prototype.achiveList = function() {
        const start = 51;
        const end = 80;
        //const names = $dataSystem.switches;
        let result = DataManager.loadGlobalSaveAchive();
        for(let i = start; i < end; i++){
            if(result[i - start] === undefined) result[i - start] = false;
            if(this._data[i]) result[i - start] = true;
            //result.push(this._data[i]);
        }
        return result;
    };

})();