package io.cloudslang.content.hashicorp.terraform.actions.utils;

import com.hp.oo.sdk.content.annotations.Action;
import com.hp.oo.sdk.content.annotations.Output;
import com.hp.oo.sdk.content.annotations.Param;
import com.hp.oo.sdk.content.annotations.Response;
import com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType;
import com.hp.oo.sdk.content.plugin.StepSerializableSessionObject;
import io.cloudslang.content.hashicorp.terraform.services.CounterImpl;
import io.cloudslang.content.hashicorp.terraform.services.models.CounterImplException;

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

import static com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType.ERROR;
import static com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType.RESOLVED;
import static io.cloudslang.content.hashicorp.terraform.utils.Constants.CounterConstants.*;
import static io.cloudslang.content.hashicorp.terraform.utils.Descriptions.Common.FAILURE_DESC;
import static io.cloudslang.content.hashicorp.terraform.utils.Descriptions.Common.SUCCESS_DESC;
import static io.cloudslang.content.hashicorp.terraform.utils.Descriptions.Counter.*;
import static org.apache.commons.lang3.StringUtils.*;


public class Counter {

    public final String RESULT_DBG_INDEX = "index";
    public final StepSerializableSessionObject sessionObject=new StepSerializableSessionObject("name");

    @Action(name = COUNTER_OPERATION_NAME, description = COUNTER_DESC,
            outputs = {
                    @Output(value = RESULT_STRING, description = RESULT_STRING_DESC),
                    @Output(value = RESULT, description = RESULT_DESC)},
            responses = {
                    @Response(text = HASMORE, field = RESULT, value = HASMORE, matchType = MatchType.COMPARE_EQUAL, responseType = RESOLVED, description = SUCCESS_DESC),
                    @Response(text = NOMORE, field = RESULT, value = NOMORE, matchType = MatchType.COMPARE_EQUAL,responseType = RESOLVED, description = SUCCESS_DESC),
                    @Response(text = FAILURE, field = RESULT, value = FAILURE, matchType = MatchType.COMPARE_EQUAL, isDefault = true, isOnFail = true,responseType = ERROR, description = FAILURE_DESC)})
    public Map<String, String> execute(@Param(value = FROM , required = true, description = FROM_DESC) String from,
                                       @Param(value = TO, required = true, description = TO_DESC) String to,
                                       @Param(value = INCREMENT_BY, description = INCREMENT_BY_DESC) String incrementBy,
                                       @Param(value = RESET, description = RESET_DESC) String reset) {

        from = defaultIfEmpty(from, EMPTY);
        to = defaultIfEmpty(to, EMPTY);
        reset = defaultIfEmpty(reset, EMPTY);
        incrementBy = defaultIfEmpty(incrementBy, EMPTY);

        CounterImpl counter = new CounterImpl();
        Map<String, String> returnResult = new HashMap<String, String>();
        returnResult.put(RESULT, FAILURE_MESSAGE);

        try {
            counter.init(to, from, incrementBy, Boolean.valueOf(reset), sessionObject);
            if (counter.hasNext()) {
                returnResult.put(RESULT_DBG_INDEX, Long.toString(counter.getIndex()));
                returnResult.put(RESULT_STRING, counter.getNext(sessionObject));
                returnResult.put(RESULT, HASMORE);
            } else {
                counter.setStepSessionEnd(sessionObject);
                returnResult.put(RESULT_STRING, "");
                returnResult.put(RESULT, NOMORE);
            }
        } catch (CounterImplException e) {
            returnResult.put(EXCEPTION, e.toString());
        }
        return returnResult;
    }
    }

@@ -1230,32 +1230,39 @@
 r %7B%0A%0A    public 
+static 
 final String RES
@@ -1290,20 +1290,114 @@
 x%22;%0A    
-publ
+final static Random rand = new Random();%0A    static int number=rand.nextInt(1000);%0A    public stat
 ic final
@@ -1481,16 +1481,24 @@
 t(%22name%22
+ +number
 );%0A%0A    

maintain session object as unique