class InventoryUtil {

	public static Map<String,List<PhdVH>> fetchPHDInfo(BasicSessionCredentials temporaryCredentials,String accountId,String accountName) {

		Map<String,List<PhdVH>> phd = new LinkedHashMap<>();
		AWSHealth awsHealth;
		String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource\" ,\"type\": \"PHD\"" ;
		try{
			awsHealth = AWSHealthClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion("us-east-1").build();
			List<PhdVH> phdList = new ArrayList<>();
			List<Event> resultEvents = new ArrayList<>();
			String nextToken = "";
			do {
				DescribeEventsRequest describeEventsRequest = new DescribeEventsRequest().withMaxResults(100);
				if (!StringUtils.isEmpty(nextToken)) {
					describeEventsRequest.withNextToken(nextToken);
				}
				DescribeEventsResult eventsResult = awsHealth.describeEvents(describeEventsRequest);
				nextToken = eventsResult.getNextToken();
				resultEvents.addAll(eventsResult.getEvents());
			} while (!StringUtils.isEmpty(nextToken));
			List<String> eventArns = resultEvents.stream().map(Event::getArn).collect(Collectors.toList());
			int eventSize = eventArns.size();
			List<String> eventArnsTemp = new ArrayList<>();
			for(int i =0 ; i<eventSize;i++){
				eventArnsTemp.add(eventArns.get(i));
				if((i+1)%10==0 || i == eventSize-1){ // 10 is because API could only accept 10 event arns max
					List<EventDetails> successfulEventDetails = awsHealth.describeEventDetails(new DescribeEventDetailsRequest().withEventArns(eventArnsTemp)). getSuccessfulSet();
					List<AffectedEntity> affectedEntities = new ArrayList<>();
					do {
						DescribeAffectedEntitiesRequest affectedEntitiesRequest = new DescribeAffectedEntitiesRequest().withMaxResults(100);
						if (!StringUtils.isEmpty(nextToken)) {
							affectedEntitiesRequest.withNextToken(nextToken);
						}
						DescribeAffectedEntitiesResult affectedEntitiesResult = awsHealth.describeAffectedEntities(affectedEntitiesRequest.withFilter(new EntityFilter().withEventArns(eventArnsTemp)));
						nextToken = affectedEntitiesResult.getNextToken();
						affectedEntities.addAll(affectedEntitiesResult.getEntities());
					} while (!StringUtils.isEmpty(nextToken));
					for(EventDetails eventDetail : successfulEventDetails) {
						PhdVH phdObj = new PhdVH();
						phdObj.setEventDetails(eventDetail);
						phdObj.setAffectedEntities(affectedEntities.parallelStream().filter(affEntity -> affEntity.getEventArn().equals(eventDetail.getEvent().getArn())).collect(Collectors.toList()));
						phdList.add(phdObj);
					}
					eventArnsTemp =  new ArrayList<>();
				}
			}
			if( !phdList.isEmpty() ) {
				log.debug(InventoryConstants.ACCOUNT + accountId +" Type : PHD "+ " >> "+phdList.size());
				phd.put(accountId+delimiter+accountName,phdList);
			}
		}catch(Exception e){
				log.error(expPrefix +", \"cause\":\"" +e.getMessage()+"\"}");
				ErrorManageUtil.uploadError(accountId,"","phd",e.getMessage());
		}
		return phd;
	}

	private  InventoryUtil();

}