package com.transloadit.sdk.response;

import com.transloadit.sdk.exceptions.LocalOperationException;
import org.json.JSONArray;

/**
 * An AssemblyApi tailored Http Response
 */
public class AssemblyResponse extends Response {
    public AssemblyResponse(okhttp3.Response response, boolean usesTus) throws LocalOperationException {
        super(response);
    }

    public AssemblyResponse(okhttp3.Response response) throws LocalOperationException {
        this(response, false);
    }

    /**
     *
     * @return assembly id
     */
    public String getId() {
        return this.json().getString("assembly_id");
    }

    /**
     *
     * @return assembly url
     */
    public String getUrl() {
        return this.json().getString( "assembly_url");
    }

    /**
     *
     * @return assembly ssl url
     */
    public String getSslUrl() {
        return this.json().getString("assembly_ssl_url");
    }

    /**
     * returns the assembly result of a particular step.
     *
     * @param stepName the name of the step.
     * @return {@link JSONArray} the assembly result of the specified step.
     */
    public JSONArray getStepResult(String stepName) {
        return json().getJSONObject("results").getJSONArray(stepName);
    }

    /**
     *
     * @return {@link Boolean} indicating the assembly has completed.
     */
    public Boolean isCompleted() {
        return json().getString("ok").equals("ASSEMBLY_COMPLETED");
    }

    /**
     *
     * @return {@link Boolean} indicating the assembly has aborted.
     */
    public Boolean isAborted() {
        return json().getString("ok").equals("REQUEST_ABORTED");
    }

    /**
     *
     * @return {@link Boolean} indicating the assembly has canceled.
     */
    public Boolean isCanceled() {
        return json().getString("ok").equals("ASSEMBLY_CANCELED");
    }

    /**
     *
     * @return {@link Boolean} indicating the assembly is still executing.
     */
    public Boolean isExecuting() {
        return json().getString("ok").equals("ASSEMBLY_EXECUTING");
    }

    /**
     *
     * @return {@link Boolean} indicating the assembly is uploading.
     */
    public Boolean isUploading() {
        return json().getString("ok").equals("ASSEMBLY_UPLOADING");
    }

    /**
     *
     * @return {@link Boolean} indicating if the assembly returned an error
     */
    public Boolean hasError() {
        return json().has("error");
    }

    /**
     *
     * @return {@link Boolean} indicating the assembly has stopped executing.
     */
    public Boolean isFinished() {
        return isAborted() || isCanceled() || isCompleted() || hasError();
    }
}

@@ -1383,32 +1383,52 @@
 %7B%0A        return
+ json().has(%22ok%22) &&
  json().getStrin
@@ -1461,24 +1461,24 @@
 OMPLETED%22);%0A
-
     %7D%0A%0A    /
@@ -1602,32 +1602,52 @@
 %7B%0A        return
+ json().has(%22ok%22) &&
  json().getStrin
@@ -1820,32 +1820,52 @@
 %7B%0A        return
+ json().has(%22ok%22) &&
  json().getStrin
@@ -2047,32 +2047,52 @@
 %7B%0A        return
+ json().has(%22ok%22) &&
  json().getStrin
@@ -2255,32 +2255,32 @@
 isUploading() %7B%0A
-
         return j
@@ -2269,32 +2269,52 @@
 %7B%0A        return
+ json().has(%22ok%22) &&
  json().getStrin

make assambly status checks safer