import * as fs from "fs";
import * as path from "path";
import {ImagingBase} from "./imaging-base";
import {
    CreateResizedImageRequest, ResizeImageRequest,
    ImagingApi,
} from "@asposecloud/aspose-imaging-cloud";

const ImageFileName = "example_image.gif";
const ImagesFolder = "ExampleImages";
const CloudFolder = "CloudImages";
const OutputFolder = "Output";

// Get ClientId and ClientSecret from https://dashboard.aspose.cloud/ 
// or use on-premise version (https://docs.aspose.cloud/imaging/getting-started/how-to-run-docker-container/)
const api = new ImagingApi(clientSecret, clientId, "https://api.aspose.cloud");

/**
* Resizes the image from cloud storage.
*/
public async ResizeImageFromStorage() {

    const localInputImage = fs.readFileSync(path.join(ImagesFolder, ImageFileName));
    const uploadFileRequest = new UploadFileRequest({path: path.join(CloudFolder, imageName), file: image});
    const result = await this.api.uploadFile(uploadFileRequest);
    if (result.errors.length > 0)
        console.log(`Uploading errors count: ${result.errors.length}`

    // Please refer to https://docs.aspose.cloud/imaging/supported-file-formats/#resize
    // for possible formats
    const format = "gif";     
    const width = 100;
    const height = 150;
    const folder = this.CloudFolder; // Input file is saved at the desired folder in the storage
    const storage = undefined; // We are using default Cloud Storage

    const request = new ResizeImageRequest({
            name: this.ImageFileName,
            format,
            width,
            height,
            folder,
            storage,
        });

    const resizedImage = await this.api.resizeImage(request);

    // Save the image file to output folder
    const resizedImageName = ImageFileName.substr(0, ImageFileName.lastIndexOf(".")) + "gif";
    const filePath = path.resolve(path.join(OutputFolder, resizedImageName));
    fs.writeFile(filePath, resizedImage, (err) => {
        if (err) {
            throw err;
            }
    });
}

/**
* Resizes an image. Image data is passed in a request stream.
*/
public async CreateResizedImageFromRequest() {

    // Please refer to https://docs.aspose.cloud/imaging/supported-file-formats/#resize
    // for possible formats
    const format = "gif";
    const width = 100;
    const height = 150;
    const outPath = undefined; // Path to updated file (if this is empty, response contains streamed image)
    const storage = undefined; // We are using default Cloud Storage

    const inputStream = fs.readFileSync(path.resolve(ImagesFolder, ImageFileName));
    const request = new CreateResizedImageRequest({
            imageData: inputStream,
            format,
            width,
            height,
            outPath,
            storage,
        });
    const resizedImage = await this.api.createResizedImage(request);

    // Save the image file to output folder
    const resizedImageName = ImageFileName.substr(0, ImageFileName.lastIndexOf(".")) + "gif";
    const filePath = path.resolve(path.join(OutputFolder, resizedImageName));
    fs.writeFile(filePath, resizedImage, (err) => {
        if (err) {
            throw err;
            }
    });
}