/*******************************************************************************
 * Copyright 2016 Centre for Development of Advanced Computing(C-DAC), Pune
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
var semantictagParam;

var enumSearchSERVICES = {
	SEARCH : {
		type : "search",		
		search_url : "http://localhost:8080/csnoserv/api/search/search",
		hostname : "http://localhost:8080/", //change host name here
	},
};

var enumSearchACCEPTABILITY = {
	ALL : "all",
	PREFERRED : "preferred",
	PREFERRED_EXCLUDING_FSN : "preferredexcludingfsn",
	SYNONYMS : "synonyms",
	ACCEPTABLE : "acceptable"
};
var enumSearchSTATE = {
	BOTH : "both",
	ACTIVE : "active",
	INACTIVE : "inactive"
};

var enumSearchSEMANTICTAG = {

	ALL : "all",
	PROCEDURE : "procedure",
	DISORDER : "disorder",
	FINDING : "finding",
	OBSERVABLE_ENTITY : "observable entity",
	BODY_STRUCTURE : "body structure",
	ORGANISM : "organism",
	SUBSTANCE : "substance",
	SPECIMEN : "specimen",
	SPECIAL_CONCEPT : "special concept",
	LINKAGE_CONCEPT : "linkage concept",
	PHYSICAL_FORCE : "physical force",
	EVENT : "event",
	ENVIRONMENT : "environment",
	GEOGRAPHIC_LOCATION : "geographic location",
	SOCIAL_CONCEPT : "social concept",
	SITUATION_WITH_EXPLICIT_CONTEXT : "situation",
	STAGING_SCALE : "staging scale",
	PHYSICAL_OBJECT : "physical object",
	QUALIFIER_VALUE : "qualifier value",
	RECORD_ARTIFACT : "record artifact",
	PERSON : "person",
	LINK_ASSERTION : "link assertion",
	NAMESPACE_CONCEPT : "namespace concept",
	ATTRIBUTE : "attribute",
	ASSESSMENT_SCALE : "assessment scale",
	RACIAL_GROUP : "racial group",
	TUMOR_STAGING : "tumor staging",
	OCCUPATION : "occupation",
	MORPHOLOGIC_ABNORMALITY : "morphologic abnormality",
	CELL : "cell",
	CELL_STRUCTURE : "cell structure",
	ETHNIC_GROUP : "ethnic group",
	PRODUCT : "product",
	INACTIVE_CONCEPT : "inactive concept",
	NAVIGATIONAL_CONCEPT : "navigational concept",
	LIFE_STYLE : "life style",
	REGIME_THERAPY : "regime/therapy",
	RELIGION_PHILOSOPHY : "religion/philosophy",
	DISPOSITION : "disposition",
	MEDICINAL_PRODUCT : "medicinal product",
	MEDICINAL_PRODUCT_FORM : "medicinal product form",
	CLINICAL_DRUG : "clinical drug",
	UNIT_OF_PRESENTATION : "unit of presentation",
	NUMBER : "number",
	ADMINISTRATION_METHOD : "administration method",
	BASIC_DOSE_FORM : "basic dose form",
	DOSE_FORM : "dose form",
	INTENDED_SITE : "intended site",
	RELEASE_CHARACTERISTIC : "release characteristic",
	STATE_OF_MATTER : "state of matter",
	TRANSFORMATION : "transformation",
	BRAND_PRODUCT : "brand product",
	ROLE : "role",
	PRODUCT_NAME : "product name",
	PACKAGED_CLINICAL_DRUG : "packaged clinical drug",
	REAL_CLINICAL_DRUG : "real clinical drug",
	REAL_MEDICINAL_PRODUCT : "real medicinal product",
	REAL_PACKAGED_CLINICAL_DRUG : "real packaged clinical drug",
	SUPPLIER : "supplier",
	GEOGRAPHIC_LOCATION :"geographic location"
};


/*
*on document loading
*/
$(document).ready(function(){
	loadSemanticTagsAutoSearch();
});


/*
*Ajax call used for load semantic tags from CSNOServ
*/
function loadSemanticTagsAutoSearch(){
	var semanticURL=enumSERVICES.SEARCH.hostname+"csnoserv/api/search/semantic";
	$.ajax({url: semanticURL,
				method: 'GET',
				headers: {
						'Access-Control-Allow-Origin': '*'
					},
				success: function(result){			
			loadSemanticTagListAutoSearch(result);
		 },error:function(result){	
			console.log("no response"+result);	
		}});
	
	}
	
	/*
	*loading semantic tags from result of Ajax call loadSemanticTags
	*/
	function loadSemanticTagListAutoSearch(semanticTagList){	
		var dataStr='';
		enumSEMANTICTAG = {};
		semanticTagList.forEach(element => {
		dataStr=element;
		if(dataStr!="undefined"){
			dataStr=dataStr.toUpperCase().split(' ').join('_');
			enumSEMANTICTAG[dataStr]=element;	
		}	
	});
	enumSEMANTICTAG.ALL='all';	
}







function autoselectSNOMEDCT(FIELD_ID,state_IN, semantictag_IN, acceptability_IN, returnlimit_IN,
		callback) {		
	if (returnlimit_IN <= -1 || returnlimit_IN == '' || returnlimit_IN == undefined
			|| returnlimit_IN == null) {
		returnlimit_IN =10;

	}	

	if (state_IN == null || state_IN == ''
			|| state_IN == undefined) {
		state_IN = enumSearchSTATE.ACTIVE;
	}else
		state_IN = enumSearchSTATE[state_IN];

	if (semantictag_IN == null || semantictag_IN == undefined
			|| semantictag_IN == '') {
		semantictag_IN = enumSearchSEMANTICTAG.ALL;
	}
	//**************** for single semantic tag searching
//	else
//		semantictag_IN = enumSearchSEMANTICTAG[semantictag_IN];
	
	//************************** for multiple semantic tag searching
	 else
		{
			var semanticTagList="";	
			for(var i=0;i<semantictag_IN.length;i++)
			{			
			 semanticTagList=semanticTagList+(enumSEMANTICTAG[semantictag_IN[i]])+"++";
			}
			semantictag_IN=semanticTagList.slice(0,semanticTagList.length-2);	
		}
	semantictagParam = semantictag_IN;

	if (acceptability_IN == null
			|| acceptability_IN == undefined || acceptability_IN == '') {
		acceptability_IN = enumSearchACCEPTABILITY.SYNONYMS;
	} else
		acceptability_IN = enumSearchACCEPTABILITY[acceptability_IN];

	var xhrRequest = null;							
				$('#'+FIELD_ID).on('keypress, keydown', function(event) {	
					var isFirefox = typeof InstallTrigger !== 'undefined';
					if (event.which==186 ||( isFirefox && event.which==59)) {          			
            				event.preventDefault();
         				}
					if($('#'+FIELD_ID).val().trim()!="")
					{
						if(this.selectionStart<$('#'+FIELD_ID).val().length||event.which==37 || event.which==39)
						{
							return false;
						}
					}				
					if (event.which == 8||event.which == 46){
        			if($('#'+FIELD_ID).val().slice(-1)==';')
        			{
        				return false;
        			}
        			}
        			if (event.ctrlKey==true) {          			
            				event.preventDefault();
         				}
				}); 


	$("#"+FIELD_ID)
			.autocomplete(
					{
						max : 20,
						minLength : 3,
						cacheLength : 1,
						scroll : false,
						width : 200,
						highlight : false,
						autoFocus : false,
						source : function(request, response) {
							var dataValue = document.getElementById(FIELD_ID).value;									
//*******************   // 	dataValue=dataValue.substring(dataValue.lastIndexOf(';')+1,dataValue.length);//searching for multiple concept term in same textbox using ;
							var servURL = "";
							if (dataValue == '') {
								return;
							}
							servURL = enumSearchSERVICES.SEARCH.search_url;							
							servURL += "?term="
									+ dataValue
									+ "&state=" + state_IN + "&semantictag="
									+ semantictagParam + "&acceptability="
									+ acceptability_IN + "&returnlimit="+returnlimit_IN;
							console.log(servURL);

							if (xhrRequest && xhrRequest.readystate != 4)
								xhrRequest.abort();
							xhrRequest = $
									.ajax({
										type : "GET",
										url : servURL,
										//dataType : "jsonp",
										crossDomain : true,
										success : function(data, textStatus,
												jqXHR) {
											console.log(data);
											var items=data;											
											if(items.length<=0)
											{
												//$('#nosuggestion').show();
											}
											response($.map(items, function (item) {
												var val=document.getElementById(FIELD_ID).value.replace(dataValue,"");
                   								// replace textbox concept term for single search
												return { value: val+item.term+";", concept: item };		
												
													/* code for
													// replace text box concept term for multiple searching seperated by ; 
													response($.map(items, function (item) {
															var val=document.getElementById(FIELD_ID).value.replace(dataValue,"");
			                       								return { value: val+item.term+";", concept: item };
													*/
													
                       							}));
										},
										error : function(jqXHR, textStatus,
												errorThrown) {
											console.log(textStatus);
										}
									});						

						},						
						select : function(event, ui) {
						if (typeof callback === "function")
						callback(ui.item.concept,FIELD_ID);							
							$("#"+FIELD_ID).blur();
							$("#"+FIELD_ID).focus();
						},
						focus: function (event, ui) {
 					    //  this.value = ui.item.label;
       						event.preventDefault(); // Prevent the default focus behavior.
						}
					}).data("autocomplete")._renderItem = function (ul, item) {
        return $("<li>")
            .data("item.autocomplete", item)
            //.append("<a>" + item.concept.term+ "</a>") // shows term only 
            .append("<a title='" + item.concept.conceptFsn + "'>" + item.concept.term+ "</a>")// shows fsn mouseover
            .appendTo(ul);
    };    
    

}

