(function () {
	//#####################　メニューの背景変える　#####################
	Scene_Item.prototype.createBackground = function () {
		this._backgroundSprite = new Sprite();
		this._backgroundSprite.bitmap = ImageManager.loadSystem("menu_item_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_Item.prototype.categoryWindowRect = function () {
		const wx = 270;
		const wy = -6;
		const ww = 600;
		const wh = 58 + 8;
		return new Rectangle(wx, wy, ww, wh);
	};

	Window_ItemCategory.prototype.initialize = function (rect) {
		Window_HorzCommand.prototype.initialize.call(this, rect);
		this._padding = 0;
		this.contents.fontFace = "serif";
	};

	//カテゴリーはアイテムと大事な物だけにする
	Window_ItemCategory.prototype.makeCommandList = function () {
		if (this.needsCommand("item")) {
			this.addCommand(TextManager.item, "item");
		}
		if (this.needsCommand("keyItem")) {
			this.addCommand(TextManager.keyItem, "keyItem");
		}
	};

	Window_ItemCategory.prototype.needsCommand = function (name) {
		const table = ["item", "keyItem"];
		const index = table.indexOf(name);
		if (index >= 0) {
			return $dataSystem.itemCategories[index];
		}
		return true;
	};

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

	//カーソルの変更
	Window_ItemCategory.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_ItemCategory.prototype.lineHeight = function () {
		return 58;
	};

	Window_ItemCategory.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_ItemCategory.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_ItemCategory.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_ItemCategory.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_ItemCategory.prototype.select = function (index) {
		this._index = index;

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

		this.refreshCursor();
		this.callUpdateHelp();
	};
	//↑のセレクトでの不具合を出ないようにする
	Window_ItemCategory.prototype.drawAllItems = function () {
		this.contents.fontFace = "serif";
		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);
			}
		}
	};

	// #######################################################
	// ################### アイテム一覧 #######################
	// #######################################################
	Scene_Item.prototype.itemWindowRect = function () {
		const wx = 147;
		const wy = 82;
		const ww = 630;
		const wh = 464;
		return new Rectangle(wx, wy, ww, wh);
	};
	Window_ItemList.prototype._refreshAllParts = function () {
		//this._refreshBack();
		//this._refreshFrame();
		this._refreshCursor();
		this._refreshArrows();
		this._refreshPauseSign();
	};

	Window_ItemList.prototype.changePaintOpacity = function (enabled) {
		this.contents.paintOpacity = enabled ? 255 : 100;
	};
	Window_ItemList.prototype.initialize = function (rect) {
		Window_Selectable.prototype.initialize.call(this, rect);
		this._category = "none";
		this._data = [];
		this.contents.fontFace = "serif";
	};
	Window_ItemList.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 + 34, y, itemWidth);
			this.resetTextColor();
		}
	};
	Window_ItemList.prototype.drawItemNumber = function (item, x, y, width) {
		if (this.needsNumber()) {
			this.changeTextColor("#000000");
			this.drawTextS($gameParty.numItems(item), x - 70, y, width, "right");
			this.resetTextColor();
		}
	};
	Window_ItemList.prototype.maxCols = function () {
		return 1;
	};

	//カーソルの変更
	Window_ItemList.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_ItemList.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_ItemList.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);
		}
	};

	// ##################################################
	// ############# ステータスウィンドウ #################
	// ##################################################
	Scene_Item.prototype.actorWindowRect = function () {
		const wx = 756;
		const wy = 170;
		const ww = 522;
		const wh = 540;
		return new Rectangle(wx, wy, ww, wh);
	};
	//カーソルが上に来るように
	Window_MenuActor.prototype._createCursorSprite = function () {
		this._cursorSprite = new Sprite();
		for (let i = 0; i < 9; i++) {
			this._cursorSprite.addChild(new Sprite());
		}
		this.addChild(this._cursorSprite);
	};
	Window_MenuActor.prototype._updateCursor = function () {
		this._cursorSprite.alpha = this._makeCursorAlpha();
		this._cursorSprite.visible = this.isOpen() && this.cursorVisible;
		this._cursorSprite.x = this._cursorRect.x + 12;
		this._cursorSprite.y = this._cursorRect.y + 14;
	};
	Scene_Item.prototype.showActorWindow = function () {
		this._actorWindow.show();
		this._actorWindow.activate();
	};

	Window_MenuActor.prototype.initialize = function (rect) {
		Window_MenuStatus.prototype.initialize.call(this, rect);
		//this.hide();
	};

	Window_MenuActor.prototype.maxCols = function () {
		return 2;
	};
	Window_MenuActor.prototype.colSpacing = function () {
		return 9;
	};
	Window_MenuActor.prototype.drawItem = function (index) {
		//this.drawPendingItemBackground(index);
		//this.drawItemImage(index);
		this.drawItemStatus(index);
	};
	Window_MenuActor.prototype.selectLast = function () {
		this.forceSelect(0);
	};

	// 選択時のカーソル
	Window_MenuActor.prototype.loadWindowskin = function () {
		this.windowskin = ImageManager.loadSystem("Window");
		this.menu_item_cursor_skin = ImageManager.loadSystem("menu_item_cursor");
	};
	Window_MenuActor.prototype.rowSpacing = function () {
		return 0;
	};
	Window_MenuActor.prototype.refreshCursor = function () {
		if (this._cursorAll) {
			this.refreshCursorForAll();
		} else if (this.index() >= 0) {
			const rect = this.itemRect(this.index());
			this.setCursorRect(rect.x + 4, rect.y + 4, rect.width, rect.height);
		} else {
			this.setCursorRect(0, 0, 0, 0);
		}
	};

	Window_MenuActor.prototype.itemHeight = function () {
		return 493;
	};

	Window_MenuActor.prototype._refreshCursor = function () {
		const drect = this._cursorRect.clone();
		const srect = { x: 0, y: 0, width: 240, height: 493 };
		const m = 0;
		for (const child of this._cursorSprite.children) {
			child.bitmap = this.menu_item_cursor_skin;
		}
		this._setRectPartsGeometry(this._cursorSprite, srect, drect, m);
	};

	Window_MenuActor.prototype.drawItemStatus = function (index) {
		const actor = this.actor(index);
		const rect = this.itemRect(index);
		const x = rect.x;
		const y = rect.y + Math.floor(rect.height / 2 - this.lineHeight() * 1.5);
		this.drawActorSimpleStatus(actor, x, y, index);
	};
	Window_MenuActor.prototype.drawActorSimpleStatus = function (actor, x, y, index) {
		const lineHeight = this.lineHeight();
		const x2 = x + 10;
		//this.drawActorName(actor, x, y);
		//this.drawActorLevel(actor, x, y + lineHeight * 1);
		//this.drawActorIcons(actor, x, y + lineHeight * 2);
		//this.drawActorClass(actor, x2, y);
		this.placeBasicGauges(actor, x2 + 10, y + lineHeight + 140);

		//faceName, faceIndex, x, y, width, height
		w = 236;
		if (actor._name == "Shirayuki") {
			var bitmap = ImageManager.loadSystem("menu_item_sira");
		} else {
			var bitmap = ImageManager.loadSystem("menu_item_yozu");
		}
		x = 10 + index * 249;
		var y = -182;

		this.contents.blt(bitmap, 0, 0, w, 720, x, y);
		if (!bitmap.isReady()) {
			bitmap.addLoadListener(() => {
				this.refresh(); // 画像がロードされたら再描画
			});
			return;
		}
	};
	Window_MenuActor.prototype._refreshAllParts = function () {
		//this._refreshBack();
		//this._refreshFrame();
		this._refreshCursor();
		this._refreshArrows();
		this._refreshPauseSign();
	};

	//コマンド欄の背景削除
	Window_MenuActor.prototype.drawItemBackground = function (index) {};
	//################## ゲージ ####################
	Window_MenuActor.prototype.placeBasicGauges = function (actor, x, y) {
		this.placeGauge(actor, "hp", x, y);
		this.placeGauge(actor, "mp", x, y + this.gaugeLineHeight() + 34);
		if ($dataSystem.optDisplayTp) {
			this.placeGauge(actor, "tp", x, y + this.gaugeLineHeight() * 2);
		}
	};

	Window_MenuActor.prototype.placeGauge = function (actor, type, x, y) {
		const key = "actor%1-gauge-%2".format(actor.actorId(), type);
		const sprite = this.createInnerSprite(key, Sprite_Gauge_Menu);
		sprite.setup(actor, type);
		sprite.move(x, y);
		sprite.show();
	};

	Scene_Item.prototype.hideActorWindow = function () {
		//		this._actorWindow.hide();
		this._actorWindow.deselect();
		this._actorWindow.deactivate();
	};

	// ############################################
	// ############# ヘルプウィンドウ ##############
	// ############################################
	Scene_Item.prototype.helpWindowRect = function () {
		const wx = 20;
		const wy = 550;
		const ww = 740;
		const wh = this.helpAreaHeight() + 10;
		return new Rectangle(wx, wy, ww, wh);
	};

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

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

			this.contents.fontFace = "serif";
			this.changeTextColor("#000000");
			this.contents.fontSize = 20;
			this.drawTextM("概要", 16, -3, 80, "center");

			this.changeTextColor("#FFFFFF");
			this.drawTextExS(this._text, rect.x + 100, rect.y, rect.width);
		};
		this.addWindow(this._helpWindow);
	};
})();
