CBR_evRecos = null;

DataManager._databaseFiles.push({ name: "CBR_evRecos", src: "CBR_eventList.json" });

(function () {
	Scene_Menu.prototype.commandEv_rec = function () {
		SceneManager.push(Scene_Ev_rec);
	};

	function Scene_Ev_rec() {
		this.initialize(...arguments);
	}
	Scene_Ev_rec.prototype = Object.create(Scene_MenuBase.prototype);
	Scene_Ev_rec.prototype.constructor = Scene_Ev_rec;

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

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

	Scene_Ev_rec.prototype.createBackground = function () {
		this._backgroundSprite = new Sprite();
		this._backgroundSprite.bitmap = ImageManager.loadSystem("menu_evrec_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);
	};

	Scene_Ev_rec.prototype.createHelpWindow = function () {
		const rect = this.helpWindowRect();
		this._helpWindow = new Window_TravelHelp(rect);
		this.addWindow(this._helpWindow);
	};
	Scene_Ev_rec.prototype.helpWindowRect = function () {
		const wx = 732;
		const wy = 80;
		const ww = Graphics.boxWidth - wx + 50;
		const wh = 630;
		return new Rectangle(wx, wy, ww, wh);
	};

	// もうヘルプウィンドウの更新は全部 EventListでやる、カテゴリはリストに渡すだけ
	Scene_Ev_rec.prototype.createListWindow = function () {
		const rect = this.listWindowRect();
		this._listWindow = new Window_EventList(rect);
		//this._listWindow.setHandler("ok", this.onItemOk.bind(this));
		this._listWindow.setHandler("cancel", this.popScene.bind(this));
		//this._listWindow.setMode(this.mode(), this.needsAutosave());
		this._listWindow.setHelpWindow(this._helpWindow);
		//this._listWindow.selectSavefile(this.firstSavefileId());
		this._listWindow.refresh();
		this.addWindow(this._listWindow);
	};

	Scene_Ev_rec.prototype.createCategoryWindow = function () {
		const rect = this.categoryWindowRect();
		this._categoryWindow = new Window_EvCategory(rect);
		//this._categoryWindow.setHelpWindow(this._helpWindow);
		this._categoryWindow.setItemWindow(this._listWindow);
		//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_Ev_rec.prototype.listWindowRect = function () {
		const wx = 120;
		const wy = 80;
		const ww = Graphics.boxWidth - this._helpWindow.width - wx;
		const wh = 596;
		return new Rectangle(wx, wy, ww, wh);
	};

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

	Scene_Ev_rec.prototype.helpWindowText = function () {
		return TextManager.saveMessage;
	};
	// //コモンイベントを実行
	// Scene_Ev_rec.prototype.onItemOk = function () {
	// 	$gameVariables.setValue(10, this._listWindow._list[this._listWindow.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_Ev_rec.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("発生中", "active");
		this.addCommand("進行中", "doing");
		this.addCommand("終了済み", "complete");
		// this.addCommand("未発生", "notyet");
	};
	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 () {
		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);
			}
		}
	};

	Window_EvCategory.prototype.playOkSound = function () {
		// SoundManager.playOk();
	};

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

	Window_EventList.prototype.initialize = function (rect) {
		Window_Selectable.prototype.initialize.call(this, rect);
		//最初に0番目って指定、commandではデフォでやってるけど

		// デフォではアクティブに
		this._category = "active";
		this.makeList();
		this.select(0);
		this.activate();
	};
	Window_EventList.prototype.makeList = function () {
		//1発生 , 2完了 , 3進行中 , null 0未発生

		var data_list = $gameVariables.value(114) || [];
		this._list = [];
		var flag = 0;

		if (this._category == "active") {
			//this._list = [1, 3, 5, 7, 8, 9, 10, 11, 12];
			flag = 1;
		} else if (this._category == "doing") {
			//this._list = [4, 13, 14, 15, 16, 17];
			flag = 3;
		} else if (this._category == "complete") {
			//this._list = [2, 3, 21, 27, 28, 29, 30, 31, 32, 42, 43, 51, 57, 58, 59, 70, 71, 72];
			flag = 2;
		} else if (this._category == "notyet") {
			//this._list = [2, 3, 21, 27, 28, 29, 30, 31, 32, 42, 43, 51, 57, 58, 59, 70, 71, 72];
			flag = 0;
		}

		data_list.forEach((e, i) => {
			if (e == flag) {
				this._list.push(i);
			}
		});
	};
	Window_EventList.prototype.maxItems = function () {
		return this._list.length || 0;
	};

	Window_EventList.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_EventList.prototype.numVisibleRows = function () {
		return 15;
	};

	//右のウィンドウにデータを渡す
	Window_EventList.prototype.callUpdateHelp = function () {
		if (this.active && this._helpWindow) {
			this.updateHelp();
		}
	};
	Window_EventList.prototype.updateHelp = function () {
		this._helpWindow.setItem(CBR_evRecos[this._list[this.index()]], this._category);
	};

	Window_EventList.prototype.drawItem = function (index) {
		var data_index = this._list[index];

		const info = CBR_evRecos[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_EventList.prototype.drawContents = function (info, rect) {
		const bottom = rect.y + rect.height;
		this.contents.fontFace = "serif";
		this.contents.fontSize = 22;
		this.changeTextColor("#000000");
		this.drawTextM(info.title, 84, rect.y + 2, 450, "left");

		// const lineHeight = this.lineHeight();
		// const y2 = bottom - lineHeight - 4;
		// if (y2 >= lineHeight) {
		// 	this.drawPlaytime(info, rect.x, y2, rect.width);
		// }
	};

	// ################# カーソルの変更 ##################
	Window_EventList.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");

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

	//56×200にする
	Window_EventList.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_EventList.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_EventList.prototype._refreshAllParts = function () {
		//this._refreshBack();
		//this._refreshFrame();
		this._refreshCursor();
		this._refreshArrows();
		this._refreshPauseSign();
	};

	// ###########################################################
	// ##################### 移動先ウィンドウ #####################
	// ###########################################################
	function Window_TravelHelp() {
		this.initialize(...arguments);
	}
	Window_TravelHelp.prototype = Object.create(Window_Help.prototype);
	Window_TravelHelp.prototype.constructor = Window_TravelHelp;

	Window_TravelHelp.prototype.setItem = function (item, category) {
		if (item) {
			//this.setText(item ? item.name : "");
			this._CBR_evrec_data = item;
			this._CBR_evrec_category = category;
			this.refresh();
		}
	};

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

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

		var data = this._CBR_evrec_data;
		if (data) {
			this.contents.fontFace = "serif";
			this.contents.fontSize = 20;

			this.contents.fontSize = 26;
			const text_w = this.contents.measureTextWidth(data.title);
			if (450 < text_w) {
				this.contents.fontSize = Math.floor((26 * 450) / text_w);
			}
			this.changeTextColor("#000000");
			this.drawTextM(data.title, -26, 30, rect.width, "center");

			this.contents.fontSize = 16;
			this.drawTextM("進行状況", -24, 78, 140, "center");
			this.drawTextM("イベント手順", -24, 110, 140, "center");
			this.drawTextM("概要", -24, 260, 140, "center");

			this.changeTextColor("#FFFFFF");
			var type_text = "発生中";
			if (this._CBR_evrec_category == "doing") {
				type_text = "進行中";
			} else if (this._CBR_evrec_category == "complete") {
				type_text = "完了";
			} else if (this._CBR_evrec_category == "notyet") {
				type_text = "未発生";
			}
			this.drawTextS(type_text, 120, 78, 100, "left");

			this.drawTextExS(data.process, 120, 118, rect.width, "left");
			this.drawTextExS(data.text, 120, 267, rect.width, "left");
		}
	};

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

//発生→1 完了→2 進行中→3 未発生→0
var CBR_evReco_add = function (id, val) {
	var data_ary = $gameVariables.value(114) || [];

	data_ary[id] = val;
	$gameVariables.setValue(114, data_ary);
};
