// Copyright (c) 2021, JINGROW
// For license information, please see license.txt

jingrow.ui.form.on("Package", {
	validate: function (frm) {
		if (!frm.pg.package_name) {
			frm.set_value("package_name", frm.pg.name.toLowerCase().replace(" ", "-"));
		}
	},

	license_type: function (frm) {
		jingrow
			.call("jingrow.core.pagetype.package.package.get_license_text", {
				license_type: frm.pg.license_type,
			})
			.then((r) => {
				frm.set_value("license", r.message);
			});
	},

	refresh: function (frm) {
		if (frm.pg.name && !frm.is_new()) {
			frm.add_custom_button(__("Uninstall Package"), function () {
				uninstall_package(frm);
			});
			
			frm.add_custom_button(__("Package Resources"), function () {
				show_package_dependencies(frm);
			});
		}
	},
});

function uninstall_package(frm) {
	const package_name = frm.pg.name;
	
	jingrow.confirm(
		__("Are you sure you want to uninstall this package? This will remove all associated modules and pagetypes."),
		function() {
			jingrow.call("jingrow.core.pagetype.package.package.uninstall_package", {
				package_name: package_name
			}).then((r) => {
				if (r.message.status === 'success') {
					jingrow.msgprint(__("Package '{0}' uninstalled successfully", [package_name]));
					setTimeout(() => {
						window.location.href = "/app/package";
					}, 3000);
				} else {
					jingrow.throw(__("Failed to uninstall package"));
				}
			}).catch(() => {
				jingrow.throw(__("Failed to uninstall package"));
			});
		}
	);
}

function show_package_dependencies(frm) {
	const package_name = frm.pg.name;
	
	jingrow.call("jingrow.core.pagetype.package.package.get_package_dependencies", {
		package_name: package_name
	}).then((r) => {
		if (r.message.status === 'success') {
			const dependencies = r.message;
			let content = `<div style="max-height: 400px; overflow-y: auto; padding: 16px;">`;
			
			// 显示包名
			content += `<h4 style="margin: 0 0 20px 0; color: #1f2937; font-size: 18px; font-weight: 600; border-bottom: 2px solid #e5e7eb; padding-bottom: 8px;">${__("Package")}: ${dependencies.package_name}</h4>`;
			
			// 显示统计信息
			if (dependencies.summary) {
				content += `<div style="background: #f9fafb; padding: 16px; border-radius: 8px; margin-bottom: 20px;">`;
				content += `<p style="margin: 8px 0; color: #374151; font-size: 14px;"><strong style="color: #1f2937; font-weight: 600;">${__("Modules")}:</strong> ${dependencies.summary.modules_count || 0}</p>`;
				content += `<p style="margin: 8px 0; color: #374151; font-size: 14px;"><strong style="color: #1f2937; font-weight: 600;">${__("PageTypes")}:</strong> ${dependencies.summary.pagetypes_count || 0}</p>`;
				content += `<p style="margin: 8px 0; color: #374151; font-size: 14px;"><strong style="color: #1f2937; font-weight: 600;">${__("Releases")}:</strong> ${dependencies.summary.releases_count || 0}</p>`;
				content += `</div>`;
			}
			
			// 显示模块列表
			if (dependencies.modules && dependencies.modules.length > 0) {
				content += `<div style="margin-bottom: 20px;">`;
				content += `<h5 style="margin: 0 0 12px 0; color: #374151; font-size: 16px; font-weight: 600; display: flex; align-items: center;">`;
				content += `<span style="width: 4px; height: 16px; background: #3b82f6; margin-right: 8px; border-radius: 2px;"></span>${__("Modules")}:</h5>`;
				content += `<ul style="margin: 0; padding-left: 20px; list-style-type: disc;">`;
				dependencies.modules.forEach(module => {
					content += `<li style="margin: 6px 0; color: #6b7280; font-size: 14px; line-height: 1.5;">${module}</li>`;
				});
				content += `</ul></div>`;
			}
			
			// 显示页面类型列表
			if (dependencies.pagetypes && dependencies.pagetypes.length > 0) {
				content += `<div style="margin-bottom: 20px;">`;
				content += `<h5 style="margin: 0 0 12px 0; color: #374151; font-size: 16px; font-weight: 600; display: flex; align-items: center;">`;
				content += `<span style="width: 4px; height: 16px; background: #3b82f6; margin-right: 8px; border-radius: 2px;"></span>${__("PageTypes")}:</h5>`;
				content += `<ul style="margin: 0; padding-left: 20px; list-style-type: disc;">`;
				dependencies.pagetypes.forEach(pagetype => {
					content += `<li style="margin: 6px 0; color: #6b7280; font-size: 14px; line-height: 1.5;">${pagetype}</li>`;
				});
				content += `</ul></div>`;
			}
			
			// 显示发布版本列表
			if (dependencies.releases && dependencies.releases.length > 0) {
				content += `<div style="margin-bottom: 20px;">`;
				content += `<h5 style="margin: 0 0 12px 0; color: #374151; font-size: 16px; font-weight: 600; display: flex; align-items: center;">`;
				content += `<span style="width: 4px; height: 16px; background: #3b82f6; margin-right: 8px; border-radius: 2px;"></span>${__("Releases")}:</h5>`;
				content += `<ul style="margin: 0; padding-left: 20px; list-style-type: disc;">`;
				dependencies.releases.forEach(release => {
					content += `<li style="margin: 6px 0; color: #6b7280; font-size: 14px; line-height: 1.5;">${release}</li>`;
				});
				content += `</ul></div>`;
			}
			
			content += `</div>`;
			
			jingrow.msgprint(content, __("Package Resources"));
		} else {
			jingrow.throw(__("Failed to get package resources"));
		}
	}).catch(() => {
		jingrow.throw(__("Failed to get package resources"));
	});
}
