package io.rapidpro.surveyor;

import org.json.JSONException;
import org.threeten.bp.ZoneId;

import java.util.List;

import io.rapidpro.expressions.dates.DateStyle;
import io.rapidpro.flows.definition.Flow;
import io.rapidpro.flows.runner.Contact;
import io.rapidpro.flows.runner.Field;
import io.rapidpro.flows.runner.FlowRunException;
import io.rapidpro.flows.runner.Org;
import io.rapidpro.flows.runner.RunState;
import io.rapidpro.flows.runner.Runner;
import io.rapidpro.surveyor.data.DBFlow;
import io.rapidpro.surveyor.data.DBOrg;


/**
 * This is bridge from our DB objects and the flow engine objects
 */
public class RunnerUtil {


    public static Flow createFlow(DBFlow flow) throws JSONException {
        return Flow.fromJson(flow.getDefinition());
    }

    public static DateStyle getDateStyle (DBOrg org){
        DateStyle dateStyle = DateStyle.DAY_FIRST;
        if (org.getDateStyle().equals("month_first")) {
            dateStyle = DateStyle.DAY_FIRST.MONTH_FIRST;
        }
        return dateStyle;
    }

    public static Org createOrg(DBOrg org) {
        return new Org(org.getCountry(), org.getPrimaryLanguage(),
                       ZoneId.of(org.getTimezone()), getDateStyle(org),
                       org.isAnonymous());
    }

    public static Field.ValueType getValueType(String valueType) {
        if (valueType.equals("D")) {
            return Field.ValueType.DATETIME;
        } else if (valueType.equals("I")) {
            return Field.ValueType.DISTRICT;
        } else if (valueType.equals("S")) {
            return Field.ValueType.STATE;
        } else if (valueType.equals("N")) {
            return Field.ValueType.DECIMAL;
        } else {
            return Field.ValueType.TEXT;
        }
    }

    public static RunState getRunState(Runner runner, DBFlow flow, List<Field> fields) throws FlowRunException, JSONException {
        return runner.start(createOrg(flow.getOrg()), fields, new Contact(), flow.getUuid());
    }
}

@@ -28,39 +28,8 @@
 r;%0A%0A
-import org.json.JSONException;%0A
 impo
@@ -132,50 +132,8 @@
 le;%0A
-import io.rapidpro.flows.definition.Flow;%0A
 impo
@@ -561,17 +561,16 @@
 Util %7B%0A%0A
-%0A
     publ
@@ -583,35 +583,75 @@
 tic 
-Flow createFlow(DBFlow flow
+RunState getRunState(Runner runner, DBFlow flow, List%3CField%3E fields
 ) th
@@ -655,20 +655,23 @@
  throws 
-JSON
+FlowRun
 Exceptio
@@ -693,40 +693,82 @@
 urn 
-Flow.fromJson(flow.getDefinition
+runner.start(createOrg(flow.getOrg()), fields, new Contact(), flow.getUuid
 ());
@@ -772,37 +772,38 @@
 ));%0A    %7D%0A%0A    p
-ublic
+rivate
  static DateStyl
@@ -816,17 +816,16 @@
 ateStyle
- 
 (DBOrg o
@@ -827,16 +827,17 @@
 Org org)
+ 
 %7B%0A      
@@ -842,63 +842,15 @@
     
-DateStyle dateStyle = DateStyle.DAY_FIRST;%0A        if (
+return 
 org.
@@ -889,35 +889,10 @@
 st%22)
-) %7B%0A            dateStyle =
+ ?
  Dat
@@ -902,18 +902,8 @@
 yle.
-DAY_FIRST.
 MONT
@@ -913,44 +913,30 @@
 IRST
-;%0A        %7D%0A        return dateStyle
+ : DateStyle.DAY_FIRST
 ;%0A  
@@ -937,37 +937,38 @@
 ST;%0A    %7D%0A%0A    p
-ublic
+rivate
  static Org crea
@@ -1009,16 +1009,33 @@
 new Org(
+%0A                
 org.getC
@@ -1043,16 +1043,32 @@
 untry(),
+%0A               
  org.get
@@ -1102,23 +1102,16 @@
         
-    
-   
 ZoneId.o
@@ -1135,784 +1135,86 @@
 ()),
- getDateStyle(org),%0A                       org.isAnonymous());%0A    %7D%0A%0A    public static Field.ValueType getValueType(String valueType) %7B%0A        if (valueType.equals(%22D%22)) %7B%0A            return Field.ValueType.DATETIME;%0A        %7D else if (valueType.equals(%22I%22)) %7B%0A            return Field.ValueType.DISTRICT;%0A        %7D else if (valueType.equals(%22S%22)) %7B%0A            return Field.ValueType.STATE;%0A        %7D else if (valueType.equals(%22N%22)) %7B%0A            return Field.ValueType.DECIMAL;%0A        %7D else %7B%0A            return Field.ValueType.TEXT;%0A        %7D%0A    %7D%0A%0A    public static RunState getRunState(Runner runner, DBFlow flow, List%3CField%3E fields) throws FlowRunException, JSONException %7B%0A        return runner.start(createOrg(flow.getOrg()), fields, new Contact(), flow.getUuid()
+%0A                getDateStyle(org),%0A                org.isAnonymous()%0A        
 );%0A 

Remove unused methods