class AssetGroupEmailService implements Constants {

	@SuppressWarnings("unchecked")
	public void sendDigestMail(String assetGroupName, String ownerName, String ownerEmail, String mainTemplateUrl, String domainUrl) {
		try {
			Map<String, Object> report = Maps.newHashMap();
			Response patchingDetails =  complianceServiceClient.getPatching(assetGroupName);
			Response certificateDetails =  complianceServiceClient.getCertificates(assetGroupName);
			Response taggingDetails =  complianceServiceClient.getTagging(assetGroupName);
			Response vulnerabilityDetails =  complianceServiceClient.getVulnerabilities(assetGroupName);
			Response complianceStats = statisticsServiceClient.getComplianceStats(assetGroupName);
			Response topNonCompliantApps = complianceServiceClient.getTopNonCompliantApps(assetGroupName);
			Response applicationNames = complianceServiceClient.getVulnerabilityByApplications(assetGroupName);
			Response issueDetails = complianceServiceClient.getDistribution(assetGroupName);
			Map<String, Object> issueDistributionDetails = (Map<String, Object>) issueDetails.getData().get("distribution");
			List<Map<String, Object>> topAppsAndDirectors = (List<Map<String, Object>>) topNonCompliantApps.getData().get("response");
			List<Map<String, Object>> topApplicationNames = (List<Map<String, Object>>) applicationNames.getData().get("response");
			Set<String> appNames = getAppNames(topApplicationNames);
			report.put("appNames", appNames);
			report.put("appNamesCount", appNames.size());
			report.put("ownerName", ownerName);
			
			/**
			 * 
			 *  Top App's and Directors with Un_patched Assets Details
			 *  
			 **/
			if(!topAppsAndDirectors.isEmpty()){
				if(topAppsAndDirectors.size()>=5){
					report.put(APP_TAB_DETAILS, topAppsAndDirectors.subList(0, 5));
					report.put(TOP_APP_COUNT, 5);
				} else {
					report.put(APP_TAB_DETAILS, topAppsAndDirectors.subList(0, topAppsAndDirectors.size()));
					report.put(TOP_APP_COUNT, topAppsAndDirectors.size());
				}
			} else {
				report.put(TOP_APP_COUNT, 0);
				report.put(APP_TAB_DETAILS, Lists.newArrayList());
			}

			Map<String, Double> compliant = (Map<String, Double>) complianceStats.getData().get("compliance_stats");
			report.put("overallCompliance", compliant.get("overall_compliance"));
			/**
			 * 
			 *  Patching Details
			 *  
			 **/
			Map<String, Integer> output = (Map<String, Integer>) patchingDetails.getData().get(OUTPUT);
			int unpatchedCount = output.get("unpatched_instances");
			int unpatchedTotalInstances = getAssetGroupTotalCount("total_instances", output);
			int unpatchedCompliant = roundOf(compliant.get("patch_compliance"));
			report.put("unpatchedCount", unpatchedCount);
			report.put("unpatchedCompliant", unpatchedCompliant);
			report.put("unpatchedTotalInstances", unpatchedTotalInstances);
			
			/**
			 * 
			 *  Vulnerability Details
			 *  
			 **/
			output = (Map<String, Integer>) vulnerabilityDetails.getData().get(OUTPUT);
			int vulnerabilitiesCount = output.get("vulnerabilities");
			int vulnerabilitiesTotalHosts = getAssetGroupTotalCount("hosts", output);
			int vulnerabilitiesCompliant = roundOf(compliant.get("vuln_compliance"));
	        report.put("vulnerabilitiesCount", vulnerabilitiesCount);
			report.put("vulnerabilitiesCompliant", vulnerabilitiesCompliant);
			report.put("vulnerabilitiesTotalHosts", vulnerabilitiesTotalHosts);

			/**
			 * 
			 *  Tagging Details
			 *  
			 **/
			
			output = (Map<String, Integer>) taggingDetails.getData().get(OUTPUT);
			int untaggedCount = output.get("untagged");
			int untaggedTotalInstances = getAssetGroupTotalCount("assets", output);
			int untaggedCompliant = roundOf(compliant.get("tag_compliance"));
			report.put("untaggedCount", untaggedCount);
			report.put("untaggedCompliant", untaggedCompliant);
			report.put("untaggedTotalInstances", untaggedTotalInstances);
			/**
			 * 
			 *  Certificate Details
			 *  
			 **/
			output = (Map<String, Integer>) certificateDetails.getData().get(OUTPUT);
			int expiringCount = output.get("certificates_expiring");
			int expiringDays = getAssetGroupTotalCount("certificates", output);
			int expiringCompliant = roundOf(compliant.get("cert_compliance"));
			report.put("expiringCount", expiringCount);
			report.put("expiringCompliant", expiringCompliant == 0 ? 100: expiringCompliant);
			report.put("expiringDays", expiringDays);
			/**
			 * 
			 *  Critical Issue Details
			 *  
			 **/
			Map<String, Integer> criticalIssuesDetails = (Map<String, Integer>)issueDistributionDetails.get("distribution_by_severity"); 
			if(criticalIssuesDetails.isEmpty()) {
				report.put(CRITICAL_ISSUE_COUNT, 0);
			} else {
				Integer criticalIssuesCount = criticalIssuesDetails.get("critical");
				if(criticalIssuesCount != null) {
					report.put(CRITICAL_ISSUE_COUNT, criticalIssuesCount);	
				} else {
					report.put(CRITICAL_ISSUE_COUNT, 0);
				}
			}

			report.put("criticalIssuesCompliant", compliant.get("overall_compliance"));
			report.put("criticalIssuesTotalIssues", issueDistributionDetails.get("total_issues"));
			domainUrl = domainUrl+"/un-subscribe?email="+ownerEmail;
			report.put("subscriptionUrl", domainUrl);
			String attachmentUrl="";
			List<String> toDetails = Lists.newArrayList(); 
			toDetails.add(ownerEmail); 

	        String emailContent = mailService.processTemplate(mainTemplateUrl, report);
	        log.info(EXE_EMAIL_SEND,assetGroupName);
		    if(emailContent != null) {
		    	mailService.prepareTemplateAndSendMail("noreply@pacman.t-mobile.com", toDetails, "Asset Group Weekly Report - "+assetGroupName, emailContent, attachmentUrl);
		    }
		} catch(Exception exception) {
			log.info("Exception in sendDigestMail:",exception);
		}
	}

}