import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.ResourceUri;
import com.aspose.slides.model.Slides;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class Example_AddPresentationSlides {
    protected static SlidesApi presentationApi;

    public Example_AddPresentationSlides() {
        if (presentationApi == null) {
            presentationApi = new SlidesApi("appSid", "appKey");
        }
    }

    public void addSlide() throws ApiException, IOException {
        String localPath = "/home/downloads/";
        String fileName = "Sample.pptx";
        String storageFolderName = "TempTests";

        presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null);

        // Add a presentation slide
        Slides response = presentationApi.createSlide(fileName, null,1, null, storageFolderName, null);

        for (ResourceUri slide : response.getSlideList())
        {
            System.out.println(slide.getHref());
        }

        File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null);

        // Copy the downloaded presentation with new slide added to the local directory
        copyFile(presentationFile, new File(localPath, fileName));

        System.out.println("Presentation slide deleted and copied to: " + localPath + fileName);
    }

    public static byte[] readFileToByteArray(String filePath) throws IOException {
        Path path = new File(filePath).toPath();
        return Files.readAllBytes(path);
    }

    private void copyFile(File sourceFile, File targetFile) throws IOException {
        if (sourceFile == null || !sourceFile.exists()) {
            throw new IOException("Source file does not exist: " + sourceFile);
        }

        // Ensure the target directory exists
        Path targetPath = targetFile.toPath();
        Files.createDirectories(targetPath.getParent());

        // Copy the file
        Files.copy(sourceFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING);
    }
}