function Window_power_up_list() {
	this.initialize(...arguments);
}

(function () {
	const OPTION_HEIGHT = 98;

	Scene_Survive_Normal_Map.prototype.createDisplayObjects = function () {
		this.createSpriteset();
		this.createWindowLayer();
		this.createAllWindows();

		// this._powerUpWindow = new Window_power_up_list(rect);

		// this.addWindow(this._powerUpWindow);

		this.createButtons();
	};

	Scene_Survive_Normal_Map.prototype.createNumberInputWindow = function () {
		const ww = 680;
		const wh = 4 * (OPTION_HEIGHT + 8) + $gameSystem.windowPadding() * 2;
		const wx = (Graphics.width - ww) / 2 + 20;
		const wy = 70;

		var rect = new Rectangle(wx, wy, ww, wh);
		this._numberInputWindow = new Window_power_up_list(rect);

		// this._numberInputWindow.setHandler("ok", this.onItemOk.bind(this));
		// this._numberInputWindow.setHandler("cancel", this.popScene.bind(this));
		this.addWindow(this._numberInputWindow);
	};

	// Scene_Survive_Normal_Map.prototype.onItemOk = function () {
	// 	this._powerUpWindow.index();
	// };
	// Scene_Survive_Normal_Map.prototype.onItemCancel = function () {
	// 	if (this._categoryWindow.needsSelection()) {
	// 		this._itemWindow.deselect();
	// 		this._categoryWindow.activate();
	// 	} else {
	// 		this.popScene();
	// 	}
	// };

	Window_power_up_list.prototype = Object.create(Window_Command.prototype);
	Window_power_up_list.prototype.constructor = Window_power_up_list;

	Window_power_up_list.prototype.initialize = function (rect) {
		Window_Selectable.prototype.initialize.call(this, rect);
		// this.refresh();
		// this.select(0);
		// this.activate();
		this.openness = 0;
		this.deactivate();
	};

	Window_power_up_list.prototype.maxCols = function () {
		return 3;
	};

	Window_power_up_list.prototype.lineHeight = function () {
		return OPTION_HEIGHT;
	};
	Window_power_up_list.prototype.loadWindowskin = function () {
		this.windowskin = ImageManager.loadSystem("Window_sviv");
	};

	Window_power_up_list.prototype.maxItems = function () {
		return SURVIVOR_ABILITY_LIST.length;
	};

	//[{name:"test", power:3},{name:"test", power:1}]
	Window_power_up_list.prototype.refresh = function () {
		var ary = $gameVariables.value(SURVIVOR_CONST.ABILITY_VARI) || [];
		for (var i = 0, len = ary.length; i < len; i++) {
			this.power_data[ary[i].name] = ary[i].power;
		}

		this.paint(); // ここでdrawAllItems
	};
	Window_power_up_list.prototype.drawAllItems = function () {
		const topIndex = this.topIndex();

		for (let i = 0; i < SURVIVOR_ABILITY_LIST.length; i++) {
			const index = topIndex + i;
			if (index < this.maxItems()) {
				this.drawItemBackground(index);
				this.drawItem(index);
			}
		}
	};

	Window_power_up_list.prototype.drawItem = function (index) {
		const item = SURVIVOR_ABILITY_LIST[index];
		if (item) {
			const rect = this.itemLineRect(index);

			this.contents.fontSize = 22;
			this.resetTextColor();
			this.drawText(SURVIVOR_ABILITY_DISP_DATA[item].name, rect.x + 0, rect.y - 30, 200, "center");
			this.drawIcon(SURVIVOR_ABILITY_DISP_DATA[item].icon.x + SURVIVOR_ABILITY_DISP_DATA[item].icon.y * 16, rect.x - 4, rect.y);

			// powers [{id: string, level: number} numberは1~10
			var powers = $gameVariables.value(173).power;
			//var power = powers.find((e) => e.id == item);
			var idx = powers.findIndex((e) => e.id == item);

			var level = idx !== -1 ? powers[idx].level : 0;

			var price = SURVIVOR_CONST.ABILITY_PRICE;
			var need = price[level];

			this.contents.fontSize = 18;
			if (price[level] !== Infinity) {
				this.drawText(need, rect.x + 30, rect.y - 2, 140, "center");
			} else {
				this.drawText("MAX", rect.x + 30, rect.y - 2, 140, "center");
			}

			const stars = Math.max(0, Math.min(10, level)); // 0〜10に制限
			//const text = "★★★★★★★★★★☆☆☆☆☆☆☆☆☆☆".slice(10 - stars, 20 - stars);
			const text = "★★★★★☆☆☆☆☆".slice(5 - stars, 10 - stars);
			const text1 = text.slice(0, 5);
			//const text2 = text.slice(5, 10);

			this.contents.fontSize = 16;
			this.drawText(SURVIVOR_CONST.ABILITY_TEXT[item], rect.x, rect.y + 40, rect.width, "center");
			this.changeTextColor("#bfff00ff");
			this.drawText(text1, rect.x + 30, rect.y + 20, 140, "center");
			//this.drawText(text2, rect.x + 30, rect.y + 42, 140, "center");
		}
	};

	Window_power_up_list.prototype.isCurrentItemEnabled = function () {
		var price = SURVIVOR_CONST.ABILITY_PRICE;

		const item = SURVIVOR_ABILITY_LIST[this.index()];

		var data = $gameVariables.value(173);
		var powers = data.power;
		var idx = powers.findIndex((e) => e.id == item);

		var level = idx !== -1 ? powers[idx].level : 0;
		var need = price[level];
		if (need <= data.gold) {
			if (idx !== -1) {
				//既に存在してる時
				data.power[idx].level += 1;
			} else {
				data.power.push({ id: item, level: 1 });
			}
			data.gold -= need; //お金を引く
			$gameVariables.setValue(173, data);
			this.refresh();
			return true;
		} else {
			return false;
		}

		// return this.currentData() ? this.currentData().enabled : false;
	};

	Window_power_up_list.prototype.setMessageWindow = function (messageWindow) {
		this._messageWindow = messageWindow;
	};

	Window_power_up_list.prototype.start = function () {
		// this._maxDigits = $gameMessage.numInputMaxDigits();
		// this._number = $gameVariables.value($gameMessage.numInputVariableId());
		// this._number = this._number.clamp(0, Math.pow(10, this._maxDigits) - 1);
		// this.updatePlacement();
		// this.placeButtons();
		// this.createContents();
		this.refresh();
		this.open();
		this.activate();
		this.select(0);

		$gameMessage.setChoices([1]);
	};

	Window_power_up_list.prototype.processOk = function () {
		if (this.isCurrentItemEnabled()) {
			this.playOkSound();
			//this.updateInputData();
			// this.deactivate();
			//this.callOkHandler();
		} else {
			this.playBuzzerSound();
		}
	};

	Window_power_up_list.prototype.isCancelEnabled = function () {
		return true;
	};
	Window_power_up_list.prototype.processCancel = function () {
		SoundManager.playCancel();
		this._messageWindow.terminateMessage();
		this.updateInputData();
		this.deactivate();
		this.close();
	};

	// ################# 左上のお金表示
	const WIN_W = 220; // 表示幅
	const WIN_H = 62; // 表示高さ
	const WIN_X = 780; // 表示位置 X
	const WIN_Y = 10; // 表示位置 Y
	const PADDING = 8; // 内側余白
	const FONT_SIZE = 20; // フォントサイズ（必要なら変更）

	// 小さなウィンドウを作るクラス
	class Window_Survive_gold extends Window_Base {
		constructor(x, y, w, h) {
			super(new Rectangle(x, y, w, h));
			this._lastValue = null;
			this.refresh();
		}

		standardFontSize() {
			return FONT_SIZE;
		}

		refresh() {
			this.contents.clear();
			const value = $gameVariables.value(173).gold;
			const text = `${value} GOLD`;
			this.resetFontSettings();
			this.drawText(text, PADDING, 0, 170, "right");
			this._lastValue = value;
		}

		loadWindowskin() {
			this.windowskin = ImageManager.loadSystem("Window_sviv");
		}

		update() {
			super.update();
			const value = $gameVariables.value(SURVIVOR_CONST.MONEY_VARI);
			if (value !== this._lastValue) {
				this.refresh();
			}
		}
	}

	// NumberInputが開いたらウィンドウを作り、閉じたら消す
	const _Window_power_up_list_open = Window_power_up_list.prototype.open;
	Window_power_up_list.prototype.open = function () {
		_Window_power_up_list_open.call(this);
		const scene = SceneManager._scene;
		if (scene && !scene._variablePreviewWindow) {
			scene._variablePreviewWindow = new Window_Survive_gold(WIN_X, WIN_Y, WIN_W, WIN_H);
			scene.addChild(scene._variablePreviewWindow);
		}
	};

	const _Window_power_up_list_close = Window_power_up_list.prototype.close;
	Window_power_up_list.prototype.close = function () {
		_Window_power_up_list_close.call(this);
		const scene = SceneManager._scene;
		if (scene && scene._variablePreviewWindow) {
			scene.removeChild(scene._variablePreviewWindow);
			scene._variablePreviewWindow.destroy();
			scene._variablePreviewWindow = null;
		}
	};

	// 念のため、シーン切り替え時にもクリアしておく
	const _Scene_Survive_Normal_Map_terminate = Scene_Survive_Normal_Map.prototype.terminate;
	Scene_Survive_Normal_Map.prototype.terminate = function () {
		if (this._variablePreviewWindow) {
			this.removeChild(this._variablePreviewWindow);
			this._variablePreviewWindow.destroy();
			this._variablePreviewWindow = null;
		}
		_Scene_Survive_Normal_Map_terminate.call(this);
	};
})();
