class VulnerabilityService implements Constants {

	public Map<String, Object> getVulnerabilitySummaryByAssets(String assetGroup)
			throws ServiceException, DataException {

		List<String> vulnTargetTypes = getVulnTargetTypes(assetGroup);

		long totalCount = 0;
		long exemptedCount = 0;
		long inscopeCount = 0;
		long scannedCount = 0;
		long unscannedCount = 0;

		for (String vulnType : vulnTargetTypes) {
			try {
				long tempTotalCount = vulnerabilityRepository.getRunningInstancesCount(assetGroup, vulnType);
				long exemptedByRule = vulnerabilityRepository.getExemptedByRuleCount(assetGroup, vulnType);

				Request request = new Request();
				request.setAg(assetGroup);
				Map<String, String> filter = new HashMap<>();
				filter.put("domain", "Infra & Platforms");

				if (vulnType.equals(EC2)) {
					filter.put("ruleId.keyword", EC2_QUALYS_RULEID);
				} else if (vulnType.equals(VIRTUALMACHINE)) {
					filter.put("ruleId.keyword", VIRTUALMACHINE_QUALYS_RULEID);
				} else {
					filter.put("ruleId.keyword", ONPREM_QUALYS_RULEID);
				}
				request.setFilter(filter);
				//
				// Map<String, Object> response =
				// complianceService.getRulecompliance(request).getResponse().get(0);
				// new code starts
				List<LinkedHashMap<String, Object>> responses = new ArrayList<>();
				Map<String, Object> response = new HashMap<>();
				ResponseEntity<Object> nonCompliancePolicyRuleresponse = complianceServiceClient
						.getNonCompliancePolicyByRule(request);
				if (nonCompliancePolicyRuleresponse != null) {
					Map<String, Object> responseBody = (Map<String, Object>) nonCompliancePolicyRuleresponse.getBody();
					Map<String, Object> dataResponseMap = (Map<String, Object>) responseBody.get("data");
					responses = (List<LinkedHashMap<String, Object>>) dataResponseMap.get("response");
					if (responses.size() > 0) {
						response = responses.get(0);

						// Response response = (Response) dataResponse.get("data");
						// new code ends
						// This inScopeRule is total - exempted by age. SO exemptedByAge would be total-
						// inScopeRule;
						long inScopeRule = Long.valueOf(response.get("assetsScanned").toString());
						long exemptedByAge = tempTotalCount - inScopeRule;
						totalCount += tempTotalCount;
						exemptedCount += exemptedByAge + exemptedByRule;
						inscopeCount += inScopeRule - exemptedByRule; // inScopeRule includes those exempted by Rule as
																		// well.
						scannedCount += Long.valueOf(response.get("passed").toString()) - exemptedByRule;
						unscannedCount += Long.valueOf(response.get("failed").toString());
					}
				}
			} catch (Exception e) {
				throw new ServiceException(e);
			}
		}

		long nonCompliantCount = vulnerabilityRepository.getNonCompliantResourceIds(assetGroup).size();

		Map<String, Object> compliant = vulnerabilityRepository.getCompliantHostsBySeverity(assetGroup);
		Map<String, Object> noncompliant = vulnerabilityRepository.getUniqueHostBySeverity(assetGroup, "3,4,5");

		noncompliant.put("S3",
				Long.valueOf(noncompliant.get("S3").toString()) - Long.valueOf(compliant.get("S3").toString()));
		noncompliant.put("S4",
				Long.valueOf(noncompliant.get("S4").toString()) - Long.valueOf(compliant.get("S4").toString()));

		for (String key : compliant.keySet()) {
			Map<String, Long> countMap = new HashMap<>();
			countMap.put(COUNT, Long.valueOf(compliant.get(key).toString()));
			compliant.put(key, countMap);
		}

		for (String key : noncompliant.keySet()) {
			Map<String, Long> countMap = new HashMap<>();
			countMap.put(COUNT, Long.valueOf(noncompliant.get(key).toString()));
			noncompliant.put(key, countMap);
		}

		Map<String, Object> response = new HashMap<>();
		response.put(COUNT, totalCount);

		Map<String, Object> exempted = new HashMap<>();
		exempted.put(COUNT, exemptedCount);
		response.put("exempted", exempted);

		Map<String, Object> inscope = new HashMap<>();
		inscope.put(COUNT, inscopeCount);
		Map<String, Object> unscanned = new HashMap<>();
		unscanned.put(COUNT, unscannedCount);
		inscope.put("unscanned", unscanned);
		Map<String, Object> scanned = new HashMap<>();
		scanned.put(COUNT, scannedCount);

		compliant.put(COUNT, scannedCount - nonCompliantCount);
		scanned.put("compliant", compliant);
		noncompliant.put(COUNT, nonCompliantCount);
		scanned.put("noncompliant", noncompliant);

		inscope.put("scanned", scanned);
		response.put("inscope", inscope);

		return response;
	}

}