CBR_extra_hscene_list = null;
DataManager._databaseFiles.push({ name: "CBR_extra_hscene_list", src: "CBR_extra_hscene_list.json" });

CBR_extra_pic_list = null;
DataManager._databaseFiles.push({ name: "CBR_extra_pic_list", src: "CBR_extra_pic_list.json" });

CBR_extra_video_list = null;
DataManager._databaseFiles.push({ name: "CBR_extra_video_list", src: "CBR_extra_video_list.json" });

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

Scene_Extra.prototype = Object.create(Scene_ItemBase.prototype);
Scene_Extra.prototype.constructor = Scene_Extra;

(function () {
	Scene_Menu.prototype.commandExtra = function () {
		SceneManager.push(Scene_Extra);
	};

	Scene_Extra.prototype.initialize = function () {
		Scene_MenuBase.prototype.initialize.call(this);
	};

	Scene_Extra.prototype.do_common = function (id) {
		const common = $dataCommonEvents[id];
		if (!common) return;

		// Interpreter（イベント処理機構）を新規作成
		const interpreter = new Game_Interpreter();
		interpreter.setup(common.list);

		// タイトルで定期的に更新されるように保存
		this._extraInterpreter = interpreter;
	};

	Scene_Extra.prototype.create = function () {
		Scene_MenuBase.prototype.create.call(this);
		DataManager.loadAllSavefileImages();
		this.createHelpWindow();
		this.createListWindow();
		this.createCategoryWindow();
		//this._helpWindow.setText(this.helpWindowText());

		this._pic_videoWindow = new Window_Extra_pic_video(new Rectangle(0, 0, Graphics.boxWidth, Graphics.boxHeight));
		this._pic_videoWindow.setHandler("ok", this.onPicOk.bind(this));
		this._pic_videoWindow.setHandler("cancel", this.onPicCancel.bind(this));

		this._pic_videoWindow.hide();
		this._itemWindow.setPic_VideoWindow(this._pic_videoWindow);
		this._pic_videoWindow.setItemWindow(this._pic_videoWindow);
		this.addWindow(this._pic_videoWindow);

		// 回想から戻ってきた時用
		if (SceneManager._CBR_kaisou_val) {
			this._categoryWindow.select(1);
			this._categoryWindow.deactivate();

			this._itemWindow.setCategory("scene");
			this._itemWindow._page = SceneManager._CBR_kaisou_val.page;
			this._itemWindow.select(SceneManager._CBR_kaisou_val.index);
			this._itemWindow.activate();

			this._itemWindow._kaisou_page = SceneManager._CBR_kaisou_val.page;
			SceneManager._CBR_kaisou_val = null;
			this._itemWindow.refresh();
		} else if (SceneManager._CBR_kaisou_video_val) {
			this._categoryWindow.select(2);
			this._categoryWindow.deactivate();

			this._itemWindow.setCategory("video");
			this._itemWindow._page = SceneManager._CBR_kaisou_video_val.page;
			this._itemWindow.select(SceneManager._CBR_kaisou_video_val.index);
			this._itemWindow.activate();

			this._itemWindow._kaisou_page = SceneManager._CBR_kaisou_video_val.page;
			SceneManager._CBR_kaisou_video_val = null;
			this._itemWindow.refresh();
		}

		// ピクチャコンテナ（描画用）
		this._pictureContainer = new Sprite();
		this.addChild(this._pictureContainer);

		// ピクチャスプライト群
		this._pictureSprites = [];
		for (let i = 1; i <= 30; i++) {
			const sprite = new Sprite_Picture(i);
			this._pictureContainer.addChild(sprite);
			this._pictureSprites.push(sprite);
		}
	};

	// ピクチャ用
	Scene_Extra.prototype.update = function () {
		Scene_ItemBase.prototype.update.call(this);

		if (this._extraInterpreter && this._extraInterpreter._list) {
			this._extraInterpreter.update();
		}

		$gameScreen.update();
	};
	// ピクチャ用
	Scene_Extra.prototype.stop = function () {
		Scene_ItemBase.prototype.stop.call(this);
		$gameScreen.clearPictures();
	};

	Scene_Extra.prototype.createBackground = function () {
		this._backgroundSprite = new Sprite();
		this._backgroundSprite.bitmap = ImageManager.loadSystem("menu_extra_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("Extra", 22, 20, 200, 22, "center");
		this.addChild(this._backgroundTextSprite);

		this._background_page_sprite = new Sprite();
		this._background_page_sprite.bitmap = ImageManager.loadSystem("menu_extra_page");
		this.addChild(this._background_page_sprite);

		this._pageSprite = new Sprite(new Bitmap(300, 50));
		this._pageSprite.x = 900;
		this._pageSprite.y = 20;
		this.addChild(this._pageSprite);
	};

	Scene_Extra.prototype.createHelpWindow = function () {
		const rect = this.helpWindowRect();
		this._helpWindow = new Window_ExtraHelp(rect);
		this.addWindow(this._helpWindow);
	};
	Scene_Extra.prototype.helpWindowRect = function () {
		const wx = 0;
		const wy = 610;
		const ww = Graphics.boxWidth;
		const wh = 110;
		return new Rectangle(wx, wy, ww, wh);
	};

	// もうヘルプウィンドウの更新は全部 EventListでやる、カテゴリはリストに渡すだけ
	Scene_Extra.prototype.createListWindow = function () {
		const rect = this.listWindowRect();
		this._itemWindow = new Window_ExtraList(rect);
		this._itemWindow.setHandler("ok", this.onItemOk.bind(this));
		this._itemWindow.setHandler("cancel", this.onItemCancel.bind(this));
		this._itemWindow.setHelpWindow(this._helpWindow);
		this._itemWindow.setPageSprite(this._pageSprite);
		this._itemWindow.refresh();
		this.addWindow(this._itemWindow);
	};

	Scene_Extra.prototype.onPicOk = function () {
		// this._itemWindow.onOk();
		// this._itemWindow.deselect();
		// this._pic_videoWindow.show();
		// this._pic_videoWindow.activate();
	};
	Scene_Extra.prototype.onPicCancel = function () {
		this._pic_videoWindow.deselect();
		this._pic_videoWindow.hide();
		this._itemWindow.activate();
		$gameScreen.clearPictures();
	};
	Scene_Extra.prototype.onItemOk = function () {
		if (this._itemWindow._category == "cg") {
			this._itemWindow.onOk();
			this._itemWindow.deactivate();
			this._pic_videoWindow.show();
			this._pic_videoWindow.activate();
		} else if (this._itemWindow._category == "scene") {
			var data_index = this._itemWindow._list[this._itemWindow.index() + this._itemWindow._page * this._itemWindow._maxPageItems];
			info = CBR_extra_hscene_list[data_index];
			$gameTemp.reserveCommonEvent(info.common_id);
			$gameTemp.reserveCommonEvent(1177);
			if ($gameTemp.isCommonEventReserved()) {
				SceneManager._CBR_kaisou_val = {
					page: this._itemWindow._page,
					index: this._itemWindow.index()
				};
				$gameSwitches.setValue(106, true);
				$gamePlayer.reserveTransfer(3, 10, 5, 2, 0);
				SceneManager.push(Scene_Map);
			}
		} else if (this._itemWindow._category == "video") {
			// this._itemWindow.onOk();
			// this._itemWindow.deactivate();
			// this._pic_videoWindow.show();
			// this._pic_videoWindow.activate();
			var data_index = this._itemWindow._list[this._itemWindow.index() + this._itemWindow._page * this._itemWindow._maxPageItems];
			info = CBR_extra_hscene_list[data_index];
			$gameVariables.setValue(404, info.dispIdx);
			$gameTemp.reserveCommonEvent(916);
			$gameTemp.reserveCommonEvent(1177);
			if ($gameTemp.isCommonEventReserved()) {
				SceneManager._CBR_kaisou_video_val = {
					page: this._itemWindow._page,
					index: this._itemWindow.index()
				};
				$gameSwitches.setValue(106, true);
				$gamePlayer.reserveTransfer(3, 10, 5, 2, 0);
				SceneManager.push(Scene_Map);
			}
		}
	};

	Game_System.prototype.isSaveEnabled = function () {
		return this._saveEnabled && !$gameSwitches.value(106);
	};

	Scene_Extra.prototype.onItemCancel = function () {
		this._itemWindow.deselect();
		this._categoryWindow.activate();
		this._helpWindow.hide();
	};

	Scene_Extra.prototype.createCategoryWindow = function () {
		const rect = this.categoryWindowRect();
		this._categoryWindow = new Window_EvCategory(rect);
		//this._categoryWindow.setHelpWindow(this._helpWindow);
		this._categoryWindow.setItemWindow(this._itemWindow);
		//this._categoryWindow.setHelpWindow(this._helpWindow);
		this._categoryWindow.setHandler("ok", this.onCategoryOk.bind(this));
		this._categoryWindow.setHandler("cancel", this.popScene.bind(this));
		this.addWindow(this._categoryWindow);
	};

	Scene_Extra.prototype.popScene = function () {
		SceneManager.goto(Scene_Title);
	};

	Scene_Extra.prototype.listWindowRect = function () {
		const wx = 0;
		const wy = 80;
		const ww = Graphics.boxWidth;
		const wh = 500;
		return new Rectangle(wx, wy, ww, wh);
	};

	Scene_Extra.prototype.helpWindowText = function () {
		return "";
	};

	Scene_Extra.prototype.helpWindowText = function () {
		return TextManager.saveMessage;
	};

	Scene_Extra.prototype.onCategoryOk = function () {
		this._itemWindow.activate();
		this._itemWindow.select(0);
		if (this._categoryWindow.currentSymbol() == "scene") {
			this._helpWindow.show();
		} else {
			this._helpWindow.hide();
		}
	};
	// //コモンイベントを実行
	// Scene_Extra.prototype.onItemOk = function () {
	// 	$gameVariables.setValue(10, this._itemWindow._list[this._itemWindow.index()]);
	// 	$gameTemp.reserveCommonEvent(23);
	// 	SceneManager.goto(Scene_Map);
	// };

	// ####################################################
	// ################# アイテムの種類 ####################
	// ####################################################
	// カテゴリーウィンドウ
	function Window_EvCategory() {
		this.initialize(...arguments);
	}

	Window_EvCategory.prototype = Object.create(Window_HorzCommand.prototype);
	Window_EvCategory.prototype.constructor = Window_EvCategory;

	Scene_Extra.prototype.categoryWindowRect = function () {
		const wx = 270;
		const wy = -6;
		const ww = 600;
		const wh = 58 + 8;
		return new Rectangle(wx, wy, ww, wh);
	};

	Window_EvCategory.prototype.initialize = function (rect) {
		Window_HorzCommand.prototype.initialize.call(this, rect);
		this._padding = 0;
	};

	//カテゴリーはアイテムと大事な物だけにする
	Window_EvCategory.prototype.makeCommandList = function () {
		this.addCommand("CG鑑賞", "cg");
		this.addCommand("Ｈシーン鑑賞", "scene");
		this.addCommand("Ｈアニメ鑑賞", "video");
	};
	Window_EvCategory.prototype.setItemWindow = function (itemWindow) {
		this._itemWindow = itemWindow;
	};

	// 変更があったらカテゴリーを更新する
	Window_EvCategory.prototype.update = function () {
		Window_HorzCommand.prototype.update.call(this);
		if (this._itemWindow) {
			this._itemWindow.setCategory(this.currentSymbol());
		}
	};

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

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

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

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

		this.refreshCursor();
		this.callUpdateHelp();
	};
	//↑のセレクトでの不具合を出ないようにする
	Window_EvCategory.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);
			}
		}
	};

	// ########################################################
	// ################### 一覧ウィンドウ ######################
	// ########################################################
	function Window_ExtraList() {
		this.initialize(...arguments);
	}
	Window_ExtraList.prototype = Object.create(Window_Selectable.prototype);
	Window_ExtraList.prototype.constructor = Window_ExtraList;

	Scene_Extra.prototype.listWindowRect = function () {
		const ww = (280 + 12) * 4;
		const wx = (Graphics.boxWidth - ww) / 2;
		const wy = 60;
		const wh = 560;
		return new Rectangle(wx, wy, ww, wh);
	};
	Window_ExtraList.prototype.lineHeight = function () {
		return 169;
	};

	Window_ExtraList.prototype.setPic_VideoWindow = function (pic_videoWindow) {
		this._pic_videoWindow = pic_videoWindow;
	};

	Window_ExtraList.prototype.setPageSprite = function (sprite) {
		this._page_sprite = sprite;
	};

	const _Window_ExtraList_update = Window_ExtraList.prototype.update;
	Window_ExtraList.prototype.update = function () {
		_Window_ExtraList_update.call(this);

		if (this.active && TouchInput.isTriggered()) {
			const x = TouchInput.x;
			const y = TouchInput.y;

			if (302 <= y && y <= 422) {
				if (1 <= x && x <= 60) {
					if (this._page <= 0) {
						this._page = this._maxPage;
					} else {
						this._page -= 1;
					}
					this.select(0);
					this._pageItems = Math.min(this._maxPageItems, this._list.length - this._page * this._maxPageItems);
					// セレクトは一番右にする
					this.smoothSelect(0);

					this.playCursorSound();
					this.refresh();
				} else if (1221 <= x && x <= 1280) {
					if (this._maxPage <= this._page) {
						this._page = 0;
					} else {
						this._page += 1;
					}
					this.select(0);
					this._pageItems = Math.min(this._maxPageItems, this._list.length - this._page * this._maxPageItems);
					// セレクトは一番左にする
					this.smoothSelect(0);
					this.playCursorSound();
					this.refresh();
				}
			}
		}
	};

	Window_ExtraList.prototype.onOk = function () {
		if (this._category === "cg") {
			var data_index = this._list[this.index() + this._page * this._maxPageItems];
			this._pic_videoWindow.setPic_info(CBR_extra_pic_list[data_index]);
		} else if (this._category === "scene") {
		} else if (this._category === "video") {
			// var data_index = this._list[this.index() + this._page * this.maxItems()];
			// this._pic_videoWindow.setVideo_info(CBR_extra_video_list[data_index]);
			// SceneManager._scene.do_common(916);
		}
	};
	Window_ExtraList.prototype.initialize = function (rect) {
		Window_Selectable.prototype.initialize.call(this, rect);
		//最初に0番目って指定、commandではデフォでやってるけど

		// デフォではアクティブに
		this._category = "cg";
		// ページ化用
		this._page = this._kaisou_page || 0;

		this._maxPageItems = 12;
		this.makeList();
	};
	Window_ExtraList.prototype.makeList = function () {
		this._list = [];

		if (this._category == "cg") {
			// this._list = $gameVariables.value(521) || [];
			for (var i = 1, len = CBR_extra_pic_list.length; i < len; i++) {
				this._list.push(i);
			}
		} else if (this._category == "scene") {
			for (var i = 1, len = CBR_extra_hscene_list.length; i < len; i++) {
				this._list.push(i);
			}
		} else if (this._category == "video") {
			for (var i = 1, len = CBR_extra_video_list.length; i < len; i++) {
				this._list.push(i);
			}
		}

		// ページをリセット
		this._page = this._kaisou_page || 0;
		this._kaisou_page = null;
		// 最大ページ
		this._maxPage = Math.floor(this._list.length / this._maxPageItems);
		// 今のページに表示するアイテム数
		this._pageItems = Math.min(this._maxPageItems, this._list.length - this._page * this._maxPageItems);

		if (this._maxPage === 0) {
			SceneManager._scene._background_page_sprite.hide();
		} else {
			SceneManager._scene._background_page_sprite.show();
		}

		// ヘルプウィンドウの表示非表示
		if (this.parent) {
			if (this._category == "scene") {
				this.parent.parent._helpWindow.show();
			} else {
				this.parent.parent._helpWindow.hide();
			}
		}
	};

	Window_ExtraList.prototype.maxCols = function () {
		return 4;
	};
	Window_ExtraList.prototype.maxItems = function () {
		return this._pageItems;
	};
	Window_ExtraList.prototype.itemPadding = function () {
		return 0;
	};
	Window_ExtraList.prototype.updatePadding = function () {
		this.padding = 0;
	};
	Window_ExtraList.prototype.colSpacing = function () {
		return 12;
	};

	Window_ExtraList.prototype.rowSpacing = function () {
		return 0;
	};

	Window_ExtraList.prototype.setCategory = function (category) {
		if (this._category !== category) {
			this._category = category;

			this.makeList();

			//this.callUpdateHelp();
			this.refresh();

			// 毎回一番上に戻す
			// this.select(0);
			// this.scrollTo(0, 0);
		}
	};

	//右のウィンドウにデータを渡す
	Window_ExtraList.prototype.callUpdateHelp = function () {
		if (this.active && this._helpWindow) {
			this.updateHelp();
		}
	};
	Window_ExtraList.prototype.updateHelp = function () {
		this._helpWindow.setItem(CBR_extra_hscene_list[this._list[this.index() + this._page * this._maxPageItems]]);
	};

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

		// ページ化用 ページの切り替えの時
		if ((index + 1) % this.maxCols() == 0 || index + 1 == this._pageItems) {
			if (this._maxPage <= this._page) {
				this._page = 0;
			} else {
				this._page += 1;
			}

			this._pageItems = Math.min(this._maxPageItems, this._list.length - this._page * this._maxPageItems);

			this.playCursorSound();

			// セレクトは一番左にする
			this.smoothSelect(Math.min(index - (index % this.maxCols()), this._pageItems - ((this._pageItems - 1) % this.maxCols()) - 1));
		} else if (maxCols >= 2 && (index < maxItems - 1 || (wrap && horizontal))) {
			this.smoothSelect((index + 1) % maxItems);
		}

		this.refresh();
	};

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

		// ページ化用 ページの切り替えの時
		if (index % this.maxCols() == 0) {
			if (this._page <= 0) {
				this._page = this._maxPage;
			} else {
				this._page -= 1;
			}

			this._pageItems = Math.min(this._maxPageItems, this._list.length - this._page * this._maxPageItems);
			// セレクトは一番右にする
			this.smoothSelect(Math.min(index + 3, this._pageItems - 1));

			this.playCursorSound();
		} else if (maxCols >= 2 && (index > 0 || (wrap && horizontal))) {
			this.smoothSelect((index - 1 + maxItems) % maxItems);
		}

		this.refresh();
	};

	Window_ExtraList.prototype.paint = function () {
		if (this.contents) {
			var reload_flag = false;
			for (let i = 0; i < this._maxPageItems; i++) {
				var data_index = this._list[i + this._page * this._maxPageItems];
				if (data_index === undefined) {
					continue;
				}
				var info;
				if (this._category == "cg") {
					info = CBR_extra_pic_list[data_index];
				} else if (this._category == "scene") {
					info = CBR_extra_hscene_list[data_index];
				} else if (this._category == "video") {
					info = CBR_extra_video_list[data_index];
				}
				const bitmap = ImageManager.loadSystem(info.img);
				if (!bitmap.isReady()) {
					reload_flag = true;
					bitmap.addLoadListener(() => {
						this.refresh(); // 画像がロードされたら再描画
					});
					return;
				}
			}
			if (!reload_flag) {
				this.contents.clear();
				this.contentsBack.clear();
				this.drawAllItems();
			}
		}
	};

	// ここでitemを描写する前に画像がロードされてるのか調べる
	Window_ExtraList.prototype.drawAllItems = function () {
		// 量が多いでロード処理はココでやる
		var reload_flag = false;

		for (let i = 0; i < this._maxPageItems; i++) {
			var data_index = this._list[i + this._page * this._maxPageItems];
			if (data_index === undefined) {
				continue;
			}
			var info;
			if (this._category == "cg") {
				info = CBR_extra_pic_list[data_index];
			} else if (this._category == "scene") {
				info = CBR_extra_hscene_list[data_index];
			} else if (this._category == "video") {
				info = CBR_extra_video_list[data_index];
			}
			const bitmap = ImageManager.loadSystem(info.img);
			if (!bitmap.isReady()) {
				reload_flag = true;
				bitmap.addLoadListener(() => {
					this.refresh(); // 画像がロードされたら再描画
				});
				return;
			}
		}
		if (reload_flag) {
			return;
		}

		this._pageItems = Math.min(this._maxPageItems, this._list.length - this._page * this._maxPageItems);

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

		// ページ描写
		var b = this._page_sprite.bitmap;
		b.clear();
		b.fontFace = "serif";
		b.fontSize = 32;
		b.textColor = "#000000";
		b.drawTextM(`${this._page + 1} / ${this._maxPage + 1} page`, 0, 0, 300, 32, "right");
	};

	// 回想チラつき対処作りかけ
	// Window_ExtraList.prototype.paint = function () {
	// 	if (this.contents) {
	// 		var flag = false;
	// 		for (var i = 0; i < 12; i++) {
	// 			var data_index = this._list[i + this._page * this._maxPageItems];
	// 			info = CBR_extra_pic_list[data_index];
	// 			if (info) {
	// 				const bitmap = ImageManager.loadSystem(info.img);
	// 				if (bitmap.isReady()) {
	// 					flag = true;
	// 				}
	// 			}
	// 		}

	// 		if (!flag) {
	// 			this.contents.clear();
	// 			this.contentsBack.clear();
	// 		}
	// 		this.drawAllItems();
	// 	}
	// };

	Window_ExtraList.prototype.drawItem = function (index) {
		var data_index = this._list[index + this._page * this._maxPageItems];

		var info;
		if (this._category == "cg") {
			const ary = $gameVariables.value(521) || [];
			if (!$gameSwitches.value(778) && !ary.includes(data_index)) {
				return;
			} else {
				info = CBR_extra_pic_list[data_index];
			}
		} else if (this._category == "scene") {
			info = CBR_extra_hscene_list[data_index];
			var s_id = info.switch_id;
			if (!$gameSwitches.value(778) && !$gameSwitches.value(s_id)) {
				return;
			}
		} else if (this._category == "video") {
			const ary = $gameVariables.value(522) || [];
			if (!$gameSwitches.value(778) && !ary.includes(data_index)) {
				return;
			} else {
				info = CBR_extra_video_list[data_index];
			}
		}
		const rect = this.itemRectWithPadding(index);
		this.resetTextColor();
		//this.changePaintOpacity(this.isEnabled(savefileId));
		//this.drawTitle(savefileId, rect.x, rect.y + 4);

		if (info) {
			this.drawContents(info, rect);
		}
	};

	Window_ExtraList.prototype.drawContents = function (info, rect) {
		const bitmap = ImageManager.loadSystem(info.img);
		this.contents.blt(bitmap, 0, 0, 256, 144, rect.x + 12, rect.y + 25);

		// this.drawText(info.dispIdx, rect.x, rect.y + 2, 400, "left");
	};

	// ################# カーソルの変更 ##################
	Window_ExtraList.prototype.loadWindowskin = function () {
		this.windowskin = ImageManager.loadSystem("Window");
		this.menu_cursor_skin = ImageManager.loadSystem("menu_extra_cursor");
		this.menu_item_skin = ImageManager.loadSystem("menu_extra_select");

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

	Window_ExtraList.prototype._refreshCursor = function () {
		const drect = this._cursorRect.clone();
		const srect = { x: 0, y: 0, width: 280, height: 177 };
		const m = 0;
		for (const child of this._cursorSprite.children) {
			child.bitmap = this.menu_cursor_skin;
		}
		this._setRectPartsGeometry(this._cursorSprite, srect, drect, m);
	};

	// //56×200にする
	// Window_ExtraList.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_ExtraList.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, 280, 177, dx, dy, dw, dh);
		}
	};

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

	// 未開放の回想はブザー
	Window_ExtraList.prototype.isCurrentItemEnabled = function () {
		if (this._category == "scene") {
			var data_index = this._list[this.index() + this._page * this.maxItems()];
			info = CBR_extra_hscene_list[data_index];
			var s_id = info.switch_id;
			if (!$gameSwitches.value(778) && !$gameSwitches.value(s_id)) {
				return false;
			}
		} else if (this._category == "cg") {
			var data_index = this._list[this.index() + this._page * this.maxItems()];
			const ary = $gameVariables.value(521) || [];
			if (!$gameSwitches.value(778) && !ary.includes(data_index)) {
				return false;
			}
		} else if (this._category == "video") {
			var data_index = this._list[this.index() + this._page * this.maxItems()];
			const ary = $gameVariables.value(522) || [];
			if (!$gameSwitches.value(778) && !ary.includes(data_index)) {
				return false;
			}
		}
		return true;
	};

	// ###########################################################
	// ##################### 移動先ウィンドウ #####################
	// ###########################################################

	/*
	
	//CG鑑賞案
	{
    "dispIdx": 1,（1からスタート）
    "name": "eva_a001",
    "sabun_list":["eva_a001_001", "eva_a001_002", "eva_a001_003"],
    "img": "サムネ画像名（.pngはなし）256×144"
    },

	//Ｈアニメ鑑賞モード
	{
        "dispIdx": 表示するインデックスの数値,（1からスタート）
        "common_id": コモンイベントのid,
        "switch_id": オープンするswitchのid,
        "img": "サムネ画像名（.pngはなし）256×144"
    },
	*/
	function Window_ExtraHelp() {
		this.initialize(...arguments);
	}
	Window_ExtraHelp.prototype = Object.create(Window_Help.prototype);
	Window_ExtraHelp.prototype.constructor = Window_ExtraHelp;

	Window_ExtraHelp.prototype.setItem = function (item) {
		if (item) {
			this.setText(item ? item.text : "");
		}
	};

	Window_ExtraHelp.prototype.setText = function (text) {
		if (this._text !== text) {
			this._text = text;
			this.refresh();
		}
	};

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

		var bitmap = ImageManager.loadSystem("menu_extra_help");
		this.contents.blt(bitmap, 0, 0, 1150, 73, 54, 4);
		if (!bitmap.isReady()) {
			bitmap.addLoadListener(() => {
				this.refresh(); // 画像がロードされたら再描画
			});
			return;
		}
		this.drawTextEx(this._text, rect.x + 60, rect.y + 4, rect.width);
	};

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

	function Window_Extra_pic_video() {
		this.initialize(...arguments);
	}
	Window_Extra_pic_video.prototype = Object.create(Window_Selectable.prototype);
	Window_Extra_pic_video.prototype.constructor = Window_Extra_pic_video;
	Window_Extra_pic_video.prototype.updatePadding = function () {
		this.padding = 0;
	};

	Window_Extra_pic_video.prototype.setItemWindow = function (itemWindow) {
		this._itemWindow = itemWindow;
	};
	Window_Extra_pic_video.prototype.setPic_info = function (pic_info) {
		this._pic_info = pic_info;
		this.refresh();
	};
	Window_Extra_pic_video.prototype.setVideo_info = function (video_info) {
		this._video_info = video_info;
		this.refresh();
	};

	const _Window_Extra_pic_video_update = Window_Extra_pic_video.prototype.update;
	Window_Extra_pic_video.prototype.update = function () {
		_Window_Extra_pic_video_update.call(this);

		if (this.active && TouchInput.isTriggered()) {
			this.processOk();
		}
	};

	Window_Extra_pic_video.prototype.processOk = function () {
		if (this.isCurrentItemEnabled() && this._pic_info) {
			this.playCursorSound();

			this.pic_page_index = (this.pic_page_index + 1) % this._pic_info.sabun_list.length;
			// this.updateInputData();
			// this.deactivate();
			// this.callOkHandler();
			this.refresh();
		} else if (this.isCurrentItemEnabled() && this._video_info) {
			this.playCursorSound();
			this.video_page_index = (this.video_page_index + 1) % this._video_info.sabun_list.length;
			this.refresh();
		} else {
			this.playBuzzerSound();
		}
	};

	Window_Extra_pic_video.prototype.show = function () {
		this.visible = true;
		this.pic_page_index = 0;
		this.video_page_index = 0;
		this.refresh();
	};

	Window_Extra_pic_video.prototype.hide = function () {
		this.paint();
		this.contents.clear();

		this.visible = false;
		this._pic_info = null;
		this._video_info = null;
	};

	Window_Extra_pic_video.prototype.refresh = function () {
		if (this._pic_info && this._pic_info.sabun_list[this.pic_page_index]) {
			const bitmap = ImageManager.loadPicture(this._pic_info.name + this._pic_info.sabun_list[this.pic_page_index]);

			if (!bitmap.isReady()) {
				bitmap.addLoadListener(() => {
					this.refresh(); // 画像がロードされたら再描画
				});
				return;
			} else {
				this.paint();
				this.contents.clear();
				this.contents.blt(bitmap, 0, 0, 1280, 720, 0, 0);
			}
		} else if (this._video_info && this._video_info.sabun_list[this.video_page_index]) {
			// $gameScreen.setVideoPicture({
			// 	fileName: this._video_info.name + this._video_info.sabun_list[this.video_page_index],
			// 	loop: "false",
			// 	smooth: "true",
			// 	volume: "100",
			// 	reload: "false",
			// 	autoplay: "true",
			// 	finishSwitch: "0"
			// });
			// $gameScreen.showPicture(11, "", 0, 0, 0, 100, 100, 255, 0);
		}
	};
})();
