$( document ).ready(function() {
    console.log( "ready!" );
	$.ajax({
		  url: 'http://localhost:3001/require/all',
		  error: function() {
			console.log("Error");
		  },
		  dataType: 'text',
		  success: function(data) {
			console.log("success");
		  },
		  type: 'GET'
		});
});

$('#New').click(function () {
	console.log('New project');
	var ul = 'http://127.0.0.1:3001/';
	$.ajax({
			url: ul+'new_project',
			type: 'GET',  // http method
			success: function (resp, status, xhr) {
				console.log('Success');
			},
			error: function (errorMessage) {
					console.log('Error: ' + errorMessage);
				}
		});		
	});


$('#jstree_demo_div').on("open_node.jstree", function (e, data) {
	console.log(data.node);
	});



$('#jstree_demo_div').on("select_node.jstree", function (e, data) {
	console.log(data);
	var x = data.node.data.nsindex;
	var nid = data.node.id;
	var nc = data.node.data.nodeclass;
	$('#instancenodeid').val(data.node.id);
	$('#instancenodeidtype').val(data.node.data.nodeidtype);
	$('#instancename').val(data.node.text);
	$('#instancenamespace').val(data.node.data.nsindex +":"+data.node.data.nsurl );
	$('#instancedisplayname1').val("");
	$('#instancedisplayname2').val(data.node.data.displayname);
	$('#instancebrowsename1').text(x);
	$('#instancebrowsename2').val(":"+data.node.data.displayname);
	
	$('#instancenodeclass').val(nc);
	if(nc == "Object"){

		//$('#instancetypedefinition').val(data.node.data.eventnotifier);
		
		var plc = "";
		switch(data.node.data.eventnotifier){
			case 0: plc = "0 (None)"; break;
			case 1: plc = "1 (EventNotifierSubscribeToEvents)"; break;
			case 4: plc = "4 (EventNotifierHistoryRead)"; break;
			case 5: plc = "5 (EventNotifierSubscribeToEvents|EventNotifierHistoryRead)"; break;
			case 8: plc = "8 (EventNotifierHistoryWrite)"; break;
			case 9: plc = "9 (EventNotifierSubscribeToEvents|EventNotifierHistoryWrite)"; break;
			case 12: plc = "12 (EventNotifierHistoryRead|EventNotifierHistoryWrite)"; break;
			default: plc = "";
		}
		$('#instanceeventnotifier').attr('placeholder',plc);
				
		console.log("obj enabled");
		$('#objectattributesbutton').prop('disabled', false);
		$('#collapseTwo1').collapse('hide');
		$('#variableattributesbutton').prop('disabled',true);
		$('#collapseTwo2').collapse('hide');
		$('#methodattributesbutton').prop('disabled',true);
	}
	if(data.node.data.nodeclass == "Variable"){
		$('#datatype').val(data.node.data.datatype);
		$('#variablevalue').val(data.node.data.value);
		$('#variablevaluerank').val(data.node.data.valuerank);
		$('#variablearraydimensions').val(data.node.data.arraydimensions);
		var aclev = data.node.data.accesslevel;
		var plc = "";
				switch(aclev){
					case 0: plc = "0 (None)"; break;
					case 1: plc = "1 (EventNotifierSubscribeToEvents)"; break;
					case 4: plc = "4 (EventNotifierHistoryRead)"; break;
					case 5: plc = "5 (EventNotifierSubscribeToEvents|EventNotifierHistoryRead)"; break;
					case 8: plc = "8 (EventNotifierHistoryWrite)"; break;
					case 9: plc = "9 (EventNotifierSubscribeToEvents|EventNotifierHistoryWrite)"; break;
					case 12: plc = "12 (EventNotifierHistoryRead|EventNotifierHistoryWrite)"; break;
					default: plc = "";
				}
		$('#instanceaccesslevel').val(data.node.data.accesslevel);
		$('#accesslevelex').val(data.node.data.accesslevelex);
		$('#variablehistorizing').val(data.node.data.historizing);
		$('#minimumsamplinginterval').val(data.node.data.minsamplinginterval);
		


		$('#variableattributesbutton').prop('disabled', false);
		$('#collapseTwo').collapse('hide');
		$('#objectattributesbutton').prop('disabled',true);
		$('#collapseTwo2').collapse('hide');
		$('#methodattributesbutton').prop('disabled',true);
	}
	if(data.node.data.nodeclass == "Method"){
		//$('#writemask').val(data.node.data.accesslevelex);
		if(data.node.data.executable == 1){
		$('#executablemethodcheck').prop('checked', true);
		}else if(data.node.data.executable == 0){
			$('#executablemethodcheck').prop('checked', false);
		}

		$('#methodattributesbutton').prop('disabled',false);
		$('#collapseTwo').collapse('hide');
		$('#objectattributesbutton').prop('disabled',true);
		$('#collapseTwo1').collapse('hide');
		$('#variableattributesbutton').prop('disabled',true);
	}
	
});
	
$('#nodesetfile').on("open", function (e, data) {
	console.log('hello');
	});
	
$('#changeinstance').click(function () {
	console.log('hello');
	});
	
$('#savenodespecification').click(function () {
	console.log('save this');
	console.log($('#instancename').val());
	console.log($('#instancenamespace').val());
	console.log($('#instancenodeclass').val());
	console.log($('#instancetypedefinition').val());
	console.log($('#instancenodeidtype').val());
	console.log($('#instancenodeid').val());
	console.log($('#instanceeventnotifier').attr('placeholder'));
	console.log($('#instancedisplayname1').val());
	console.log($('#instancedisplayname2').val());
	console.log($('#instancebrowsename1').val());
	});
	
$('#numericnodeid').click(function () {
	console.log("It is number");
	$('#instancenodeidtype').text('Numeric')
});

$('#stringnodeid').click(function () {
	console.log("It is string");
	$('#instancenodeidtype').text('String')
});

$('#guidnodeid').click(function () {
	console.log("It is guid");
	$('#instancenodeidtype').text('Guid')
});

$('#opaquenodeid').click(function () {
	console.log("It is opaque");
	$('#instancenodeidtype').text('Opaque')
});

$('#instancebrowsename1').click(function () {
	console.log("Add the script");
	var sz = parseInt($('#namespaceidxbrowsename').attr("size")); 
	console.log(sz);
	var num_ns = "6";
	var nnum = parseInt(num_ns);
	if(sz < nnum){
		var lin = "<li><a class='dropdown-item' href='#'>"+num_ns+"</a></li>"
		$('#namespaceidxbrowsename').append(lin);
		$('#namespaceidxbrowsename').attr("size",num_ns);
	}
	
});



$('#subscribeevents').click(function () {
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '13 (EventNotifierSubscribeToEvents|EventNotifierHistoryRead|EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '1 (EventNotifierSubscribeToEvents)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '4 (EventNotifierHistoryRead)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '8 (EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '0 (None)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '5 (EventNotifierSubscribeToEvents|EventNotifierHistoryRead)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '12 (EventNotifierHistoryRead|EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '9 (EventNotifierSubscribeToEvents|EventNotifierHistoryWrite)');
	}
});

$('#historyread').click(function () {
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '13 (EventNotifierSubscribeToEvents|EventNotifierHistoryRead|EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '1 (EventNotifierSubscribeToEvents)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '4 (EventNotifierHistoryRead)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '8 (EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '0 (None)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '5 (EventNotifierSubscribeToEvents|EventNotifierHistoryRead)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '12 (EventNotifierHistoryRead|EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '9 (EventNotifierSubscribeToEvents|EventNotifierHistoryWrite)');
	}
});

$('#historywrite').click(function () {
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '13 (EventNotifierSubscribeToEvents|EventNotifierHistoryRead|EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '1 (EventNotifierSubscribeToEvents)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '4 (EventNotifierHistoryRead)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '8 (EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '0 (None)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==false ){
		$('#instanceeventnotifier').attr('placeholder', '5 (EventNotifierSubscribeToEvents|EventNotifierHistoryRead)');
	}
	
	if($('#subscribeevents').prop('checked')==false && $('#historyread').prop('checked')==true && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '12 (EventNotifierHistoryRead|EventNotifierHistoryWrite)');
	}
	
	if($('#subscribeevents').prop('checked')==true && $('#historyread').prop('checked')==false && $('#historywrite').prop('checked')==true ){
		$('#instanceeventnotifier').attr('placeholder', '9 (EventNotifierSubscribeToEvents|EventNotifierHistoryWrite)');
	}
});

