class PatchingServiceImpl implements PatchingService, Constants {

	@Override
	public Medal getStarRatingForAgPatching(String ag, int quarter, int year) {
		
	    LocalDate firstDay =  LocalDate.parse(repository.getAmilAvailDate(year, quarter));
		LocalDate lastDay = getLastDayOfQuarter(quarter, year);

		boolean incompleteQuarter = false;

		// We don't want data for future weeks. If the quarter being
		// requested is the ongoing quarter, the max we we are interested
		// is data up to and including the ongoing day in the ongoing week.
		if (lastDay.isAfter(LocalDate.now())) {
			lastDay = LocalDate.now();
			incompleteQuarter = true;
		}

		List<Map<String, Object>> ratingList = repository.getPatchingPercentForDateRange(ag, firstDay, lastDay);
		List<Map<String, Object>> filteredRatingList = new ArrayList<Map<String, Object>>();

		ratingList.forEach(rateMap -> {
			if (ag.equals(rateMap.get("ag"))) {
				filteredRatingList.add(rateMap);
			}
		});
		
		

      

		// Sort the list by the date in descending order
		Comparator<Map<String, Object>> comp = (m1, m2) -> LocalDate
				.parse(m2.get("date").toString(), DateTimeFormatter.ISO_DATE)
				.compareTo(LocalDate.parse(m1.get("date").toString(), DateTimeFormatter.ISO_DATE));
		Collections.sort(filteredRatingList, comp);
		
		List<String> patchingPercentFor45Days = new ArrayList<>(); 
		filteredRatingList.forEach(rateMap->{
			LocalDate d = LocalDate.parse(rateMap.get("date").toString(),
					DateTimeFormatter.ISO_DATE);
			if((patchingPercentFor45Days.isEmpty()) && (ChronoUnit.DAYS.between(firstDay,d)<45)) {
				patchingPercentFor45Days.add(rateMap.get("patching_percentage").toString());
			}
		});
		
		List<String> patchingPercentFor60Days = new ArrayList<>(); 
		filteredRatingList.forEach(rateMap->{
			LocalDate d = LocalDate.parse(rateMap.get("date").toString(),
					DateTimeFormatter.ISO_DATE);
			if((patchingPercentFor60Days.isEmpty()) && (ChronoUnit.DAYS.between(firstDay,d)<60)) {
				patchingPercentFor60Days.add(rateMap.get("patching_percentage").toString());
			}
		});

		Medal patchingMedal = new Medal();
		patchingMedal.setMedalType("");
		patchingMedal.setMedalStatus("");
		
		double patchingPercentValueFor45Days = patchingPercentFor45Days.isEmpty()?0:Double.parseDouble(patchingPercentFor45Days.get(0));
		double patchingPercentValueFor60Days = patchingPercentFor60Days.isEmpty()?0:Double.parseDouble(patchingPercentFor60Days.get(0));
		double lastDatePatchingPercentValue = filteredRatingList.isEmpty()?0:Double.parseDouble(filteredRatingList.get(0).get("patching_percentage").toString());
		
		if(incompleteQuarter) {
			patchingMedal.setMedalStatus("Pending");
			if(patchingPercentValueFor45Days>=100) {
				patchingMedal.setMedalType("GOLD");
			}else if(patchingPercentValueFor60Days>=100) {
				patchingMedal.setMedalType("SILVER");
			}
		}else {
			//If we reach here, this means quarter end data is available
			patchingMedal.setMedalStatus("Confirmed");
			if(patchingPercentValueFor45Days>=100 && lastDatePatchingPercentValue>=100) {
				patchingMedal.setMedalType("GOLD");
			}else if(patchingPercentValueFor45Days>=100 && lastDatePatchingPercentValue>=90) {
				patchingMedal.setMedalType("SILVER");
			}else if(patchingPercentValueFor60Days>=100 && lastDatePatchingPercentValue>=100) {
				patchingMedal.setMedalType("SILVER");
			}else if(patchingPercentValueFor60Days>=100 && lastDatePatchingPercentValue>=90) {
				patchingMedal.setMedalType("BRONZE");
			}else if(lastDatePatchingPercentValue>=100) {
				patchingMedal.setMedalType("BRONZE");
			}else {
				patchingMedal.setMedalStatus("");
			}
			
		}

		return patchingMedal;
	}

}