import { sidebarTemplate } from "./scripts-funcs.js";

function prepareData(data) {
	const preparedData = [];

	for(let script of data.scripts) {
		const btnSwitch = script.state == "disabled"
			? `<button 
					class="btn btn-success btn-small change-state-btn" 
					data-alias='${script.alias}'
					type="button"
					data-value="enable"
				>Enable</button>`
			: `<button 
					class="btn btn-warning btn-small change-state-btn" 
					data-alias='${script.alias}'
					type="button"
					data-value="disable"
				>Disable</button>`;

		preparedData.push({
			alias: `${script.alias}<br><small class="text-muted">by ${script.created_by}</small>`,
			name: `${script.name}<div class="mt-1"><small>${script.description}</small></div>`,
			filename: `${script.filename}<br><code><small>${script.path}</small></code>`,
			state: Helper.template.toogleStateBadge(script.state),
			actions: `
				${btnSwitch}
			`
		});
	}

	return preparedData;
}

function renderingMainTable(scr, data, total) {
	const provideSciptsData = {};
	for(let i in data) {
		provideSciptsData[data[i].alias] = data[i];
		data[i]["code"] = "";
	}
	scr.currentScreen.DOMObject.provideSciptsData = provideSciptsData;

	scr.currentScreen.DOMObject.querySelector(".main-container").innerHTML = Helper.template.table(
		"Regular scripts list", 
		{
			alias: "Alias", 
			name: "Script name",  
			filename: "Filename", 
			state: "State", 
			actions: "Actions"
		},
		data,
		`<span class="table-meta">Total: <span class="total">${total}</span> scripts</span>`
	);
}

function initMainTableInteractiveElements(scr, sh_api) {
	scr.currentScreen.DOMObject.querySelectorAll(".change-state-btn").forEach(btn => {
		btn.addEventListener("click", e => {
			const btn = e.currentTarget;
			if(btn.getAttribute("disabled")) {
				return ;
			}

			const methName = btn.dataset.value == "enable"
				? "regular_enable"
				: "regular_disable"

			const script = scr.currentScreen.DOMObject.provideSciptsData[btn.dataset.alias] ?? { alias: btn.dataset.alias };

			Helper.states.btnLoadingState(btn, true);
			sh_api.scripts[methName](script.alias, (err, data, meta) => {
				Helper.states.btnLoadingState(btn, false);

				if(data) {
					setTimeout(() => Screens.reinit(), 250);
					Toasts.createSuccess(
						`Request Success`,
						`State of <b>${script.alias}</b> was changed`
					).show();
				} else {
					Toasts.createError(
						"Request failed",
						`Regular script state not changed`
					).show();
				}

			});
		});
	});
}

function regular(sh_api) {
	return {
		title: "Scripts Regular",
		alias: "scripts-regular",
		renderer: () => {
			return Helper.template.mainTemplate(sidebarTemplate("regular"));
		},
		initer: scr => {
			sh_api.scripts.regular_list((err, resp, meta) => {
				console.log("sh_api.scripts.regular_list", err, resp, meta);

				if(meta.status_code != 200) {
					return scr.error("Server API ERROR", "");
				}

				renderingMainTable(scr, prepareData(resp.data), resp.data.total);
				initMainTableInteractiveElements(scr, sh_api);

				scr.ready();
			});			
		}
	}
}

export {
	regular
}