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

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

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

	Scene_Menu.prototype.commandTravel = function () {
		SceneManager.push(Scene_Travel);
	};

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

	Scene_Travel.prototype.initialize = function () {
		SDN_SetFastTravel();

		Scene_MenuBase.prototype.initialize.call(this);
	};

	Scene_Travel.prototype.create = function () {
		Scene_MenuBase.prototype.create.call(this);
		DataManager.loadAllSavefileImages();
		this.createHelpWindow();
		this.createListWindow();
		this._helpWindow.setText("");
	};

	Scene_Travel.prototype.createBackground = function () {
		this._backgroundSprite = new Sprite();
		this._backgroundSprite.bitmap = ImageManager.loadSystem("menu_travel_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_Travel.prototype.createHelpWindow = function () {
		const rect = this.helpWindowRect();
		this._helpWindow = new Window_TravelHelp(rect);
		this.addWindow(this._helpWindow);
	};
	Scene_Travel.prototype.helpWindowRect = function () {
		const wx = 762;
		const wy = 80;
		const ww = Graphics.boxWidth - wx;
		const wh = 630;
		return new Rectangle(wx, wy, ww, wh);
	};

	Scene_Travel.prototype.createListWindow = function () {
		const rect = this.listWindowRect();
		this._listWindow = new Window_TravelList(rect);
		this._listWindow.setHandler("ok", this.onCategoryOk.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_Travel.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_Travel.prototype.helpWindowText = function () {
		return "";
	};

	Scene_Travel.prototype.helpWindowText = function () {
		return TextManager.saveMessage;
	};
	//コモンイベントを実行
	Scene_Travel.prototype.onCategoryOk = function () {
		this._listWindow.go_travel();
	};

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

	Window_TravelList.prototype.go_travel = function () {
		var data = this.CBR_travel_list[this.index()];

		$gameVariables.setValue(121, data.map_id);
		$gameVariables.setValue(122, data.x);
		$gameVariables.setValue(123, data.y);
		$gameVariables.setValue(130, data.d);

		$gameTemp.reserveCommonEvent(1104);

		SceneManager.goto(Scene_Map);
		/*
$gamePlayer.reserveTransfer(
97,
16,
8,
8,
0
);
		*/
	};

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

		this.CBR_travel_data = [
			{
				switch: 371,
				name: "アイリスアカデミー",
				file: "travel01.png",
				map_id: 10,
				x: 24,
				y: 41,
				d: 8
			},
			{
				switch: 372,
				name: "アイリスアカデミー",
				file: "travel02.png",
				map_id: 34,
				x: 24,
				y: 41,
				d: 8
			},
			{
				switch: 373,
				name: "奥多田山",
				file: "travel03.png",
				map_id: 64,
				x: 14,
				y: 13,
				d: 8
			},
			{
				switch: 374,
				name: "神宿",
				file: "travel04.png",
				map_id: 72,
				x: 34,
				y: 12,
				d: 2
			},
			{
				switch: 375,
				name: "神宿",
				file: "travel05.png",
				map_id: 76,
				x: 34,
				y: 12,
				d: 2
			},
			{
				switch: 376,
				name: "千々武洞窟",
				file: "travel06.png",
				map_id: 86,
				x: 16,
				y: 12,
				d: 8
			},
			{
				switch: 377,
				name: "尾鹿の森",
				file: "travel07.png",
				map_id: 92,
				x: 9,
				y: 46,
				d: 8
			},
			{
				switch: 378,
				name: "津久場岳",
				file: "travel08.png",
				map_id: 97,
				x: 22,
				y: 15,
				d: 8
			},
			{
				switch: 379,
				name: "アイリス本部",
				file: "travel09.png",
				map_id: 104,
				x: 19,
				y: 27,
				d: 8
			},
			{
				switch: 380,
				name: "アイリスダンジョン",
				file: "travel10.png",
				map_id: 115,
				x: 12,
				y: 4,
				d: 2
			},
			{
				switch: 381,
				name: "一ノ谷釣り公園",
				file: "travel11.png",
				map_id: 120,
				x: 17,
				y: 11,
				d: 2
			},
			{
				switch: 382,
				name: "赤城峠・登山口",
				file: "travel12.png",
				map_id: 134,
				x: 12,
				y: 11,
				d: 8
			},
			{
				switch: 383,
				name: "明郷埠頭",
				file: "travel13.png",
				map_id: 140,
				x: 49,
				y: 36,
				d: 8
			},
			{
				switch: 384,
				name: "渋野",
				file: "travel14.png",
				map_id: 143,
				x: 20,
				y: 33,
				d: 8
			},
			{
				switch: 385,
				name: "渋野",
				file: "travel15.png",
				map_id: 144,
				x: 20,
				y: 33,
				d: 8
			},
			{
				switch: 386,
				name: "青井山墓地",
				file: "travel16.png",
				map_id: 150,
				x: 14,
				y: 18,
				d: 8
			},
			{
				switch: 387,
				name: "白川研究所跡",
				file: "travel17.png",
				map_id: 154,
				x: 11,
				y: 12,
				d: 8
			},
			{
				switch: 388,
				name: "ネクサス・フューチャーのアジト",
				file: "travel18.png",
				map_id: 169,
				x: 32,
				y: 6,
				d: 2
			},
			{
				switch: 389,
				name: "エロドールダンジョン",
				file: "travel19.png",
				map_id: 173,
				x: 12,
				y: 13,
				d: 8
			},
			{
				switch: 390,
				name: "羽山高原",
				file: "travel20.png",
				map_id: 180,
				x: 21,
				y: 27,
				d: 8
			},
			{
				switch: 391,
				name: "専代ダンジョン",
				file: "travel21.png",
				map_id: 185,
				x: 12,
				y: 12,
				d: 8
			},
			{
				switch: 392,
				name: "魔法犯罪刑務所",
				file: "travel22.png",
				map_id: 190,
				x: 12,
				y: 14,
				d: 8
			},
			{
				switch: 393,
				name: "八場島",
				file: "travel23.png",
				map_id: 196,
				x: 19,
				y: 20,
				d: 8
			},
			{
				switch: 394,
				name: "都知木ダンジョン",
				file: "travel24.png",
				map_id: 204,
				x: 20,
				y: 26,
				d: 8
			},
			{
				switch: 395,
				name: "東都古代遺跡",
				file: "travel25.png",
				map_id: 212,
				x: 12,
				y: 12,
				d: 8
			},
			{
				switch: 396,
				name: "ダリアのアジト",
				file: "travel26.png",
				map_id: 225,
				x: 25,
				y: 25,
				d: 8
			},
			{
				switch: 397,
				name: "不二の樹海",
				file: "travel27.png",
				map_id: 232,
				x: 30,
				y: 46,
				d: 8
			},
			{
				switch: 398,
				name: "鳥影岳",
				file: "travel28.png",
				map_id: 242,
				x: 8,
				y: 26,
				d: 8
			},
			{
				switch: 399,
				name: "聖園教会への道",
				file: "travel29.png",
				map_id: 247,
				x: 6,
				y: 37,
				d: 8
			},
			{
				switch: 403,
				name: "地下格闘技場",
				file: "travel30.png",
				map_id: 253,
				x: 14,
				y: 16,
				d: 8
			},
			{
				switch: 404,
				name: "アイリス本部への道",
				file: "travel31.png",
				map_id: 255,
				x: 29,
				y: 39,
				d: 8
			}
		];

		this.CBR_travel_list = [];
		for (data of this.CBR_travel_data) {
			if ($gameSwitches.value(data.switch)) {
				ImageManager.loadSystem(data.file.slice(0, -4));
				this.CBR_travel_list.push(data);
			}
		}
	};

	Window_TravelList.prototype.maxItems = function () {
		return this.CBR_travel_list.length;
	};

	Window_TravelList.prototype.numVisibleRows = function () {
		return 15;
	};

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

	Window_TravelList.prototype.drawItem = function (index) {
		const info = this.CBR_travel_list[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_TravelList.prototype.drawContents = function (info, rect) {
		//		const bottom = rect.y + rect.height;
		this.contents.fontFace = "serif";
		//		this.contents.fontSize = 20;
		//b.textColor = "#000000";
		this.changeTextColor("#000000");
		this.drawTextM(info.name, 104, 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_TravelList.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_TravelList.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_TravelList.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_TravelList.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) {
		if (item) {
			//this.setText(item ? item.name : "");
			this._CBR_travel_data = item;
			this.refresh();
		}
	};

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

		var data = this._CBR_travel_data;
		if (data) {
			this.contents.fontFace = "serif";
			this.contents.fontSize = 22;

			this.changeTextColor("#000000");
			this.drawTextM("移動する", 8, 32, rect.width, "center");

			this.contents.fontSize = 28;
			this.changeTextColor("#000000");
			this.drawTextM(data.name, 8, 97, rect.width, "center");

			const bitmap = ImageManager.loadSystem(data.file.slice(0, -4));
			this.contents.blt(bitmap, 0, 0, 426, 240, 37, 151);

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

			this.contents.fontSize = 20;
			this.drawTextM("現在地", 46, 398, 100, "left");
			this.drawTextM("目的地", 46, 429, 100, "left");

			this.changeTextColor("#FFFFFF");
			this.drawTextS($dataMapInfos[$gameMap.mapId()].name, 148, 398, 200, "left");
			this.drawTextS(data.name, 148, 429, 200, "left");
		}
	};

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