/*:
 * @target MZ
 * @plugindesc 選択肢で時間切れを実装
 * @author
 */
Window_ChoiceList.prototype.makeCommandList = function () {
	const choices = $gameMessage.choices();

	if ($gameSwitches.value(141)) {
		//スイッチがある場合はダミーを削る
		for (var i = 0; i < choices.length - 1; i++) {
			this.addCommand(choices[i], "choice");
		}
	} else {
		for (const choice of choices) {
			this.addCommand(choice, "choice");
		}
	}
};

Window_ChoiceList.prototype.numVisibleRows = function () {
	const choices = $gameMessage.choices();
	if ($gameSwitches.value(141)) {
		//スイッチがある場合はダミーを削る
		return Math.min(choices.length - 1, 10);
	} else {
		return Math.min(choices.length, 10);
	}
};

Window_ChoiceList.prototype.update = function () {
	Window_Selectable.prototype.update.call(this);
	this.updateCancelButton();

	if ($gameSwitches.value(141) && this.active && ConfigManager.selectLimit) {
		var v = $gameVariables.value(115);

		//$gameScreen.showPicture(15,"choiceGauge",1,1280/2,720/2,100,100,255,0);

		if (v < 0) {
			//選択肢から+1した場所の物を選択する
			this._index = this._list.length;
			this.updateInputData();
			this.deactivate();
			this.callOkHandler();
			return;
		} else {
			if (!$gameScreen._pictures[15] || !$gameScreen._pictures[15].visible) {
				$gameScreen.showPicture(15, "choiceGauge", 0, 1280 / 2 - 611 / 2, 720 / 2 - 65 / 2 - 100, 100, 100, 255, 0);
			}
			$gameScreen._pictures[15]._scaleX = (v / 300) * 100;

			v--;
			$gameVariables.setValue(115, v);
		}
	} else {
		if ($gameScreen._pictures[15] && $gameScreen._pictures[15].visible) {
			$gameScreen.erasePicture(15);
		}
	}
};

Window_ChoiceList.prototype.deactivate = function () {
	Window_Scrollable.prototype.deactivate.call(this);
	this.reselect();

	$gameSwitches.setValue(141, false);
};

//テキストと選択肢の間に変数やスイッチがあってもメッセージを表示する
Game_Interpreter.prototype.command101 = function (params) {
	if ($gameMessage.isBusy()) {
		return false;
	}
	$gameMessage.setFaceImage(params[0], params[1]);
	$gameMessage.setBackground(params[2]);
	$gameMessage.setPositionType(params[3]);
	$gameMessage.setSpeakerName(params[4]);
	while (this.nextEventCode() === 401 || this.nextEventCode() === 121) {
		//スイッチがあったらトバす
		//ならばもうイベントで追加するの、スイッチ・変数じゃなくて注釈とかでよくね？
		if (this.nextEventCode() === 121) {
			var p = this._list[this._index + 1].parameters;
			if (p[0] == 141) {
				this._index++;
				this._index++;
				$gameSwitches.setValue(141, true);
				$gameVariables.setValue(115, 300);
			} else {
				break;
			}
		} else {
			// Text data
			this._index++;
			$gameMessage.add(this.currentCommand().parameters[0]);
		}
	}
	switch (this.nextEventCode()) {
		case 102: // Show Choices
			this._index++;
			this.setupChoices(this.currentCommand().parameters);
			break;
		case 103: // Input Number
			this._index++;
			this.setupNumInput(this.currentCommand().parameters);
			break;
		case 104: // Select Item
			this._index++;
			this.setupItemChoice(this.currentCommand().parameters);
			break;
	}
	this.setWaitMode("message");
	return true;
};

/*
Game_Interpreter.prototype.command101 = function(params) {
    if ($gameMessage.isBusy()) {
        return false;
    }
    $gameMessage.setFaceImage(params[0], params[1]);
    $gameMessage.setBackground(params[2]);
    $gameMessage.setPositionType(params[3]);
    $gameMessage.setSpeakerName(params[4]);
    while (this.nextEventCode() === 401) {
        // Text data
        this._index++;
        $gameMessage.add(this.currentCommand().parameters[0]);
    }
    switch (this.nextEventCode()) {
        case 121:
            //this._index++;
            var p = this._list[this._index + 1].parameters;
            if(p[0] == 141){
                this._index++;
                console.log(this._list[this._index + 1]);
                this._index++;
                this.setupChoices(this.currentCommand().parameters);
            }
            

            break;
        case 102: // Show Choices
            this._index++;
            this.setupChoices(this.currentCommand().parameters);
            break;
        case 103: // Input Number
            this._index++;
            this.setupNumInput(this.currentCommand().parameters);
            break;
        case 104: // Select Item
            this._index++;
            this.setupItemChoice(this.currentCommand().parameters);
            break;
    }
    this.setWaitMode("message");
    return true;
};
*/
Window_ChoiceList.prototype.selectDefault = function () {
	if ($gameMessage.choiceDefaultType() < 0) {
		this.select(0);
	} else {
		this.select($gameMessage.choiceDefaultType());
	}
};
