$(document).ready(function () {
	init_datatable();

	$(document).on('click', '.editCron', async function (e) {	// Dynamic binding, since element doesn't exists when loading this JS
		editCronDialog(e);
	});

	$(document).on('click', '.runCron', async function (e) {	// Dynamic binding, since element doesn't exists when loading this JS
		runCron(e.currentTarget);
	});

	$(document).on('click', '.enableCronSwitch', async function (e) {	// Dynamic binding, since element doesn't exists when loading this JS
		toggleEnableCronSwitch(e.currentTarget.id, this);
	});
});

function copyCron(id) {
	var content = $('#' + id).text();

	navigator.clipboard.writeText(content).then(function () { });

	$('#' + id).addClass('flash-copy').delay('1000').queue(function () {
		$('#' + id).removeClass('flash-copy').dequeue();
	});
}

function init_expression_tooltips() {
	$('.crontable tbody tr').each(function () {
		var expression = $(this).find('td:eq(3)').text().trim();

		var humanReadable = cronstrue.toString(expression);

		$(this).find('#humanreadable_tooltip').attr('data-bs-original-title', humanReadable).tooltip();
	});
}

function init_datatable() {
	var disableRunNow = $('.crontable').data('disable-run-now') == 1;

	$('.crontable').DataTable({
		"pageLength": 25,
		responsive: true,
		ordering: true,
		"scrollCollapse": true,
		"paging": false,
		"scrollX": true,
		"autoWidth": false,
		columnDefs: disableRunNow ? [{ targets: 7, visible: false }] : [],
		"language": {
			url: getDataTablesLanguageUrl(),
		},
		dom: 'Bfrtip',
		buttons: [
			{
				text: lang_general_refresh_list,
				action: function (e, dt, node, config) {
					reloadCrons();
				}
			}
		]
	});
	init_expression_tooltips();
}

function modalEventListener() {
	$('#edit_cron_expression_custom').on('input change', function (e) {
		humanReadableInEditDialog()
	});

	$('#edit_cron_expression_dropdown').change(function () {
		humanReadableInEditDialog()
	});
}

function editCronDialog(e) {
	$('#editCronModal').remove();

	$.ajax({
		url: base_url + 'index.php/cron/editDialog',
		type: 'post',
		data: {
			id: e.currentTarget.id,
		},
		success: function (data) {
			$('body').append(data);

			var editCronModal = new bootstrap.Modal(document.getElementById('editCronModal'));
			editCronModal.show();
			modalEventListener();
			$('[data-bs-toggle="tooltip"]').tooltip();
		},
		error: function (data) {

		},
	});
	return false;
}

function editCron() {
	var $cron_id = $('#edit_cron_id').val();
	var $cron_description = $('#edit_cron_description').val();
	var $cron_expression = $('#edit_cron_expression_custom').val();
	var $cron_enabled = $('#edit_' + $cron_id).is(':checked') ? 'true' : 'false';

	$.ajax({
		url: base_url + 'index.php/cron/edit',
		method: 'POST',
		data: {
			cron_id: $cron_id,
			cron_description: $cron_description,
			cron_expression: $cron_expression,
			cron_enabled: $cron_enabled
		},
		success: function (response) {
			if (response.success) {
				reloadCrons();
				showToast(lang_general_word_success, response.message, 'bg-success text-white', 5000);
			} else {
				showToast(lang_general_word_error, response.message, 'bg-danger text-white', 5000);
			}
		},
		error: function (response) {
			showToast(lang_general_word_error, lang_general_word_query_failed_unkown, 'bg-danger text-white', 5000);
		}
	});

}

function runCron(button) {
	var $button = $(button);
	$button.prop('disabled', true).html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>');

	$.ajax({
		url: base_url + 'index.php/cron/runNow',
		type: 'post',
		dataType: 'json',
		data: {
			id: button.id
		},
		success: function (response) {
			if (response.success) {
				showToast(lang_general_word_success, response.message, 'bg-success text-white', 5000);
			} else if (response.messagecategory == 'error') {
				showToast(lang_general_word_error, response.message, 'bg-danger text-white', 5000);
			} else {
				showToast(lang_general_word_warning, response.message, 'bg-warning text-white', 5000);
			}
		},
		error: function (response) {
			var message = response.responseJSON && response.responseJSON.message ? response.responseJSON.message : lang_general_word_query_failed_unkown;
			showToast(lang_general_word_error, message, 'bg-danger text-white', 5000);
		},
		complete: function () {
			reloadCrons();
		}
	});
}

function humanReadableInEditDialog() {
	var exp_inputID = $('#edit_cron_expression_custom');
	var exp_dropdownID = $('#edit_cron_expression_dropdown');
	var exp_humanreadableID = $('#exp_humanreadable');
	var humanReadable = '';

	exp_inputID.on('input', function () {
		exp_dropdownID.val('');
	});

	if (exp_dropdownID.val() == '') {
		exp_humanreadableID.show();

		try {
			humanReadable = cronstrue.toString(exp_inputID.val());
		} catch (error) {
			humanReadable = 'waiting for complete expression...';
		}

		exp_humanreadableID.text(humanReadable);
	} else {
		exp_humanreadableID.hide();

		exp_inputID.val(exp_dropdownID.val());
	}
}


function toggleEnableCronSwitch(id, thisvar) {
	$.ajax({
		url: base_url + 'index.php/cron/toogleEnableCronSwitch',
		type: 'post',
		data: {
			id: id,
			checked: $(thisvar).is(':checked')
		},
		success: function (data) {
			reloadCrons();
		},
		error: function (data) {
		},
	});
	return false;
}

function reloadCrons() {
	$.ajax({
		url: base_url + 'index.php/cron/fetchCrons',
		type: 'post',
		dataType: 'json',
		success: function (data) {
			loadCronTable(data);
		},
		error: function (data) {
			BootstrapDialog.alert({
				title: 'ERROR',
				message: 'An error occurred while making the request',
				type: BootstrapDialog.TYPE_DANGER,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
		},
	});
	return false;
}

function loadCronTable(rows) {
	var uninitialized = $('.crontable').filter(function () {
		return !$.fn.DataTable.isDataTable(this);
	});

	uninitialized.each(function () {
		init_datatable();
	});

	var table = $('.crontable').DataTable();

	table.clear();

	for (i = 0; i < rows.length; i++) {
		let cron = rows[i];

		var data = [];
		data.push(cron.cron_id);
		data.push(cron.cron_description);
		data.push(cron.cron_status);
		data.push(cron.cron_expression);
		data.push(cron.cron_last_run);
		data.push(cron.cron_next_run);
		data.push(cron.cron_edit);
		data.push(cron.cron_run);
		data.push(cron.cron_enabled);

		let createdRow = table.row.add(data).index();
	}
	table.draw();
	init_expression_tooltips();
}
