/*
 * Copyright 2012-2019 MarkLogic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.marklogic.hub.step.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.marklogic.hub.step.Step;
import com.marklogic.hub.util.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class StepImpl implements Step {
    private String language = "zxx";
    private String name;
    private StepType type;
    private int version;
    private Map<String, Object> options;
    private JsonNode customHook;
    private String modulePath;
    private String identifier;
    private int retryLimit;
    private int batchSize;
    private int threadCount;
    private String sourceDB;
    private String destDB;

    public StepImpl(String name, StepType type) {
        this.name = name;
        this.type = type;
        this.version = 1;
        JSONObject jsonObject = new JSONObject();
        options = new HashMap<>();
        jsonObject.putArray("collections", name);
        if (type == StepType.INGEST) {
            jsonObject.put("outputFormat", "json");
        } else if (type == StepType.MAPPING || type == StepType.CUSTOM) {
            this.identifier = "cts.uris(null, null, cts.collectionQuery('default-ingest'))";
        }
        this.modulePath = "/path/to/your/step/module/main.sjs";
        this.customHook = new JSONObject().jsonNode();
        this.retryLimit = 0;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage() {
        this.language = "zxx";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public StepType getType() {
        return type;
    }

    public void setType(StepType type) {
        this.type = type;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public Map<String, Object> getOptions() {
        return options;
    }

    public void setOptions(Map<String, Object> options) {
        this.options = options;
    }

    public String getModulePath() {
        return modulePath;
    }

    public void setModulePath(String path) {
        this.modulePath = path;
    }

    public JsonNode getCustomHook() {
        return customHook;
    }

    public void setCustomHook(JsonNode hookObj) {
        this.customHook = hookObj;
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public int getRetryLimit() {
        return retryLimit;
    }

    public void setRetryLimit(int retryLimit) {
        this.retryLimit = retryLimit;
    }

    public int getBatchSize() {
        return batchSize;
    }

    public void setBatchSize(int batchSize) {
        this.batchSize = batchSize;
    }

    public int getThreadCount() {
        return threadCount;
    }

    public void setThreadCount(int threadCount) {
        this.threadCount = threadCount;
    }

    public String getSourceDB() {
        return sourceDB;
    }

    public void setSourceDB(String sourceDB) {
        this.sourceDB = sourceDB;
    }

    public String getDestDB() {
        return destDB;
    }

    public void setDestDB(String destDB) {
        this.destDB = destDB;
    }

    @Override
    public void deserialize(JsonNode json) {
        JSONObject jsonObject = new JSONObject(json);
        setName(jsonObject.getString("name"));
        setType(StepType.getStepType(jsonObject.getString("type")));
        setVersion(jsonObject.getInt("version"));
        setOptions(jsonObject.getMap("options"));
        setCustomHook(jsonObject.getNode("customHook"));
        setModulePath(jsonObject.getString("modulePath"));
        setIdentifier(jsonObject.getString("identifier"));
        setRetryLimit(jsonObject.getInt("retryLimit"));
        setBatchSize(jsonObject.getInt("batchSize"));
        setThreadCount(jsonObject.getInt("threadCount"));
        setSourceDB(jsonObject.getString("sourceDB"));
        setDestDB(jsonObject.getString("destDB"));
    }
}

@@ -790,23 +790,74 @@
 va.util.
-HashMap
+ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List
 ;%0Aimport
@@ -1406,37 +1406,32 @@
 = type;%0A        
-this.
 version = 1;%0A   
@@ -1427,24 +1427,25 @@
 on = 1;%0A
+%0A
         
 JSONObje
@@ -1440,112 +1440,145 @@
     
-JSONObject jsonObject = new JSONObject();%0A        options = new HashMap%3C%3E();%0A        jsonObject.putArray
+options = new HashMap%3C%3E();%0A        List%3CString%3E collectionName = new ArrayList%3C%3E();%0A        collectionName.add(name);%0A        options.put
 (%22co
@@ -1589,23 +1589,34 @@
 tions%22, 
-n
+collectionN
 ame);%0A
+%0A
         
@@ -1658,26 +1658,23 @@
         
-jsonObject
+options
 .put(%22ou
@@ -1773,37 +1773,32 @@
 ) %7B%0A            
-this.
 identifier = %22ct
@@ -1867,37 +1867,32 @@
       %7D%0A        
-this.
 modulePath = %22/p
@@ -1926,37 +1926,32 @@
 n.sjs%22;%0A        
-this.
 customHook = new
@@ -1976,37 +1976,32 @@
 Node();%0A        
-this.
 retryLimit = 0;%0A

Add options to the options object