//CHARGEMENT DE LA PAGE
function init_plugin_hackpoint(){
	switch($.urlParam('page')){
		default:
		break;
	}
	hackpoint_part_search();
	
	$('#parts').sortable_table({
		onSort : hackpoint_part_search
	});

	
	$('#sketchs').sortable_table({
		onSort : hackpoint_sketch_search
	});

	hackpoint_resource_search();

}


//Enregistrement des configurations
function hackpoint_setting_save(){
	$.action({ 
		action : 'hackpoint_setting_save', 
		fields :  $('#hackpoint-setting-form').toJson() 
	},function(){ $.message('info','Configuration enregistrée'); });
}


/** PART **/
	
//Récuperation d'une liste de part dans le tableau #parts
function hackpoint_part_search(callback){
	
		
	$('#parts').fill({
		action:'hackpoint_part_search',
		filters : $('#filters').filters(),
		sort : $('#parts').sortable_table('get')
	},function(){
		if(callback!=null) callback();
	});
}

//Ajout ou modification d'élément part
function hackpoint_part_save(){
	var data = $('#part-form').toJson();
	$.action(data,function(r){
		
		
		$.message('success','Enregistré');
	});
}


//Suppression d'élement part
function hackpoint_part_delete(element){
	if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
	var line = $(element).closest('tr');
	$.action({
		action : 'hackpoint_part_delete',
		id : line.attr('data-id')
	},function(r){
		line.remove();
		$.message('info','Élement supprimé');
	});
}



/** SKETCH **/
	
//Récuperation d'une liste de sketch dans le tableau #sketchs
function hackpoint_sketch_search(callback){
	
		
	$('#sketchs').fill({
		action:'hackpoint_sketch_search',
		filters : $('#filters').filters(),
		sort : $('#sketchs').sortable_table('get')
	},function(){
		if(callback!=null) callback();
	});
}

//Ajout ou modification d'élément sketch
function hackpoint_sketch_save(){
	var data = {
		action : 'hackpoint_sketch_save',
		id : $('#sketch-form').attr('data-id'),
		label : $('#label').val(),
		state : $('#state').prop('checked'),
		comment : $('#comment').val()
	}
	$.action(data);
}


//Suppression d'élement sketch
function hackpoint_sketch_delete(element){
	if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
	$.action({
		action : 'hackpoint_sketch_delete',
		id : $('#sketch-form').attr('data-id')
	},function(r){
		window.location = 'index.php?module=hackpoint&success=Sketch supprimé';
	});
}



/** RESOURCE **/
	
//Récuperation d'une liste de resource dans le tableau #resources
function hackpoint_resource_search(callback,triggered){
	$('#resources').fill({
		action:'hackpoint_resource_search',
		sketch : $('#sketch-form').attr('data-id'),
		showItems : false
	},function(){



		//rend les menu sortables
		$( "#resources" ).sortable({
			axis: "y",
			update: function( event, ui ){
				var sort = [];
				$( "#resources li:visible" ).each(function(i,li){
					li = $(li);
					sort.push(li.attr('data-id'));
				});
				$.action({
					action : 'hackpoint_resource_sort',
					sort : sort
				},function(r){
					
				});
			}
		});
    	$( "#resources" ).disableSelection();

    	//affiche les menu de façon progressive
    	$('#resources li:not(:eq(0))').removeClass('hidden')
    	.css('transform','translateX(-120px)');
    	$('#resources li:visible').each(function(i,element){
    		setTimeout(function(){
				$(element)
				.css('transform','translateX(0px)');
			},150*i);
    	});
    
    	var trigger =  '#resources li:eq(1)';
    	if(triggered){
    		trigger = triggered;
    	}else if($.urlParam('resource')){
    		trigger = '#resources li[data-id="'+$.urlParam('resource')+'"]';
    	}
		
		$(trigger).trigger('click');

		if(callback!=null) callback();
	});
}

function hackpoint_resource_edit(element){
	var line = $(element).closest('li');
	var id = line.attr('data-id');
	$('#resources li:visible').removeClass('active');
	line.addClass('active');
	$.action({
		action : 'hackpoint_resource_edit',
		id : id
	},function(r){
		$('#sketch-editor').html(r.html).attr('data-id',id);

		window.history.replaceState(null, null, "index.php?module=hackpoint&page=sheet.sketch&id="+$('#sketch-form').attr('data-id')+"&resource="+id);

		init_components('#sketch-editor');
		if(r.javascript){
			eval(r.javascript);
		}
	});
}

function resource_add_document(files){

	$.action({
		action : 'resource_add_document',
		id: $('#sketch-editor').attr('data-id'),
		files : files
	}, function(r){
		
		$.each(r.files, function(i, file){
			var line = $('#sketch-editor li[data-path="'+file.oldPath+'"]');
			line.attr('data-path', file.relative);
			line.find('a').attr('href', file.url);
			line.find('i.pointer').attr('onclick', 'resource_delete_document(this)');
			line.find('img').attr('src', file.url);

			$('#sketch-editor [data-type="dropzone"] input:not(:visible)').val('');
			
		});

	});
}

function resource_delete_document(element){
	if(!confirm("Êtes-vous sûr de vouloir supprimer ce fichier ?")) return;
	var line = $(element).closest('li');
	$.action({
		action : 'resource_delete_document',
		path : line.attr('data-path')
	},function(r){
		line.remove();
	});
}

function hackpoint_resource_title_edit(event,element){
	event.stopPropagation();
	event.preventDefault();
	var title = $(element);
	var span = $(element).find('span');
	var input = $(element).find('input');
	span.hide();
	input.show();
	input.focus();
	input.val(span.text());
	input.select();
	
	
	input.blur(function(){
		input.hide();
		span.text(input.val());
		span.show();
		var li = $(this).closest('li');
		$.action({
			action : 'hackpoint_resource_save',
			id : li.attr('data-id'),
			label : input.val()
		},function(r){
			
		});

	});
 	console.log(event,'hey!');
}

function hackpoint_resource_mirrorify(element,data){
	var editor = CodeMirror.fromTextArea($(element).get(0), data);
	editor.on("blur", function(cm,obj){
		var data = {};
		data.action = 'hackpoint_resource_save_content';
		data.id = $('#sketch-editor').attr('data-id');
		data.content = cm.getValue();
		$('.sketch-preloader').show();
		$.action(data,function(r){
			setTimeout(function(){
				$('.sketch-preloader').fadeOut(200);
			},300);
		});
	});
	editor.on("change", function() { 
		var data ={height:800};
		var wrap = editor.getWrapperElement();      
		var approp = editor.getScrollInfo().height > data.height ? data.height+"px" : "auto";
		if (wrap.style.height != approp) {
			wrap.style.height = approp;
			editor.refresh();
		}
	});
}

//Ajout ou modification d'élément resource
function hackpoint_resource_save(element){
	var data = {
		action:'hackpoint_resource_save',
		sketch:$('#sketch-form').attr('data-id'),
		type:$(element).attr('data-slug')
	}
	$.action(data,function(r){
		hackpoint_resource_search(null,'#resources li[data-id="'+r.id+'"]');
	});
}



//Suppression d'élement resource
function hackpoint_resource_delete(element,event){
	event.stopPropagation();
	if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
	var line = $(element).closest('li');
	$.action({
		action : 'hackpoint_resource_delete',
		id : line.attr('data-id')
	},function(r){
		
		$('#resources li:visible(:eq(0))').removeClass('active');
		$('#sketch-editor').html('');
		
		
		line.remove();
		
	});
}
