// Copyright (c) 2015, JINGROW and Contributors
// MIT License. See license.txt

jingrow.provide("jingrow.help");

jingrow.help.video_id = {};

jingrow.help.has_help = function (pagetype) {
	return jingrow.help.video_id[pagetype];
};

jingrow.help.show = function (pagetype) {
	if (jingrow.help.video_id[pagetype]) {
		jingrow.help.show_video(jingrow.help.video_id[pagetype]);
	}
};

jingrow.help.show_video = function (video_id, title) {
	// 处理Bilibili URL
	if (jingrow.utils.is_url(video_id)) {
		// Bilibili视频URL模式，支持多种格式：
		// https://www.bilibili.com/video/BV1xx411c7mD/
		// https://www.bilibili.com/video/av12345678/
		// https://player.bilibili.com/player.html?bvid=BV1xx411c7mD
		const bilibiliRegex = /bilibili\.com\/video\/(BV[a-zA-Z0-9]+|av\d+)|bvid=(BV[a-zA-Z0-9]+)/i;
		const match = video_id.match(bilibiliRegex);
		if (match) {
			video_id = match[1] || match[2];
		}
	}

	let dialog = new jingrow.ui.Dialog({
		title: title || __("帮助"),
		size: "large",
	});

	// 使用Bilibili嵌入播放器，采用响应式比例布局
	let video = $(`
		<div style="width:100%; position:relative; overflow:hidden; padding-bottom:56.25%;">
			<iframe src="//player.bilibili.com/player.html?bvid=${video_id}&page=1" 
			scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true" 
			style="position:absolute; top:0; left:0; width:100%; height:100%;"></iframe>
		</div>
	`);
	
	video.appendTo(dialog.body);

	dialog.show();
	dialog.$wrapper.addClass("video-modal");

	// 确保视频模态框有足够的宽度
	setTimeout(() => {
		dialog.$wrapper.find(".modal-dialog").css({
			"max-width": "900px",
			"width": "90%"
		});
		dialog.$wrapper.find(".modal-content").css({
			"height": "auto"
		});
		dialog.$wrapper.find(".modal-body").css({
			"padding": "15px"
		});
	}, 100);
};

$("body").on("click", "a.help-link", function () {
	var pagetype = $(this).attr("data-pagetype");
	pagetype && jingrow.help.show(pagetype);
});
