import com.jfrog.ResponsePair
import com.jfrog.filestransfer.FilesTransfer
import com.jfrog.general.General
import org.apache.commons.lang3.exception.ExceptionUtils
import org.artifactory.exception.CancelException
import org.artifactory.resource.ResourceStreamHandle

executions {
    /**
     * Return the node ID of the server.
     */
    pingDataTransfer(httpMethod: 'GET') {
        try {
            message = General.pingDataTransfer(security, log)
            status = 200
        } catch (CancelException e) {
            message = e.getMessage()
            status = e.getErrorCode()
        } catch (Throwable e) {
            log.error("An unexpected error occurred during GET pingDataTransfer", e)
            message = ExceptionUtils.getRootCauseMessage(e)
            status = 500
        }
    }

    /**
     * Send bulk of Artifacts to the target Artifactory.
     * If all files uploaded were checksum deployed successfully, return 200 OK.
     * If one or more file requires a regular upload, return a UUID token and start an upload thread.
     */
    uploadChunk(httpMethod: 'POST') { ResourceStreamHandle body ->
        try {
            ResponsePair response = FilesTransfer.uploadChunk(body, security, repositories, log)
            message = response.getMessage()
            status = response.getStatusCode()
        } catch (CancelException e) {
            message = e.getMessage()
            status = e.getErrorCode()
        } catch (Throwable e) {
            log.error("An unexpected error occurred during POST uploadChunk", e)
            message = ExceptionUtils.getRootCauseMessage(e)
            status = 500
        } finally {
            body.close()
        }
    }

    /**
     * Get or delete the status of the input chunk tokens.
     */
    syncChunks(httpMethod: 'POST') { ResourceStreamHandle body ->
        try {
            message = FilesTransfer.syncChunks(body, security, log)
            status = 200
        } catch (CancelException e) {
            message = e.getMessage()
            status = e.getErrorCode()
        } catch (Throwable e) {
            log.error("An unexpected error occurred during POST syncChunks", e)
            message = ExceptionUtils.getRootCauseMessage(e)
            status = 500
        } finally {
            body.close()
        }
    }

    /**
     * Verify compatibility between JFrog CLI version and this plugin's version.
     */
    verifyCompatibility(httpMethod: 'POST') { ResourceStreamHandle body ->
        try {
            message = FilesTransfer.verifyCompatibility(body, security, log)
        } catch (CancelException e) {
            message = e.getMessage()
            status = e.getErrorCode()
        } catch (Throwable e) {
            log.error("An unexpected error occurred during POST verifyCompatibility", e)
            message = ExceptionUtils.getRootCauseMessage(e)
            status = 500
        }
    }

    /**
     * Verify connectivity between source and target Artifactory servers, including the proxy.
     */
    verifySourceTargetConnectivity(httpMethod: 'POST') { ResourceStreamHandle body ->
        try {
            FilesTransfer.verifySourceTargetConnectivity(body, security, log)
            status = 200
        } catch (CancelException e) {
            message = e.getMessage()
            status = e.getErrorCode()
        } catch (Throwable e) {
            log.error("An unexpected error occurred during POST verifySourceTargetConnectivity", e)
            message = ExceptionUtils.getRootCauseMessage(e)
            status = 500
        }
    }

    /**
     * Stop all asynchronous tasks.
     */
    stop(httpMethod: 'POST') {
        try {
            message = FilesTransfer.stop(security, log)
        } catch (CancelException e) {
            message = e.getMessage()
            status = e.getErrorCode()
        } catch (Throwable e) {
            log.error("An unexpected error occurred during POST stop", e)
            message = ExceptionUtils.getRootCauseMessage(e)
            status = 500
        }
    }
}
