//(function () {

angular.module('MobileWebLogistics')
	.service('searchResult', function () {

		this.showResults = function ($scope, $location, results, addup) {
			document.getElementById("clearResultButton").style.backgroundColor = 'rgba(' + $scope.mSelParcelSymbolFill[0] + ', ' + $scope.mSelParcelSymbolFill[1] + ', ' + $scope.mSelParcelSymbolFill[2] + ', ' + $scope.mSelParcelSymbolFill[3] + ')';

			$scope.advSearch = false;
			//$scope.queryResults = [];
			$("#submitSearchMap").blur();
			$("#submitSearch").blur();

			var j = 0;
			if (addup != true) {
				$scope.queryResults = [];
				$scope.multiSelectedLayer.clear();
			}
			else if (addup == true) {
				var j = $scope.queryResults.length;
			}
			for (var i = 0; i < results.features.length; i++) {
				var totVal = 0;
				var uniquePId = results.features[i].attributes[$scope.parcelUid];
				var isUnique = true;
				for (var k = 0; k < $scope.queryResults.length; k++) {
					if ($scope.queryResults[k][$scope.parcelUid] == uniquePId) {
						isUnique = false;
						k = $scope.queryResults.length;
					}
				}
				if (isUnique) {
					if ($scope.sumQueryResultFld.length > 1) {
						for (var k = 0; k < $scope.sumQueryResultFld.length; k++) {
							totVal += parseInt(results.features[i].attributes[$scope.sumQueryResultFld[k]]);
						}
						results.features[i].attributes['customTotVal'] = $scope.formatParcelData('customTotVal', totVal, results.features[i].attributes['customTotVal']);

					}

					$scope.queryResults[j] = results.features[i]['attributes'];

					$scope.queryResults[j]['geometry'] = results.features[i]['geometry'];
					if (results.geometryType == "esriGeometryPolygon") {
						var hSymbol = $scope.mSelParcelSymbol;
					}
					else {
						var hSymbol = $scope.mSelPointSymbol;
					}
					var highlightGraphic = new $scope.Graphic($scope.queryResults[j]['geometry'], hSymbol);
					//console.log (results.features[i]['geometry']);			
					if ($scope.mapMyResults == "true") {
						$scope.multiSelectedLayer.add(highlightGraphic);
					}
					for (var key in $scope.queryResults[j]) {
						thisKey = $scope.queryResults[j][key.toString()];//console.log("thisKey:"+thisKey);
						$scope.queryResults[j][key.toString()] = $scope.formatParcelData(key, thisKey, $scope.queryResults[j][key.toString()]);
					}
					$scope.queryResults[j]['index'] = j;
					j++;
				}
				//}
				//$scope.queryResults[j-1]['index'] = j-1;
			}
			// console.log($scope.queryResults);
			// if(addup != true){
			// $scope.multiSelectedLayer.hide();
			// }
			// else if(addup == true){
			// 	$("#clearResultButton").addClass("showbtn");
			// 	$scope.multiSelectedLayer.show();
			// }

			if (j < 1) {
				$scope.emptySearch = true;
				$("#clearResultButton").removeClass("showbtn");
				if ($scope.searchBtn == "submitSearchMap") {
					alert("No Results Found.");
				}
			} else {
				$scope.emptySearch = false;
				/*if($scope.mapResults=="true"){
					$("#clearResultButton").addClass("showbtn");
					var multiSelectedLayerExtent = $scope.graphicsUtils.graphicsExtent($scope.multiSelectedLayer.graphics);
					$scope.map.setExtent(multiSelectedLayerExtent.expand(1.5));	
				}*/

				//console.log(multiSelectedLayerExtent);
			}

			$scope.$apply();


			if ($scope.clearResults == "false") {
				for (var i = 0; i < results.features.length; i++) {
					$("#closeIndResultBtn").addClass("hideOption");
				}
			}
			if ($scope.callFromAS == 'yes') {
				//remove all parameters from query string
				$location.search({});
			}

			setTimeout(function () {
				if (j == 1 && $scope.fromNearby == false && $scope.searchBtn != "submitSearchMap") {
					if ($scope.noReport == 'true') {

						$("#pAddr").click();
					}
					else if ($scope.callFromAS == 'yes') { //alert("g");
						setTimeout(function () {
							$(".mapIt").click();
							$scope.callFromAS = 'no';
						}, 400);
					}
					else {
						$(".details").click();
					}


				}
				$scope.fromNearby = false;
				$scope.myScroll.refresh();
				$("#searchPreloaderContainer").removeClass("active");
				$("#identifyPreloaderContainer").removeClass("active");
				//1$("#parcelPreloaderContainer").removeClass("active");              
				$("#adjoinerList").css("z-index", 21);

			}, 100);
			setTimeout(function () {


				if (addup != true) {
					$scope.multiSelectedLayer.hide();
					$("#mapContainer").addClass("inactive");
					$("#listResults").removeClass("inactive");
					$("#listResults").addClass("active");
				}
				else if (addup == true) {
					$("#clearResultButton").removeClass("showbtn");
					$scope.multiSelectedLayer.show();
				}



				if ($scope.searchBtn == "submitSearchMap") {
					$scope.showMap();

				}
				$scope.searchBtn = "";
			}, 500);


		}
		this.removeParcel = function ($scope, index, panel) { // remove items from result list, -100 is for deleting all
			if (index == -100) {
				$scope.queryResults = [];
				$scope.multiSelectedLayer.clear();
				$scope.drawLayerForSelect.clear();
				$("#clearResultButton").removeClass("showbtn");
				$(':input').val('');
				// Setting the any input element val to '' will empty the default option for the map print. Set a few back to the default
				$("#printSize").val('Letter ANSI A Portrait');
				$("#baseMapTransparency").val($scope.initOpacity);
				$("#overlayTransparency").val($scope.initOpacity);

				$("#ndefaultSearch #submitSearchMap").val('Map Results');
				$("#nadvancedSearch #submitSearchMap").val('Map Results');
				$("#ndefaultSearch #submitSearch").val($scope.searchBtnTxt);
				$("#nadvancedSearch #submitSearch").val($scope.searchBtnTxt);
				$("#selectParcelToolType").val($scope.selectParcelToolTypeModel);

				try {
					$('#priceSlider').val([$scope.searchPriceMin, $scope.searchPriceMax]);
				}
				catch (err) {
					console.log(err.message);
				}
			}
			else {
				for (var i = 0; i < $scope.queryResults.length; i++) {
					if ($scope.queryResults[i]["index"] == index) { $scope.arrayIndex = i }
				}
				$scope.queryResults.splice($scope.arrayIndex, 1);		//delete a record from result by index	
				if ($scope.mapMyResults == "true") {
					$scope.multiSelectedLayer.remove($scope.multiSelectedLayer.graphics[$scope.arrayIndex]);
				}

				//console.log($scope.multiSelectedLayer);			
			}
		}
		this.removeAdjParcel = function ($scope, index, panel) { // remove items from result list, -100 is for deleting all
			if (index == -100) {
				$scope.adjoinerList = [];
			}
			else {
				for (var i = 0; i < $scope.adjoinerList.length; i++) {
					if ($scope.adjoinerList[i]["index"] == index) { $scope.adjArrayIndex = i }
				}
				$scope.adjoinerList.splice($scope.adjArrayIndex, 1);		//delete a record from result by index					
			}
		}

		this.showResults2 = function ($scope, results) {
			document.getElementById("clearResultButton").style.backgroundColor = 'rgba(' + $scope.mSelParcelSymbolFill[0] + ', ' + $scope.mSelParcelSymbolFill[1] + ', ' + $scope.mSelParcelSymbolFill[2] + ', ' + $scope.mSelParcelSymbolFill[3] + ')';

			$scope.advSearch = true;
			$scope.thisSearchLayer = "";
			$scope.queryResults = []; //alert(results.features.length);
			$("#submitSearchMap").blur();
			$("#submitSearch").blur();
			$scope.multiSelectedLayer.clear();
			var j = 0;
			for (var i = 0; i < results.features.length; i++) {

				$scope.queryResults[j] = results.features[i]['attributes'];
				$scope.queryResults[j]['geometry'] = results.features[i]['geometry'];
				if (results.geometryType == "esriGeometryPolygon") {
					var hSymbol = $scope.mSelParcelSymbol;
				}
				else {
					var hSymbol = $scope.mSelPointSymbol;
				}
				var highlightGraphic = new $scope.Graphic($scope.queryResults[j]['geometry'], hSymbol);
				if ($scope.mapMyResults == "true") {
					$scope.multiSelectedLayer.add(highlightGraphic);
				}
				for (var key in $scope.queryResults[j]) {
					thisKey = $scope.queryResults[j][key.toString()];
					$scope.queryResults[j][key.toString()] = $scope.formatParcelData(key, thisKey, $scope.queryResults[j][key.toString()]);
				}
				$scope.queryResults[j]['index'] = j;
				j++;
				//}
			}

			if (j < 1) {
				$scope.emptySearch = true;
				$("#clearResultButton").removeClass("showbtn");
				if ($scope.searchBtn == "submitSearchMap") {
					alert("No Results Found.");
				}
			}
			else {
				$scope.emptySearch = false;
				if ($scope.mapMyResults == "true") {
					$("#clearResultButton").addClass("showbtn");
					var multiSelectedLayerExtent = $scope.graphicsUtils.graphicsExtent($scope.multiSelectedLayer.graphics);
					$scope.map.setExtent(multiSelectedLayerExtent.expand(1.5));
				}
			}
			//console.log($scope.queryResults);
			$scope.$apply();
			setTimeout(function () {
				if (j == 1) {
					if ($scope.noReport == 'true') {
						$("#advpAddr").click();
					}
					else {
						$(".details").click();
					}
				}
				$scope.myScroll.refresh();
				$("#searchPreloaderContainer").removeClass("active");
				$("#identifyPreloaderContainer").removeClass("active");
				//1$("#parcelPreloaderContainer").removeClass("active");              
				$("#adjoinerList").css("z-index", 21);

			}, 100);
			setTimeout(function () {

				$("#mapContainer").addClass("inactive");
				$("#listResults").removeClass("inactive");
				$("#listResults").addClass("active");
				if ($scope.searchBtn == "submitSearchMap") {
					$scope.showMap();
				}
				$scope.searchBtn = "";
			}, 400);
		}
		this.openReportLink = function ($scope, linkAttribute) {
			if ($scope.openReportLinkDelay == 0) {
				window.open(linkAttribute);
				$scope.openReportLinkDelay = 1;
			}
			setTimeout(function () {
				$scope.openReportLinkDelay = 0;
			}, 1000);
		}
	});

//})();