/* eslint-disable no-undef */
/*:ja
 * @plugindesc ファストトラベルプラグイン
 * @author COBRA
 *
 * @help
 * スイッチで指定したファストトラベルを解放
 */

CBR_sideEpisodes = null;

DataManager._databaseFiles.push({ name: "CBR_sideEpisodes", src: "CBR_sideEpisodeList.json" });

(function () {
	//メニューに追加

	Scene_Menu.prototype.commandSideEpisode = function () {
		SceneManager.push(Scene_SideEpisode);
	};

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

	Scene_SideEpisode.prototype = Object.create(Scene_MenuBase.prototype);
	Scene_SideEpisode.prototype.constructor = Scene_SideEpisode;

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

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

	Scene_SideEpisode.prototype.createBackground = function () {
		this._backgroundSprite = new Sprite();
		this._backgroundSprite.bitmap = ImageManager.loadSystem("side_episode_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_SideEpisode.prototype.createHelpWindow = function () {
		const rect = this.helpWindowRect();
		this._helpWindow = new Window_SideHelp(rect);
		this.addWindow(this._helpWindow);
	};
	Scene_SideEpisode.prototype.helpWindowRect = function () {
		const wx = 30;
		const wy = 48;
		const ww = Graphics.boxWidth - wx;
		const wh = Graphics.boxHeight - wy;
		return new Rectangle(wx, wy, ww, wh);
	};

	Scene_SideEpisode.prototype.createListWindow = function () {
		const rect = this.listWindowRect();
		this._listWindow = new Window_SideEpisodeList(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_SideEpisode.prototype.onItemOk = function () {
		this._listWindow.do_common();
	};
	Scene_SideEpisode.prototype.listWindowRect = function () {
		const wx = 120;
		const wy = 80;
		const ww = Graphics.boxWidth - wx - 503;
		const wh = 466;
		return new Rectangle(wx, wy, ww, wh);
	};

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

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

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

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

		// windowが重なっても透過させる
		this._isWindow = false;
	};

	//CBR_sideEpisodes
	Window_SideEpisodeList.prototype.makeList = function () {
		//未発生 = 1 / 未読 = 2 / 既読 = 3
		var data = $gameVariables.value(199) || [];
		this._disp_list = [];
		for (var i = 0; i < data.length; i++) {
			if (!data[i] || data[i] < 2) {
				continue;
			}
			this._disp_list.push({
				idx: i,
				type: data[i],
				dispIdx: CBR_sideEpisodes[i].dispIdx,
				title: CBR_sideEpisodes[i].title,
				text: CBR_sideEpisodes[i].text,
				img: CBR_sideEpisodes[i].img,
				commonId: CBR_sideEpisodes[i].commonId
			});
		}
		this._disp_list.sort(function (a, b) {
			return a.dispIdx - b.dispIdx;
		});
	};
	Window_SideEpisodeList.prototype.maxItems = function () {
		return this._disp_list.length || 0;
	};
	Window_SideEpisodeList.prototype.numVisibleRows = function () {
		return 10;
	};

	//右のウィンドウにデータを渡す
	Window_SideEpisodeList.prototype.callUpdateHelp = function () {
		if (this.active && this._helpWindow) {
			this.updateHelp();
		}
	};
	Window_SideEpisodeList.prototype.updateHelp = function () {
		var type = this._disp_list[this.index()];

		if (type !== 0) {
			this._helpWindow.setItem(this._disp_list[this.index()]);
		} else {
			//結果的にコレは不要になった、称号のみ
			this._helpWindow.setItem({
				title: "？？？",
				text: "？？？？？",
				img: null,
				commonId: null
			});
		}
	};
	Window_SideEpisodeList.prototype.select = function (index) {
		if (this._index != index && this._disp_list[this.index()]) {
			if (this._disp_list[this.index()].type == 2) {
				this._disp_list[this.index()].type = 3;

				var data_ary = $gameVariables.value(199) || [];
				data_ary[this._disp_list[this.index()].idx] = 3;
				$gameVariables.setValue(199, data_ary);

				this.refresh();
			}
		}
		this._index = index;
		this.refreshCursor();
		this.callUpdateHelp();
	};

	Window_SideEpisodeList.prototype.do_common = function () {
		var data = this._disp_list[this.index()];
		if (data && data.commonId) {
			$gameTemp.reserveCommonEvent(data.commonId);
			SceneManager.goto(Scene_Map);
		}
	};

	Window_SideEpisodeList.prototype.isCurrentItemEnabled = function () {
		return !!this._disp_list[this.index()];
	};

	Window_SideEpisodeList.prototype.drawItem = function (index) {
		const info = CBR_sideEpisodes[this._disp_list[index].idx];
		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, index);
		}
	};

	Window_SideEpisodeList.prototype.drawContents = function (info, rect, index) {
		const bottom = rect.y + rect.height;
		this.contents.fontFace = "serif";
		this.contents.fontSize = 22;
		this.changeTextColor("#000000");

		var type = this._disp_list[index].type;
		if (type) {
			this.drawTextM(info.title, 84, rect.y + 2, 400, "left");
			if (type == 2) {
				this.changeTextColor("#FF0000");
				this.drawTextM("NEW", 10, rect.y + 2, 200, "left");
			}
		} else {
			this.drawTextM("？？？", 84, rect.y + 2, 400, "left");
		}
	};

	// ################# カーソルの変更 ##################
	Window_SideEpisodeList.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_SideEpisodeList.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_SideEpisodeList.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_SideEpisodeList.prototype._refreshAllParts = function () {
		//this._refreshBack();
		//this._refreshFrame();
		this._refreshCursor();
		this._refreshArrows();
		this._refreshPauseSign();
	};

	// ###########################################################
	// ##################### 概要ウィンドウ #####################
	// ###########################################################
	function Window_SideHelp() {
		this.initialize(...arguments);
	}
	Window_SideHelp.prototype = Object.create(Window_Help.prototype);
	Window_SideHelp.prototype.constructor = Window_SideHelp;

	Window_SideHelp.prototype.initialize = function (rect) {
		Window_Base.prototype.initialize.call(this, rect);
		this._text = "";

		var sp = new Sprite();
		sp.x = 761 - this.x;
		sp.y = 66 - this.y;
		this._back_img = sp;
		this.addChild(this._back_img);
	};

	Window_SideHelp.prototype.setItem = function (item) {
		if (item) {
			if (!this._CBR_evrec_data || (this._CBR_evrec_data && this._CBR_evrec_data.idx != item.idx)) {
				this._CBR_evrec_data = item;
				this.refresh();
			}
		}
	};

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

	Window_SideHelp.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;
			this.changeTextColor("#000000");
			this.contents.fontSize = 20;
			this.drawTextM("概要", -4, 498, 100, "center");

			this.changeTextColor("#FFFFFF");
			this.drawTextExS(data.text, 102, 504, 600, "left");

			this._back_img.bitmap = ImageManager.loadSystem(data.img.slice(0, -4));
		}
	};
	//行間を詰める
	Window_SideHelp.prototype.processNewLineS = function (textState) {
		textState.x = textState.startX;
		textState.y += textState.height - 7;
		textState.height = this.calcTextHeight(textState);
	};

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

//[,,,2,1,3,3,3,3,,]みたいになってる
//未発生 = 1 / 未読 = 2 / 既読 = 3
var CBR_sideEpisode_add = function (id, val) {
	var data_ary = $gameVariables.value(199) || [];

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