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

(function () {
	const OPTION_HEIGHT = 78;

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

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

		// this.addWindow(this._powerUpWindow);

		this.createButtons();
	};

	Scene_Survive_Battle_Map.prototype.createNumberInputWindow = function () {
		const ww = 580;
		const wh = 4 * (OPTION_HEIGHT + 8) + $gameSystem.windowPadding() * 2;
		const wx = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN - ww) / 2;
		const wy = 150;

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

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

		// キャンセルウィンドウもここで作る
		this.createSurviverCancelWindow();
	};

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

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

	Window_battle_power_up_list.prototype.initialize = function (rect) {
		// this.power_data = {
		// 	collect_range: 0,
		// 	atk_power: 0,
		// 	atk_rate: 0,
		// 	atk_speed: 0,
		// 	atk_num: 0,
		// 	atk_range: 0,
		// 	st_hp: 0,
		// 	dm_resist: 0,
		// 	st_rec: 0,
		// 	st_exp: 0,
		// 	st_money: 0,
		// 	st_life: 0
		// };
		this._list = [];
		//[["ファイア", "アイス", "サンダー"]];
		Window_Selectable.prototype.initialize.call(this, rect);
		// this.refresh();
		// this.select(0);
		// this.activate();
		this.openness = 0;
		this.deactivate();
	};

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

	Window_battle_power_up_list.prototype.makeCommandList = function () {
		const maxLevel = 5;
		const maxSpot = 5; // 枠

		// まずlvel5は除く
		// 枠がmaxの時はweapon_levelにある物のみ
		const player = $gameTemp.SURVIVOR_player_data;
		const ary1 = SURVIVOR_WEAPON_LIST.filter((id) => {
			const w = player.weapon_level;
			const hasId = w.some((e) => e.id === id);
			const isMax = w.some((e) => e.id === id && e.level === maxLevel);
			return !isMax && (w.length < maxSpot || hasId);
		});

		const ary2 = SURVIVOR_ABILITY_LIST.filter((id) => {
			const w = player.ability_level;
			const hasId = w.some((e) => e.id === id);
			const isMax = w.some((e) => e.id === id && e.level === maxLevel);
			return !isMax && (w.length < maxSpot || hasId);
		});

		const shuffled = [...ary1, ...ary2].sort(() => Math.random() - 0.5);
		this._list = shuffled.slice(0, 3);

		if (this._list.length == 0) {
			this._list.push("money");
		}
		this._list.push("skip");
	};

	Window_battle_power_up_list.prototype.maxCols = function () {
		return 1;
	};

	Window_battle_power_up_list.prototype.lineHeight = function () {
		return OPTION_HEIGHT;
	};

	Window_battle_power_up_list.prototype.maxItems = function () {
		return this._list.length;
	};

	//[{name:"test", power:3},{name:"test", power:1}]
	Window_battle_power_up_list.prototype.refresh = function () {
		this.makeCommandList();

		// for (var i = 0, len = ary.length; i < len; i++) {
		// 	this.power_data[ary[i].name] = ary[i].power;
		// }

		this.paint();
	};
	Window_battle_power_up_list.prototype.drawAllItems = function () {
		const topIndex = this.topIndex();

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

	Window_battle_power_up_list.prototype.drawItem = function (index) {
		const id = this._list[index];
		if (id) {
			const rect = this.itemLineRect(index);
			if (id === "skip") {
				this.contents.fontSize = 22;
				this.resetTextColor();
				this.drawText("スキップ", rect.x + 0, rect.y, 200, "center");
			} else if (id === "money") {
				this.contents.fontSize = 22;
				this.resetTextColor();
				this.drawIcon(0 + 13 * 16, rect.x - 4, rect.y);
				this.drawText("お金", rect.x + 0, rect.y - 20, 200, "center");
				this.drawText("500 GOLD貰える", rect.x + 180, rect.y - 20, 500, "left");
			} else {
				var data = SURVIVOR_ABILITY_DISP_DATA[id] || SURVIVOR_WEAPON_DISP_DATA[id];

				this.contents.fontSize = 22;
				this.resetTextColor();
				this.drawText(data.name, rect.x + 0, rect.y - 20, 200, "center");
				this.drawIcon(data.icon.x + data.icon.y * 16, rect.x - 4, rect.y);

				this.drawText(SURVIVOR_CONST.WEAPON_TEXT[id] || SURVIVOR_CONST.ABILITY_TEXT[id], rect.x + 180, rect.y - 20, 500, "left");

				var level =
					$gameTemp.SURVIVOR_player_data.weapon_level.find((e) => e.id == id) ||
					$gameTemp.SURVIVOR_player_data.ability_level.find((e) => e.id == id);
				var value = level ? level.level : 0;

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

				this.contents.fontSize = 16;
				this.changeTextColor("#bfff00ff");
				this.drawText(text1, rect.x + 30, rect.y + 8, 140, "center");
				this.drawText(text2, rect.x + 30, rect.y + 30, 140, "center");
			}
		}
	};

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

	Window_battle_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);
	};

	Window_battle_power_up_list.prototype.processOk = function () {
		SoundManager.playUseSkill();

		// レベルアップ処理
		$gameTemp.SURVIVOR_player_data.now_exp -= $gameTemp.SURVIVOR_next_exp($gameTemp.SURVIVOR_player_data.level);
		$gameTemp.SURVIVOR_player_data.now_exp = 0;
		$gameTemp.SURVIVOR_player_data.level++;

		var id = this._list[this.index()];

		// weapon_levelとかの値を+1したり追加する{id : string, level : number}
		// スキップの場合 なにもしない
		if (id === "skip") {
			// お金を選んだ場合
		} else if (id === "money") {
			var v = $gameVariables.value(173);
			v.gold += Math.ceil(500 * $gameTemp.SURVIVOR_player_data.st_money);
			$gameVariables.setValue(173, v);
			// 武器の場合
		} else if (SURVIVOR_WEAPON_LIST.indexOf(id) !== -1) {
			var idx = $gameTemp.SURVIVOR_player_data.weapon_level.findIndex((e) => e.id == id);
			// レベルアップの時
			if (idx !== -1) {
				$gameTemp.SURVIVOR_player_data.weapon_level[idx].level += 1;
				SceneManager._scene._spriteset.refreshWeapon(id, $gameTemp.SURVIVOR_player_data.weapon_level[idx].level);
				// 新規追加の時
			} else {
				$gameTemp.SURVIVOR_player_data.weapon_level.push({
					id: id,
					level: 1
				});
				SceneManager._scene._spriteset.addWeapon(id, 1);
			}
			// アビリティの場合
		} else if (SURVIVOR_ABILITY_LIST.indexOf(id) !== -1) {
			var idx = $gameTemp.SURVIVOR_player_data.ability_level.findIndex((e) => e.id == id);
			// レベルアップの時
			if (idx !== -1) {
				$gameTemp.SURVIVOR_player_data.ability_level[idx].level += 1;
				// 新規追加の時
			} else {
				$gameTemp.SURVIVOR_player_data.ability_level.push({
					id: id,
					level: 1
				});
			}

			if (id == "atk_num") {
				for (var i = 0; i < $gameTemp.SURVIVOR_player_data.weapon_level.length; i++) {
					SceneManager._scene._spriteset.refreshWeapon(
						$gameTemp.SURVIVOR_player_data.weapon_level[i].id,
						$gameTemp.SURVIVOR_player_data.weapon_level[i].level
					);
				}
			}
		}

		SURVIVOR_DATA.refreshPlayerState();

		if (id === "st_hp") {
			$gameTemp.SURVIVOR_player_data.nowhp = $gameTemp.SURVIVOR_player_data.maxhp;
		}

		// ウィンドウの終了
		this._messageWindow.terminateMessage();
		this.updateInputData();
		this.deactivate();
		this.close();

		SceneManager._scene._iconWindow.refresh();
	};

	Window_battle_power_up_list.prototype.isCancelEnabled = function () {
		return false;
	};

	Window_battle_power_up_list.prototype.open = function () {
		if (!this.isOpen()) {
			this._opening = true;
		}
		this._closing = false;
		$gameTimer._working = false;
	};
	Window_battle_power_up_list.prototype.close = function () {
		if (!this.isClosed()) {
			this._closing = true;
		}
		this._opening = false;
		$gameTimer._working = true;
	};
})();

// ################ 左下のウィンドウ ################
function Window_Survive_Icon_Info() {
	this.initialize(...arguments);
}

(function () {
	Window_Survive_Icon_Info.prototype = Object.create(Window_Selectable.prototype);
	Window_Survive_Icon_Info.prototype.constructor = Window_Survive_Icon_Info;

	Window_Survive_Icon_Info.prototype.initialize = function (rect) {
		Window_Selectable.prototype.initialize.call(this, rect);
		this.refresh();
	};

	Window_Survive_Icon_Info.prototype.refresh = function () {
		this.contents.clear();
		// this.drawCharacter(3, 10, 10);
		//this.drawIcon(3,10,10);

		this.contents.fontSize = 16;
		for (var i = 0; i < $gameTemp.SURVIVOR_player_data.weapon_level.length; i++) {
			var data = $gameTemp.SURVIVOR_player_data.weapon_level[i];
			var icon = SURVIVOR_WEAPON_DISP_DATA[data.id].icon;

			this.drawIcon(icon.x + icon.y * 16, i * 32, 0);
			this.drawText(data.level, i * 32 + 20, 0 + 4);
		}

		for (var i = 0; i < $gameTemp.SURVIVOR_player_data.ability_level.length; i++) {
			var data = $gameTemp.SURVIVOR_player_data.ability_level[i];
			var icon = SURVIVOR_ABILITY_DISP_DATA[data.id].icon;

			this.drawIcon(icon.x + icon.y * 16, i * 32, 0 + 32);
			this.drawText(data.level, i * 32 + 20, 0 + 4 + 32);
		}
	};

	Window_Survive_Icon_Info.prototype.update = function () {
		this.processCursorMove();
		this.processHandling();
		this.processTouch();
		Window_Scrollable.prototype.update.call(this);
	};

	Window_Survive_Icon_Info.prototype.open = function () {
		this.refresh();
		Window_Selectable.prototype.open.call(this);
	};

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

	const _Scene_Survive_Battle_Map_createAllWindows = Scene_Survive_Battle_Map.prototype.createAllWindows;
	Scene_Survive_Battle_Map.prototype.createAllWindows = function () {
		_Scene_Survive_Battle_Map_createAllWindows.call(this);

		const rect = this.iconWindowRect();
		this._iconWindow = new Window_Survive_Icon_Info(rect);
		this.addChild(this._iconWindow);
	};

	Scene_Survive_Battle_Map.prototype.iconWindowRect = function () {
		const ww = 32 * 5 + 12 * 2;
		const wh = 32 * 2 + 12 * 2;
		const wx = 0;
		const wy = 54;
		return new Rectangle(wx, wy, ww, wh);
	};
})();

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

(function () {
	const OPTION_HEIGHT = 78;

	Scene_Survive_Battle_Map.prototype.createSurviverCancelWindow = function () {
		const ww = 680;
		const wh = 4 * (OPTION_HEIGHT + 8) + $gameSystem.windowPadding() * 2;
		const wx = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN - ww) / 2;
		const wy = 150;

		var rect = new Rectangle(wx, wy, ww, wh);
		this._cancelWindow = new Window_Surviver_Cancel(rect);
		this.addWindow(this._cancelWindow);
	};

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

	Window_Surviver_Cancel.prototype.initialize = function (rect) {
		this._list = ["ok", "cancel"];
		//[["ファイア", "アイス", "サンダー"]];
		Window_Selectable.prototype.initialize.call(this, rect);
		// this.refresh();
		// this.select(0);
		// this.activate();
		this.openness = 0;
		this.deactivate();
	};

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

	Window_Surviver_Cancel.prototype.makeCommandList = function () {
		this._list = ["ok", "cancel"];
	};

	Window_Surviver_Cancel.prototype.maxCols = function () {
		return 2;
	};

	Window_Surviver_Cancel.prototype.lineHeight = function () {
		return OPTION_HEIGHT;
	};

	Window_Surviver_Cancel.prototype.maxItems = function () {
		return this._list.length;
	};

	//[{name:"test", power:3},{name:"test", power:1}]
	Window_Surviver_Cancel.prototype.refresh = function () {
		this.makeCommandList();

		// for (var i = 0, len = ary.length; i < len; i++) {
		// 	this.power_data[ary[i].name] = ary[i].power;
		// }

		this.paint();
	};
	Window_Surviver_Cancel.prototype.drawAllItems = function () {
		const topIndex = this.topIndex();

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

		this.contents.fontSize = 22;
		for (var i = 0; i < SURVIVOR_ABILITY_LIST.length; i++) {
			var id = SURVIVOR_ABILITY_LIST[i];
			$gameTemp.SURVIVOR_player_data[id];

			var rect_x = Math.floor(i / 5) * 200;
			var rect_y = (i % 5) * 30;
			this.drawText(SURVIVOR_ABILITY_DISP_DATA[id].name, 10 + rect_x, 100 + rect_y, 100, "left");
			this.drawText($gameTemp.SURVIVOR_player_data[id].toFixed(2), 120 + rect_x, 100 + rect_y, 70, "right");
		}
	};

	Window_Surviver_Cancel.prototype.drawItem = function (index) {
		const id = this._list[index];
		if (id) {
			const rect = this.itemLineRect(index);

			this.contents.fontSize = 22;
			this.resetTextColor();
			var text = {
				ok: "ビーチへ戻る",
				cancel: "キャンセル"
			};

			this.drawText(text[id], rect.x + 0, rect.y, rect.width, "center");
		}
	};

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

	Window_Surviver_Cancel.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(1);
	};

	Window_Surviver_Cancel.prototype.processOk = function () {
		SoundManager.playUseSkill();

		// // レベルアップ処理
		// $gameTemp.SURVIVOR_player_data.now_exp -= $gameTemp.SURVIVOR_next_exp($gameTemp.SURVIVOR_player_data.level);
		// $gameTemp.SURVIVOR_player_data.now_exp = 0;
		// $gameTemp.SURVIVOR_player_data.level++;

		//
		var command = this._list[this.index()];
		if (command == "ok") {
			$gameTimer.stop();
			$gameTemp.reserveCommonEvent(1217);
		}

		// ウィンドウの終了
		this._messageWindow.terminateMessage();
		this.updateInputData();
		this.deactivate();
		this.close();

		SceneManager._scene._iconWindow.refresh();
	};

	Window_Surviver_Cancel.prototype.isCancelEnabled = function () {
		return true;
	};

	Window_Surviver_Cancel.prototype.processHandling = function () {
		if (this.isOpen() && this.active) {
			if (Input.isRepeated("cancel")) {
				this.processBack();
			}
			if (Input.isRepeated("ok")) {
				this.processOk();
			}
		}
	};

	Window_Surviver_Cancel.prototype.processBack = function () {
		SoundManager.playCancel();
		this._messageWindow.terminateMessage();
		this.updateInputData();
		this.deactivate();
		this.close();
	};

	Window_Surviver_Cancel.prototype.open = function () {
		if (!this.isOpen()) {
			this._opening = true;
		}
		this._closing = false;
		$gameTimer._working = false;
	};
	Window_Surviver_Cancel.prototype.close = function () {
		if (!this.isClosed()) {
			this._closing = true;
		}
		this._opening = false;
		// ビーチへ戻るコモンイベントが無い時だけタイマー再開
		if (!$gameTemp._commonEventQueue || !$gameTemp._commonEventQueue.length) {
			$gameTimer._working = true;
		}
	};
})();
