(function () {
	// まー装備選択の部分が奇怪だから、いつか使う時用に残しとく
	const _Scene_Base_createWindowLayer = Scene_Base.prototype.createWindowLayer;
	Scene_Equip.prototype.createWindowLayer = function () {
		_Scene_Base_createWindowLayer.call(this);
		this._windowLayer.x = 0;
		this._windowLayer.y = 0;
	};

	//#####################　メニューの背景変える　#####################
	Scene_Equip.prototype.createBackground = function () {
		this._backgroundSprite = new Sprite();
		var name = $gameParty.menuActor()._name;
		if (name == "Shirayuki") {
			this._backgroundSprite.bitmap = ImageManager.loadSystem("menu_equip_back_sira");
		} else if (name == "Yotsuru") {
			this._backgroundSprite.bitmap = ImageManager.loadSystem("menu_equip_back_yozu");
		}

		this.addChild(this._backgroundSprite);
	};
	Scene_Equip.prototype.create = function () {
		Scene_MenuBase.prototype.create.call(this);
		this.createStatusWindow();
		this.createCommandWindow();
		this.createSlotWindow();
		this.createItemWindow();
		this.createHelpWindow();
		this.refreshActor();
	};

	Scene_Equip.prototype.createHelpWindow = function () {
		const rect = this.helpWindowRect();
		this._helpWindow = new Window_EquipHelp(rect);
		this.addWindow(this._helpWindow);
	};

	//最強と全て外すを消去し、別キャラクター追加
	Window_EquipCommand.prototype.makeCommandList = function () {
		this.addCommand(TextManager.equip2, "equip");
		this.addCommand("別キャラクター", "pagedown");
	};

	Scene_Equip.prototype.commandWindowRect = function () {
		const wx = 270;
		const wy = 0;
		const ww = 500;
		const wh = 80;
		return new Rectangle(wx, wy, ww, wh);
	};
	Window_EquipCommand.prototype.maxCols = function () {
		return 2;
	};

	// ###########################################################
	// ###################### 装備欄ウィンドウ ####################
	// ###########################################################
	Scene_Equip.prototype.slotWindowRect = function () {
		const wx = 256;
		const wy = 124;
		const ww = Graphics.boxWidth - wx;
		const wh = Graphics.boxHeight - wy;
		return new Rectangle(wx, wy, ww, wh);
	};

	//コマンド変更
	Scene_Equip.prototype.createCommandWindow = function () {
		const rect = this.commandWindowRect();
		this._commandWindow = new Window_EquipCommand(rect);
		this._commandWindow.setHelpWindow(this._helpWindow);
		this._commandWindow.setHandler("equip", this.commandEquip.bind(this));
		//キーボードでの誤操作防止
		//this._commandWindow.setHandler("optimize", this.commandOptimize.bind(this));
		//this._commandWindow.setHandler("clear", this.commandClear.bind(this));
		this._commandWindow.setHandler("cancel", this.popScene.bind(this));
		this._commandWindow.setHandler("pagedown", this.nextActor.bind(this));
		this._commandWindow.setHandler("pageup", this.previousActor.bind(this));
		this.addWindow(this._commandWindow);
	};

	//装備欄
	Window_EquipSlot.prototype.maxCols = function () {
		return 2;
	};
	Window_EquipSlot.prototype.itemRect = function (index) {
		const maxCols = this.maxCols();
		var itemWidth = 314;
		const itemHeight = this.itemHeight() + 16;
		const colSpacing = this.colSpacing();
		const rowSpacing = this.rowSpacing() + 16;
		var col = 1;
		var row = index - 1;
		var x = 4;
		if (!index) {
			col = 0;
			row = 2;
			itemWidth = 490;
		} else {
			x += 680;
		}

		var y = row * itemHeight + rowSpacing / 2 - this.scrollBaseY() - 8;

		if (!index) {
			y = 515;
		}

		if (3 < index) {
			y += 41;
		}

		if (6 < index) {
			y += 44;
		}

		const width = itemWidth - colSpacing;
		const height = itemHeight - rowSpacing;
		return new Rectangle(x, y, width, height);
	};

	Window_EquipSlot.prototype.cursorDown = function (wrap) {
		const index = this.index();
		const maxItems = this.maxItems();
		const maxCols = 1;
		if (index < maxItems - maxCols || (wrap && maxCols === 1)) {
			this.CBR_index = index;
			this.smoothSelect((index + maxCols) % maxItems);
		}
	};
	Window_EquipSlot.prototype.cursorUp = function (wrap) {
		const index = Math.max(0, this.index());
		const maxItems = this.maxItems();
		const maxCols = 1;
		if (index >= maxCols || (wrap && maxCols === 1)) {
			this.CBR_index = index;
			this.smoothSelect((index - maxCols + maxItems) % maxItems);
		}
	};

	Window_EquipSlot.prototype.cursorRight = function (wrap) {
		const index = this.index();
		const maxItems = this.maxItems();
		const maxCols = this.maxCols();
		const horizontal = this.isHorizontal();

		if (index === 0) {
			if (maxCols >= 2 && (index < maxItems - 1 || (wrap && horizontal))) {
				this.smoothSelect(this.CBR_index || 1);
				this.CBR_index = index;
			}
		}
	};

	Window_EquipSlot.prototype.cursorLeft = function (wrap) {
		const index = Math.max(0, this.index());
		const maxItems = this.maxItems();
		const maxCols = this.maxCols();
		const horizontal = this.isHorizontal();

		if (maxCols >= 2 && (index > 0 || (wrap && horizontal))) {
			//武器選択する時は位置を記録する
			this.CBR_index = index;
			this.smoothSelect(0);
		}
	};

	// ###########################################################
	// ##################### 装備選択ウィンドウ ###################
	// ###########################################################
	Scene_Equip.prototype.createItemWindow = function () {
		const rect = this.itemWindowRect();
		this._itemWindow = new Window_EquipItem(rect);
		this._itemWindow.setHelpWindow(this._helpWindow);
		this._itemWindow.setStatusWindow(this._statusWindow);
		this._itemWindow.setHandler("ok", this.onItemOk.bind(this));
		this._itemWindow.setHandler("cancel", this.onItemCancel.bind(this));
		this._itemWindow.hide();
		this._slotWindow.setItemWindow(this._itemWindow);
		this.addWindow(this._itemWindow);
	};

	Scene_Equip.prototype.itemWindowRect = function () {
		const wx = 0;
		const wh = 659;
		const wy = Graphics.height - wh;
		const ww = Graphics.width;

		return new Rectangle(wx, wy, ww, wh);
	};

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

	// Window_EquipItem.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_EquipItem.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();
	// 	}
	// };

	// //56×200にする
	// Window_EquipItem.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_EquipItem.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);
		}
	};

	// ウィンドウの背景を変更する

	const _Window_ItemList__createAllParts = Window_EquipItem.prototype._createAllParts;
	Window_EquipItem.prototype._createAllParts = function () {
		_Window_ItemList__createAllParts.call(this);
		this._refreshCustomBackground();
	};

	Window_EquipItem.prototype._refreshCustomBackground = function () {
		if (!this._customBackSprite) {
			this._customBackSprite = new Sprite();
			this.addChildAt(this._customBackSprite, 0);
		}
		this._customBackSprite.bitmap = ImageManager.loadSystem("menu_equip_item_back"); // test.pngをロード
		this._customBackSprite.x = 0;
		this._customBackSprite.y = 0;
		this._customBackSprite.setFrame(0, 0, this.width, this.height);
	};

	Object.defineProperty(Window_EquipItem.prototype, "innerWidth", {
		get: function () {
			return 500;
		},
		configurable: true
	});

	Object.defineProperty(Window_EquipItem.prototype, "innerRect", {
		get: function () {
			return new Rectangle((1280 - this.innerWidth) / 2, this._padding, this.innerWidth, this.innerHeight);
		},
		configurable: true
	});
	Window_EquipItem.prototype._createClientArea = function () {
		this._clientArea = new Sprite();
		this._clientArea.filters = [new PIXI.filters.AlphaFilter()];
		this._clientArea.filterArea = new Rectangle();
		this._clientArea.move((1280 - this.innerWidth) / 2, this._padding);
		this.addChild(this._clientArea);
	};
	Window_EquipItem.prototype._updateClientArea = function () {
		const pad = this._padding;
		this._clientArea.move((1280 - this.innerWidth) / 2, pad);
		this._clientArea.x = (1280 - this.innerWidth) / 2 - this.origin.x;
		this._clientArea.y = pad - this.origin.y;
		if (this.innerWidth > 0 && this.innerHeight > 0) {
			this._clientArea.visible = this.isOpen();
		} else {
			this._clientArea.visible = false;
		}
	};

	Window_Selectable.prototype.hitTest = function (x, y) {
		if (this.innerRect.contains(x, y)) {
			const cx = this.origin.x + x - (1280 - this.innerWidth) / 2;
			const cy = this.origin.y + y - this.padding;
			const topIndex = this.topIndex();
			for (let i = 0; i < this.maxVisibleItems(); i++) {
				const index = topIndex + i;
				if (index < this.maxItems()) {
					const rect = this.itemRect(index);
					if (rect.contains(cx, cy)) {
						return index;
					}
				}
			}
		}
		return -1;
	};

	//ウィンドウのバックグラウンド
	Window_EquipItem.prototype._refreshAllParts = function () {
		this._refreshBack();
		//this._refreshFrame();
		this._refreshCursor();
		this._refreshArrows();
		this._refreshPauseSign();
	};

	// ウィンドウ背景の読み込み カーソルの変更
	Window_EquipItem.prototype.loadWindowskin = function () {
		this.windowskin = ImageManager.loadSystem("Window");
		this.CBR_item_back_skin = ImageManager.loadSystem("menu_equip_item_back");

		this.menu_cursor_skin = ImageManager.loadSystem("menu_item_selected");
		this.menu_item_skin = ImageManager.loadSystem("menu_item_selected_2");
	};

	Window_EquipItem.prototype._refreshBack = function () {
		const sprite = this._backSprite;
		sprite.bitmap = this.CBR_item_back_skin;
		sprite.alpha = 1;
		sprite.setFrame(0, 0, 1280, 720);
		sprite.move(0, 0);
	};

	//ウィンドウの背景を透過させる奴
	WindowLayer.prototype.render = function render(renderer) {
		if (!this.visible) {
			return;
		}
		this.children.forEach((win) => {
			if (win instanceof Window_EquipItem) {
				win._isWindow = false; // RPGツクールのウィンドウ描画を無効化
			}
			if (win instanceof Window_EquipHelp) {
				win._isWindow = false; // RPGツクールのウィンドウ描画を無効化
			}
		});

		const graphics = new PIXI.Graphics();
		const gl = renderer.gl;
		const children = this.children.clone();

		renderer.framebuffer.forceStencil();
		graphics.transform = this.transform;
		renderer.batch.flush();
		gl.enable(gl.STENCIL_TEST);

		while (children.length > 0) {
			const win = children.pop();
			if (win._isWindow && win.visible && win.openness > 0) {
				gl.stencilFunc(gl.EQUAL, 0, ~0);
				gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
				win.render(renderer);
				renderer.batch.flush();
				graphics.clear();
				win.drawShape(graphics);
				gl.stencilFunc(gl.ALWAYS, 1, ~0);
				gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
				gl.blendFunc(gl.ZERO, gl.ONE);
				graphics.render(renderer);
				renderer.batch.flush();
				gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
			}
		}

		gl.disable(gl.STENCIL_TEST);
		gl.clear(gl.STENCIL_BUFFER_BIT);
		gl.clearStencil(0);
		renderer.batch.flush();

		for (const child of this.children) {
			if (!child._isWindow && child.visible) {
				child.render(renderer);
			}
		}

		renderer.batch.flush();
	};

	// ###########################################################
	// ###################### ヘルプウィンドウ ####################
	// ###########################################################
	Scene_Equip.prototype.createHelpWindow = function () {
		const rect = this.helpWindowRect();
		this._helpWindow = new Window_EquipHelp(rect);
		this._helpWindow.hide();
		this._itemWindow.hide();
		this.addWindow(this._helpWindow);
	};

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