class InventoryUtil {

	public static Map<String,List<Instance>> fetchInstances(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName,String ec2Filters){
		Map<String,List<Instance>> instanceMap = new LinkedHashMap<>();
		AmazonEC2 ec2Client ;
		String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"EC2\" , \"region\":\"" ;
		List<String> stateNameFilters = Arrays.asList(ec2Filters.split(","));
		for(Region region : RegionUtils.getRegions()) {
			try{
			if(!skipRegions.contains(region.getName())){
				ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				List<Instance> instanceList = new ArrayList<>();
				DescribeInstancesResult  descInstResult ;
				String nextToken = null;
				do{
					descInstResult =  ec2Client.describeInstances(new DescribeInstancesRequest().withNextToken(nextToken));
					descInstResult.getReservations().forEach(
							reservation -> instanceList.addAll(reservation.getInstances().stream().filter(instance->stateNameFilters.contains(instance.getState().getName())).collect(Collectors.toList())));
					nextToken = descInstResult.getNextToken();
				}while(nextToken!=null);

				if(!instanceList.isEmpty() ) {
					log.debug(InventoryConstants.ACCOUNT + accountId + " Type : EC2 "+ region.getName()+" >> " + instanceList.size());
					instanceMap.put(accountId+delimiter+accountName+delimiter+region.getName(), instanceList);
				}
		   	}
			}catch(Exception e){
		   		log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
				ErrorManageUtil.uploadError(accountId,region.getName(),"ec2",e.getMessage());
		   	}
		}
		return instanceMap;
	}

	private  InventoryUtil();

}