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

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

CBR_eroRireki = null;

DataManager._databaseFiles.push({ name: "CBR_eroRireki", src: "CBR_h_event_data.json" });

(function () {
	// ########################################################
	// ################### 履歴セレクト ######################
	// ########################################################

	Window_EroStatus_rireki_syu_select.prototype = Object.create(Window_Selectable.prototype);
	Window_EroStatus_rireki_syu_select.prototype.constructor = Window_EroStatus_rireki_syu_select;

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

		// デフォではアクティブに
		this.makeList();
		this.select(0);
		this.activate();
	};

	Window_EroStatus_rireki_syu_select.prototype.numVisibleRows = function () {
		return 5;
	};

	Window_EroStatus_rireki_syu_select.prototype.makeList = function () {
		var name = $gameParty.menuActor().name();
		if (name == "Shirayuki") {
			var idx_rec = 144;
		} else if (name == "Yotsuru") {
			var idx_rec = 145;
		}
		var ary = $gameVariables.value(idx_rec) || [];
		this._list = ary;
		this.refresh();
	};

	Window_EroStatus_rireki_syu_select.prototype.maxItems = function () {
		return this._list.length || 0;
	};

	Window_EroStatus_rireki_syu_select.prototype.setViewWindow = function (viewWindow) {
		this._viewWindow = viewWindow;
	};

	//右のウィンドウにデータを渡す
	Window_EroStatus_rireki_syu_select.prototype.callUpdateHelp = function () {
		if (this.active && this._viewWindow) {
			this.updateHelp();
		}
	};

	Window_EroStatus_rireki_syu_select.prototype.updateHelp = function () {
		if (this._list[this.index()]) {
			this._viewWindow.setItem(CBR_eroRireki[this._list[this.index()].id]);
		}
	};

	Window_EroStatus_rireki_syu_select.prototype.drawItem = function (index) {
		const info = CBR_eroRireki[this._list[index].id];
		const rect = this.itemRectWithPadding(index);
		this.resetTextColor();
		//this.changePaintOpacity(this.isEnabled(savefileId));
		//this.drawTitle(savefileId, rect.x, rect.y + 4);
		//数値だったら
		//if (Number.isFinite(info)) {
		this.drawContents(info, rect, index);
		//}
	};

	Window_EroStatus_rireki_syu_select.prototype.drawContents = function (info, rect, index) {
		const bottom = rect.y + rect.height;
		//var newFlag = $gameVariables.value(144);
		this.contents.fontFace = "serif";
		this.contents.fontSize = 22;

		var type = this._list[index].type;
		if (type === 0) {
			this.changeTextColor("#FF0000");
			this.drawTextM("NEW", 10, rect.y + 2, 200, "left");
		}
		this.changeTextColor("#000000");
		this.drawTextM(info.title, 112, rect.y + 2, 400, "left");

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

	Window_EroStatus_rireki_syu_select.prototype.select = function (index) {
		if (this._index != index && this._list[this.index()]) {
			if (this._list[this.index()].type == 0) {
				var name = $gameParty.menuActor().name();
				if (name == "Shirayuki") {
					var idx_rec = 144;
				} else if (name == "Yotsuru") {
					var idx_rec = 145;
				}
				var data_ary = $gameVariables.value(idx_rec) || [];
				var idx = data_ary.findIndex((e) => e.id === this._list[this.index()].id);
				if (idx !== -1) {
					data_ary[idx].type = 1;
					this._list[this.index()].type = 1;
					$gameVariables.setValue(idx_rec, data_ary);
					this.refresh();
				}
			}
		}
		this._index = index;
		this.refreshCursor();
		this.callUpdateHelp();
	};

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

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

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

	// ########################################################
	// ################### 履歴のビュー ######################
	// ########################################################
	Window_EroStatus_rireki_syu_view.prototype = Object.create(Window_Base.prototype);
	Window_EroStatus_rireki_syu_view.prototype.constructor = Window_EroStatus_rireki_syu_view;

	Window_EroStatus_rireki_syu_view.prototype.initialize = function (rect) {
		Window_Base.prototype.initialize.call(this, rect);
		this.refresh();
	};
	// padding0を適用させる
	Object.defineProperty(Window_EroStatus_rireki_syu_view.prototype, "padding", {
		get: function () {
			return 0;
		},
		set: function (value) {
			this._padding = 0;
			this._refreshAllParts();
		},
		configurable: true
	});

	Window_EroStatus_rireki_syu_view.prototype.setItem = function (data) {
		this._data = data;
		this.refresh();
	};

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

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

		var bitmap = ImageManager.loadSystem("erost_rireki_back");
		this.contents.blt(bitmap, 0, 0, 1280, 720, 0, 0);
		if (!bitmap.isReady()) {
			bitmap.addLoadListener(() => {
				this.refresh(); // 画像がロードされたら再描画
			});
			return;
		}

		if (this._data) {
			this.contents.fontSize = 17;
			var sY = 38;
			var lineY = 36;
			var sX = 61;
			var lineX = 363;
			var lineX2 = 62;
			this.changeTextColor("#000000");
			this.drawTextS("相手", sX, sY, 200);
			this.drawTextS("種族", sX + lineX, sY, 200);
			this.drawTextS("場所", sX + lineX * 2, sY, 200);
			this.drawTextS("詳細", sX, sY + lineY - 4, 200);

			this.resetTextColor();
			this.contents.fontFace = "serif";
			this.contents.fontSize = 20;
			this.drawTextS(this._data.target, sX + lineX2, sY, 800);
			this.drawTextS(this._data.job, sX + lineX2 + lineX, sY, 800);
			this.drawTextS(this._data.place, sX + lineX2 + lineX + lineX, sY, 800);
			this.drawTextExS(this._data.text, sX + lineX2, sY + lineY, 800);
		}
	};
})();

var CBR_add_syussan = function (name) {
	if (name == "Shirayuki") {
		var idx = 189;
		var idx_rec = 144;
	} else if (name == "Yotsuru") {
		var idx = 190;
		var idx_rec = 145;
	}
	var ary = $gameVariables.value(idx_rec) || [];

	ary.unshift({ id: $gameVariables.value(idx), type: 0 });
	if (100 < ary.length) {
		ary.length = 100;
	}
	$gameVariables.setValue(idx_rec, ary);
	$gameVariables.setValue(idx, 0);
};
