import com.robothy.s3.rest.LocalS3;
import com.robothy.s3.rest.bootstrap.LocalS3Mode;
import com.amazonaws.services.s3.*;
import com.amazonaws.*;
import com.amazonaws.client.builder.*;
import com.amazonaws.services.s3.model.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        LocalS3 localS3 = LocalS3.builder()
                .port(19090)
                .mode(LocalS3Mode.PERSISTENCE)
                .dataPath("./local-s3")
                .build();

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("Shutting down Local S3 server...");
            localS3.shutdown();
            System.out.println("Local S3 server shut down gracefully.");
        }));

        localS3.start();
        System.out.println("Local S3 server started on port 19090");

	int port = 19090;
	AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .enablePathStyleAccess()
        .withClientConfiguration(new ClientConfiguration().withClientExecutionTimeout(0))
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
            "http://localhost:" + port, "local"
        )).build();

	String bucketName = "jamkazam-tency-uploads";

        try {
            // Check if the bucket exists
            if (s3.doesBucketExistV2(bucketName)) {
                System.out.println("Bucket already exists: " + bucketName);
            } else {
                // Create the bucket
                Bucket bucket = s3.createBucket(bucketName);
                System.out.println("Bucket created successfully: " + bucket.getName());
            }
        } catch (Exception e) {
            System.err.println("Error occurred while creating or checking the bucket: " + e.getMessage());
            e.printStackTrace();
        }

	String filePath = "ace-of-base_the-sign_10111.zip";
//	String filePath = "tester.file";
	String s3Key = filePath;

	 try {
            // Check if the object already exists
            boolean objectExists = doesObjectExist(s3, bucketName, s3Key);
            if (objectExists) {
                System.out.println("Object already exists in S3: " + s3Key);
            } else {
                // Upload the file
                File file = new File(filePath);
                if (!file.exists()) {
                    System.out.println("File not found: " + filePath);
                    return;
                }

                PutObjectRequest request = new PutObjectRequest(bucketName, s3Key, file);
		System.out.println("Uploading file " + s3Key);
                s3.putObject(request);

                System.out.println("File uploaded successfully to S3: " + s3Key);
            }
        } catch (Exception e) {
            System.err.println("Error occurred while checking or uploading to S3: " + e.getMessage());
            e.printStackTrace();
        }

        // Keep the main thread alive to avoid premature termination
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            System.err.println("Main thread interrupted: " + e.getMessage());
        }
    }

    private static boolean doesObjectExist(AmazonS3 s3, String bucketName, String objectKey) {
        try {
            s3.getObjectMetadata(new GetObjectMetadataRequest(bucketName, objectKey));
            return true; // Object exists
        } catch (com.amazonaws.services.s3.model.AmazonS3Exception e) {
            if (e.getStatusCode() == 404) {
                return false; // Object does not exist
            } else {
                throw e; // Rethrow for other errors
            }
        }
    }
}

