class InventoryUtil {

	public static Map<String,List<BucketVH>>  fetchS3Info(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
		Map<String,List<BucketVH>> s3Map = new HashMap<>();
		String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource\" ,\"type\": \"S3\" , \"Bucket\":\"" ;
		AmazonS3 amazonS3Client ;
		List<BucketVH> buckets = new ArrayList<>();
		/* A region is needed for the client and setting to us-east-1 is causing issues */
		amazonS3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(InventoryConstants.REGION_US_WEST_2).build();
		List<Bucket> s3buckets = amazonS3Client.listBuckets();
		log.debug(InventoryConstants.ACCOUNT + accountId +" Type : S3 "+  " >> "+s3buckets.size());
		Map<String,AmazonS3> regionS3map = new HashMap<>();
		for(Region region : RegionUtils.getRegions()){
			if(!skipRegions.contains(region.getName())){
				regionS3map.put(region.getName(), AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build());
			}
		}
		s3buckets.parallelStream().forEach(bucket -> {
			String bucketRegion ="";
            BucketLoggingConfiguration bucketLoggingConfiguration = null;
			BucketVersioningConfiguration versionconfig = null;
			List<Tag> tags = new ArrayList<>();
			boolean hasWebSiteConfiguration = false;
			try{
				String bucketLocation = amazonS3Client.getBucketLocation(bucket.getName());
				bucketRegion = com.amazonaws.services.s3.model.Region.fromValue(bucketLocation).toAWSRegion().getName();
				AmazonS3 s3Client = regionS3map.get(bucketRegion);
				versionconfig =  s3Client.getBucketVersioningConfiguration(bucket.getName());
                bucketLoggingConfiguration = s3Client.getBucketLoggingConfiguration(bucket.getName());
				BucketTaggingConfiguration tagConfig = s3Client.getBucketTaggingConfiguration(bucket.getName());
				if(tagConfig!=null){
					List<TagSet> tagSets = tagConfig.getAllTagSets();
					for(TagSet ts : tagSets){
						Map<String,String> tagsTemp = ts.getAllTags();
						Iterator<Entry<String,String>> it = tagsTemp.entrySet().iterator();
						while(it.hasNext()){
							Entry<String,String> tag = it.next();
							tags.add(new Tag(tag.getKey(),tag.getValue()));
						}
					}
				}
				String bucketEncryp = fetchS3EncryptInfo(bucket, s3Client);
				BucketWebsiteConfiguration bucketWebsiteConfiguration = s3Client
                        .getBucketWebsiteConfiguration(bucket.getName());
				if(bucketWebsiteConfiguration!=null) {
	                  hasWebSiteConfiguration=true;
				}
	              
				synchronized(buckets){
					buckets.add(new BucketVH(bucket,bucketRegion,versionconfig, tags, bucketEncryp,hasWebSiteConfiguration,bucketLoggingConfiguration));
				}
			}
			catch(AmazonS3Exception e){
				if("AccessDenied".equals(e.getErrorCode())){
					log.info("Access Denied for bucket " + bucket.getName());
					buckets.add(new BucketVH(bucket,"",versionconfig, tags, null,hasWebSiteConfiguration,bucketLoggingConfiguration));
				}else{
					log.info("Exception fetching S3 Bucket",e);
				}
			}
			catch(Exception e){
				log.warn(expPrefix+ bucket.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
				ErrorManageUtil.uploadError(accountId,"","s3",e.getMessage());
			}
		});
		log.debug(InventoryConstants.ACCOUNT + accountId +" Type : S3 >> "+buckets.size());
		if(!buckets.isEmpty()){
			s3Map.put(accountId+delimiter+accountName, buckets);
		}
		return s3Map;
	}

	private  InventoryUtil();

}