const POST = 'POST';

function toggle(selector){
	$(selector).toggle("slow");
}

function addDescriptionOption(text){
	if (text.trim() == '') return;
	var select = $('select[name=alt_comment]');
	if (select.length == 0){
		var area=$('textarea[name=comment]');
		var hint= area.attr('descr');
		$('<select/>',{name: 'alt_comment'})
			.append($('<option/>',{value: '',text: hint}))
			.insertBefore(area);		
		select = $('select[name=alt_comment]');
		select.on('change',function(){
			$('textarea[name=comment]').val(select.find('option:selected').text());
		});
	}
	select.append($('<option/>',{text: text}));
}

function getHeadings(elem){
	$('select[name=alt_comment]').remove();
	$('textarea[name=comment]').val('');
	var url=window.location.href.replace(/\/([^\/]*)$/,'/headings')+'?page='+encodeURIComponent(elem.value);
	$.ajax({
		url: url,
		dataType: "json",
		success: function(data){
			for (var index in data.headings) addDescriptionOption(data.headings[index]);
		}		
	});
}

var preview_timer = 0;

function keyEvent(e){
	if (e.ctrlKey){		
		if (e.key === 'f') { // display search form on Ctrl+F
			e.preventDefault();
			$('.search *').show();
			$('.search form>input').focus();
		} else
		if (e.key === 's') { // save current element on Ctrl+S
			var saveBtn = $('form[method=POST] button[type="submit"]')[0];
			if (saveBtn) {
				e.preventDefault();
				saveBtn.click();
			}			
		}
		return;
	}
	if (e.altKey){
		switch (e.keyCode){
			case 38:
				$('a.parent').each(function(){this.click()});
				return;
			case 39:
				$('a.next').each(function(){this.click()});
				return
			default:
				console.log(e.keyCode);
		}
	}
	switch (e.keyCode){
		case 27: // ESC
			if (document.location.href.includes("/add")) window.history.back();
			if (document.location.href.includes("/edit")) window.history.back();
			return;
		case 107: // +
			var active = document.activeElement.tagName;
			switch (active){
				case 'TEXTAREA':
					// do not activate add-link when in these fields
					return;
			}
			var addLink = $('a[href^="add"]')[0];
			if (addLink) addLink.click();
			return;
	}
	if (e.target.id == 'preview-source') {
		clearTimeout(preview_timer);
		preview_timer = setTimeout(preview,750,e.target);
	}
	
	console.log(e);
}

getHeadingsTimer = null;

function getHeadings_delayed(){
	if (getHeadingsTimer != null) clearTimeout(getHeadingsTimer);
	getHeadingsTimer = window.setTimeout(getHeadings,200,this);
}

async function handlePreview(resp,txt){
    const target = document.getElementById('preview');
    if (resp.ok){
        const content = await resp.text();
        $(target).removeClass('loading').html(content);
        setTimeout(() => {
            $(txt).css('height',Math.max(target.clientHeight,200));
        },200);
    } else {
        $(target).removeClass('loading').html('Preview failed…');
        console.log("preview request failed!");
    }
}

function preview(txt){
	let target = document.getElementById('preview');
	if (!target) return;
	$(target).addClass('loading');
	$(target).html('loading…');
	let url = '<? base ?>/api/preview';
	fetch(url,{
	    method: POST,
	    body: txt.value,
	    credentials: 'include'
	}).then(resp => handlePreview(resp,txt));
}
document.addEventListener('keydown',keyEvent);
