(function () {
	//#####################　メニューの背景変える　#####################
	Scene_Skill.prototype.createBackground = function () {
		this._backgroundSprite = new Sprite();
		this._backgroundSprite.bitmap = ImageManager.loadSystem("menu_skill_back");

		this.addChild(this._backgroundSprite);

		this._backgroundTextSprite = new Sprite(new Bitmap(Graphics.width, Graphics.height));
		var b = this._backgroundTextSprite.bitmap;
		b.fontFace = "serif";
		b.fontSize = 32;
		b.textColor = "#000000";
		b.drawTextM("スキル", 22, 20, 200, 22, "center");
		this.addChild(this._backgroundTextSprite);

		this._backgroundTextSprite3 = new Sprite(new Bitmap(200, 50));
		var b3 = this._backgroundTextSprite3.bitmap;
		b3.fontFace = "serif";
		b3.fontSize = 18;
		b3.textColor = "#000000";
		b3.drawTextM("消費ＳＰ", 0, 0, 200, 50, "center");
		this._backgroundTextSprite3.x = 572;
		this._backgroundTextSprite3.y = 58;
		this.addChild(this._backgroundTextSprite3);

		this._backgroundTextSprite4 = new Sprite(new Bitmap(200, 50));
		var b4 = this._backgroundTextSprite4.bitmap;
		b4.fontFace = "serif";
		b4.fontSize = 26;
		b4.textColor = "#000000";
		b4.drawTextM("スキルを使う", 0, 0, 200, 50, "center");
		this._backgroundTextSprite4.x = 920;
		this._backgroundTextSprite4.y = 124;
		this.addChild(this._backgroundTextSprite4);
	};

	Scene_Skill.prototype.create = function () {
		Scene_ItemBase.prototype.create.call(this);
		this.createHelpWindow();
		this.createSkillTypeWindow();
		//this.createStatusWindow();
		this.createItemWindow();
		this.createActorWindow();
	};

	// ####################################################
	// ################# アイテムの種類 ####################
	// ####################################################
	Scene_Skill.prototype.skillTypeWindowRect = function () {
		const wx = 270;
		const wy = -6;
		const ww = 600;
		const wh = 58 + 8;
		return new Rectangle(wx, wy, ww, wh);
	};

	Window_SkillType.prototype.maxCols = function () {
		return 5;
	};

	Window_SkillType.prototype.initialize = function (rect) {
		Window_HorzCommand.prototype.initialize.call(this, rect);
		this._padding = 0;
		this.contents.fontFace = "serif";
	};
	Window_SkillType.prototype._refreshAllParts = function () {
		this._padding = 0;
		//this._refreshBack();
		//this._refreshFrame();
		this._refreshCursor();
		this._refreshArrows();
		this._refreshPauseSign();
	};

	// デザイン関係
	//カーソルの変更
	Window_SkillType.prototype.loadWindowskin = function () {
		this.windowskin = ImageManager.loadSystem("Window");
		this.menu_item_skin = ImageManager.loadSystem("menu_item_categoly_select");
		this.menu_cursor_skin = ImageManager.loadSystem("menu_item_categoly_cursor");

		if (!this.menu_item_skin.isReady()) {
			this.menu_item_skin.addLoadListener(() => {
				this.refresh(); // 画像がロードされたら再描画
			});
			return;
		}
	};

	Window_SkillType.prototype.lineHeight = function () {
		return 58;
	};

	Window_SkillType.prototype._refreshCursor = function () {
		const drect = this._cursorRect.clone();
		const srect = { x: 0, y: 0, width: 126, height: 58 };
		const m = 0;
		for (const child of this._cursorSprite.children) {
			child.bitmap = this.menu_cursor_skin;
		}
		this._setRectPartsGeometry(this._cursorSprite, srect, drect, m);
	};
	//カーソルを微調整、あとカーソルを常に不透明に
	Window_SkillType.prototype._updateCursor = function () {
		this._cursorSprite.alpha = 255;
		this._cursorSprite.visible = this.isOpen() && this.cursorVisible;
		this._cursorSprite.x = this._cursorRect.x;
		this._cursorSprite.y = this._cursorRect.y - 4;
	};
	//未選択コマンドの背景を画像に
	Window_SkillType.prototype.drawItemBackground = function (index) {
		const rect = this.itemRect(index);
		if (this.menu_item_skin.isReady()) {
			const bitmap = this.menu_item_skin;
			const dx = rect.x;
			const dy = rect.y;
			const dw = rect.width;
			const dh = rect.height;
			this.contentsBack.blt(bitmap, 0, 0, 126, 58, dx, dy - 4, dw, dh);
		}
	};

	//カーソルが来た時、アイテムの文字名の色を変更する
	Window_SkillType.prototype.drawItem = function (index) {
		const rect = this.itemLineRect(index);
		const align = this.itemTextAlign();
		this.resetTextColor();
		if (index == this._index) {
			this.changeTextColor("#000000");
		}
		this.changePaintOpacity(this.isCommandEnabled(index));
		//文字をちょっと下にする
		this.drawTextS(this.commandName(index), rect.x, rect.y + 8, rect.width, align);
	};

	//セレクトするたびに背景をリフレッシュ
	Window_SkillType.prototype.select = function (index) {
		this._index = index;

		this.contents.clear();
		this.contentsBack.clear();
		this.drawAllItems();

		this.refreshCursor();
		this.callUpdateHelp();
	};
	//↑のセレクトでの不具合を出ないようにする
	Window_SkillType.prototype.drawAllItems = function () {
		const topIndex = this.topIndex();
		for (let i = 0; i < this.maxVisibleItems(); i++) {
			const index = topIndex + i;

			if (this._list && index < this.maxItems()) {
				//選択ちゅうだけ背景なくす
				if (i != this._index) {
					this.drawItemBackground(index);
				}
				this.drawItem(index);
			}
		}
	};

	// コマンド関係
	// コマンドの追加、2は必殺技
	Window_SkillType.prototype.makeCommandList = function () {
		if (this._actor) {
			const skillTypes = this._actor.skillTypes();
			for (const stypeId of skillTypes) {
				const name = $dataSystem.skillTypes[stypeId];
				this.addCommand("全スキル", "skill", true, stypeId, "all");
			}

			this.addCommand("攻撃", "skill", true, 2, "attack");
			this.addCommand("回復", "skill", true, 2, "cure");
			this.addCommand("補助", "skill", true, 2, "support");
			this.addCommand("エッチ", "skill", true, 2, "h");
		}
	};
	// コマンドにCBR_typeって属性作れるようにする
	Window_SkillType.prototype.addCommand = function (name, symbol, enabled = true, ext = null, CBR_type = null) {
		this._list.push({ name: name, symbol: symbol, enabled: enabled, ext: ext, CBR_type: CBR_type });
	};
	// 上の_listからextを引っ張ってくる
	Window_SkillType.prototype.currentExt = function () {
		return this.currentData() ? this.currentData() : null;
	};
	// updateで毎回これが更新されてる
	Window_SkillList.prototype.setStypeId = function (stype) {
		if (stype) {
			if (this._stypeId !== stype.ext || this._CBR_type !== stype.CBR_type) {
				this._stypeId = stype.ext;
				this._CBR_type = stype.CBR_type;
				this.refresh();
				this.scrollTo(0, 0);
			}
		}
	};
	//スキル一覧に表示するべきstypeIdを設定する、コメント欄を使用
	Window_SkillList.prototype.includes = function (item) {
		//return item && item.stypeId === this._stypeId;
		if (!this._CBR_type || this._CBR_type === "all") {
			return item;
		} else {
			return item && item.meta.CBR_type === this._CBR_type;
		}
	};

	// #############################################################
	// ################## スキル一覧ウィンドウ #######################
	// #############################################################
	Scene_Skill.prototype.createItemWindow = function () {
		const rect = this.itemWindowRect();
		this._itemWindow = new Window_MenuSkillList(rect);
		this._itemWindow.setHelpWindow(this._helpWindow);
		this._itemWindow.setHandler("ok", this.onItemOk.bind(this));
		this._itemWindow.setHandler("cancel", this.onItemCancel.bind(this));
		this._skillTypeWindow.setSkillWindow(this._itemWindow);
		this.addWindow(this._itemWindow);
	};

	Scene_Skill.prototype.itemWindowRect = function () {
		const wx = 147;
		const wy = 82;
		const ww = 630;
		const wh = 464;
		return new Rectangle(wx, wy, ww, wh);
	};

	function Window_MenuSkillList() {
		this.initialize(...arguments);
	}
	Window_MenuSkillList.prototype = Object.create(Window_SkillList.prototype);
	Window_MenuSkillList.prototype.constructor = Window_MenuSkillList;

	Window_MenuSkillList.prototype.initialize = function (rect) {
		Window_Selectable.prototype.initialize.call(this, rect);
		this._actor = null;
		this._stypeId = 0;
		this._data = [];
		this.contents.fontFace = "serif";
	};

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

	Window_MenuSkillList.prototype.drawItemName = function (item, x, y, width) {
		if (item) {
			const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
			const delta = ImageManager.standardIconWidth - ImageManager.iconWidth;
			const textMargin = ImageManager.standardIconWidth + 4;
			const itemWidth = Math.max(0, width - textMargin);
			this.resetTextColor();
			//this.drawIcon(item.iconIndex, x + delta / 2, iconY);
			this.changeTextColor("#000000");
			this.drawTextS(item.name, x + textMargin + 24, y, itemWidth);
			this.resetTextColor();
		}
	};
	Window_MenuSkillList.prototype.drawItemNumber = function (item, x, y, width) {
		if (this.needsNumber()) {
			this.changeTextColor("#000000");
			this.drawTextS($gameParty.numItems(item), x - 30, y, width, "right");
			this.resetTextColor();
		}
	};

	Window_MenuSkillList.prototype.changePaintOpacity = function (enabled) {
		this.contents.paintOpacity = enabled ? 255 : 100;
	};
	Window_MenuSkillList.prototype.drawItem = function (index) {
		const skill = this.itemAt(index);
		if (skill) {
			this.contents.fontSize = 22;
			const costWidth = this.costWidth();
			const rect = this.itemLineRect(index);
			this.changePaintOpacity(this.isEnabled(skill));
			this.drawItemName(skill, rect.x + 8, rect.y, rect.width);
			this.drawSkillCost(skill, rect.x - 70, rect.y, rect.width);
			this.changePaintOpacity(1);
		}
	};

	Window_MenuSkillList.prototype.drawSkillCost = function (skill, x, y, width) {
		this.changeTextColor("#000000");
		this.contents.fontSize = 22;
		if (this._actor.skillTpCost(skill) > 0) {
			//this.changeTextColor(ColorManager.tpCostColor());
			this.drawTextM(this._actor.skillTpCost(skill), x, y, width, "right");
		} else if (this._actor.skillMpCost(skill) > 0) {
			//this.changeTextColor(ColorManager.mpCostColor());
			this.drawTextM(this._actor.skillMpCost(skill), x, y, width, "right");
		}
		this.contents.fontSize = 16;
		this.drawTextM("SP", x + 24, y + 2, width, "right");
	};

	//カーソルの変更
	Window_MenuSkillList.prototype.loadWindowskin = function () {
		this.windowskin = ImageManager.loadSystem("Window");
		this.menu_cursor_skin = ImageManager.loadSystem("menu_item_selected");
		this.menu_item_skin = ImageManager.loadSystem("menu_item_selected_2");
	};

	//56×200にする
	Window_MenuSkillList.prototype._refreshCursor = function () {
		const drect = this._cursorRect.clone();
		const srect = { x: 0, y: 40, width: 588, height: 40 };
		const m = 0;
		for (const child of this._cursorSprite.children) {
			child.bitmap = this.menu_item_skin;
		}
		this._setRectPartsGeometry(this._cursorSprite, srect, drect, m);
	};

	//未選択コマンドの背景を画像に
	Window_MenuSkillList.prototype.drawItemBackground = function (index) {
		const rect = this.itemRect(index);
		if (this.menu_item_skin) {
			const bitmap = this.menu_item_skin;
			const dx = rect.x;
			const dy = rect.y;
			const dw = rect.width;
			const dh = rect.height;
			this.contentsBack.blt(bitmap, 0, 0, 588, 40, dx, dy, dw, dh);
		}
	};

	Window_MenuSkillList.prototype._refreshAllParts = function () {
		//this._refreshBack();
		//this._refreshFrame();
		this._refreshCursor();
		this._refreshArrows();
		this._refreshPauseSign();
	};

	// ##################################################
	// ############# ステータスウィンドウ #################
	// ##################################################
	Scene_Skill.prototype.actorWindowRect = function () {
		const wx = 756;
		const wy = 170;
		const ww = 522;
		const wh = 540;
		return new Rectangle(wx, wy, ww, wh);
	};
	Scene_Skill.prototype.refreshActor = function () {
		const actor = this.actor();
		this._skillTypeWindow.setActor(actor);
		//this._statusWindow.setActor(actor);
		this._itemWindow.setActor(actor);
	};

	Scene_Skill.prototype.useItem = function () {
		Scene_ItemBase.prototype.useItem.call(this);
		//this._statusWindow.refresh();
		this._itemWindow.refresh();
	};

	Scene_Skill.prototype.showActorWindow = function () {
		this._actorWindow.show();
		this._actorWindow.activate();
	};
	Scene_Skill.prototype.hideActorWindow = function () {
		//		this._actorWindow.hide();
		this._actorWindow.deactivate();
	};
	// ############################################
	// ############# ヘルプウィンドウ ##############
	// ############################################
	Scene_Skill.prototype.onItemCancel = function () {
		this._itemWindow.setHelpWindowItem(null);
		this._itemWindow.deselect();
		this._skillTypeWindow.activate();
	};

	Scene_Skill.prototype.helpWindowRect = function () {
		const wx = 20;
		const wy = 542;
		const ww = 740;
		const wh = this.helpAreaHeight() + 50;
		return new Rectangle(wx, wy, ww, wh);
	};

	function Window_SkillHelp() {
		this.initialize(...arguments);
	}
	Window_SkillHelp.prototype = Object.create(Window_Help.prototype);
	Window_SkillHelp.prototype.constructor = Window_SkillHelp;

	Window_SkillHelp.prototype.setItem = function (item) {
		if (item) {
			this.CBR_skill = item;
		} else {
			this.CBR_skill = null;
		}
		this.setText(item ? item.description : "");
	};

	Window_SkillHelp.prototype.refresh = function () {
		const rect = this.baseTextRect();
		this.contents.clear();

		this.contents.fontFace = "serif";
		this.changeTextColor("#000000");
		this.contents.fontSize = 20;
		this.drawTextM("概要", 16, 35, 80, "center");
		this.drawTextM("属性", rect.x + 28, rect.y + 4, rect.width);
		this.drawTextM("範囲", rect.x + 388, rect.y + 4, rect.width);
		if (this.CBR_skill) {
			this.changeTextColor("#FFFFFF");
			this.drawTextS(["必中", "物理", "魔法"][this.CBR_skill.hitType], rect.x + 108, rect.y + 4, rect.width);

			const scopeNames = [
				"なし", // 0: None
				"敵単体", // 1: One enemy
				"敵全体", // 2: All enemies
				"敵ランダム", // 3: 1 random enemy
				"敵ランダム×2", // 4: 2 random enemies
				"敵ランダム×3", // 5: 3 random enemies
				"敵ランダム×4", // 6: 4 random enemies
				"味方単体", // 7: One ally
				"味方全体", // 8: One dead ally
				"味方単体(戦闘不能)", // 9: All allies
				"味方全体(戦闘不能)", // 10: All dead allies
				"使用者", // 11: The user
				"味方単体(無条件)", // 12: One ally with HP ≤ 50%
				"味方全体(無条件)", // 13: All allies with HP ≤ 50%
				"敵と味方全体"
			];
			this.drawTextS(scopeNames[this.CBR_skill.scope], rect.x + 468, rect.y + 4, rect.width);
		}
		this.changeTextColor("#FFFFFF");
		this.drawTextExS(this._text, rect.x + 100, rect.y + 40, rect.width);
	};

	Scene_Skill.prototype.createHelpWindow = function () {
		const rect = this.helpWindowRect();
		this._helpWindow = new Window_SkillHelp(rect);
		// 背景を透明にする
		this._helpWindow.setBackgroundType(2);
		//フォントサイズの変更
		this._helpWindow.contents.fontSize = 22;
		//文字の枠線を削除

		this.addWindow(this._helpWindow);
	};
})();
