package com.filestack.model.transform.base;

import com.filestack.model.FilestackClient;
import com.filestack.model.FileLink;
import com.filestack.model.Security;
import okhttp3.HttpUrl;
import com.filestack.util.FilestackService;
import com.filestack.util.Networking;

import java.util.ArrayList;

/**
 * Base class for file transformations and conversions.
 */
public class Transform {
    String apiKey;
    String source;
    Security security;

    ArrayList<TransformTask> tasks;

    FilestackService.Process processService;

    Transform(FilestackClient fsClient, String source) {
        this(fsClient, source, null);
    }

    Transform(FileLink fileLink) {
        this(null, null, fileLink);
    }

    Transform(FilestackClient fsClient, String source, FileLink fileLink) {
        if (fsClient != null) {
            this.apiKey = fsClient.getApiKey();
            this.source = source;
        } else {
            this.source = fileLink.getHandle();
        }

        this.tasks = new ArrayList<>();
        this.processService = Networking.getProcessService();

        Security security = fsClient != null ? fsClient.getSecurity() : fileLink.getSecurity();
        this.security = security;
        if (security != null) {
            TransformTask securityTask = new TransformTask("security");
            securityTask.addOption("policy", security.getPolicy());
            securityTask.addOption("signature", security.getSignature());
            this.tasks.add(securityTask);
        }
    }

    /**
     * Build tasks into single string to insert into request.
     */
    protected String getTasksString() {
        if (tasks.size() == 0)
            return "";

        StringBuilder stringBuilder = new StringBuilder();
        for (TransformTask task : tasks)
            stringBuilder.append(task.toString()).append('/');
        stringBuilder.deleteCharAt(stringBuilder.length()-1);
        return stringBuilder.toString();
    }

    public String url() {
        String tasksString = getTasksString();
        HttpUrl httpUrl;

        if (apiKey != null)
            httpUrl = processService.getExternal(apiKey, tasksString, source).request().url();
        else
            httpUrl = processService.get(tasksString, source).request().url();

        // When forming the request we add a / between tasks, then add that entire string as a path variable
        // Because it's added as a single path variable, the / is URL encoded
        // That's a little confusing so we're replacing "%2F" with "/" for a more expected URL
        return httpUrl.toString().replace("%2F", "/");
    }
}

@@ -69,27 +69,20 @@
 del.File
-stackClient
+Link
 ;%0Aimport
@@ -98,36 +98,43 @@
 stack.model.File
-Link
+stackClient
 ;%0Aimport com.fil
@@ -160,32 +160,8 @@
 ty;%0A
-import okhttp3.HttpUrl;%0A
 impo
@@ -238,17 +238,16 @@
 orking;%0A
-%0A
 import j
@@ -270,14 +270,35 @@
 st;%0A
+import okhttp3.HttpUrl;%0A
 %0A/*
-*%0A 
 * Ba
@@ -347,17 +347,16 @@
 ersions.
-%0A
  */%0Apubl
@@ -1516,21 +1516,15 @@
  /**
-%0A     * Build
+ Format
  tas
@@ -1568,21 +1568,16 @@
 request.
-%0A    
  */%0A    
@@ -1642,16 +1642,18 @@
 () == 0)
+ %7B
 %0A       
@@ -1667,16 +1667,26 @@
 turn %22%22;
+%0A        %7D
 %0A%0A      
@@ -1778,16 +1778,18 @@
 : tasks)
+ %7B
 %0A       
@@ -1844,16 +1844,26 @@
 d('/');%0A
+        %7D%0A
         
@@ -1915,9 +1915,11 @@
 th()
--
+ - 
 1);%0A
@@ -1962,24 +1962,205 @@
 g();%0A    %7D%0A%0A
+    /**%0A     * Generates a URL of the transformation.%0A     * Includes the related %7B@link FileLink FileLink's%7D policy and signature.%0A     *%0A     * @return transformation URL%0A     */%0A
     public S
@@ -2273,16 +2273,18 @@
 != null)
+ %7B
 %0A       
@@ -2378,21 +2378,25 @@
 %0A       
+ %7D
  else
+ %7B
 %0A       
@@ -2466,16 +2466,26 @@
 ).url();
+%0A        %7D
 %0A%0A      
@@ -2494,20 +2494,21 @@
 // When 
-form
+build
 ing the 
@@ -2543,79 +2543,53 @@
 asks
-, then add that entire string as a path variable%0A        // Because it'
+%0A        // Because that entire task string i
 s ad

Fix linter warnings for Transform