package org.commcare.session;

import org.commcare.suite.model.DisplayUnit;
import org.commcare.suite.model.RemoteQueryDatum;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.xpath.expr.XPathExpression;
import org.javarosa.xpath.expr.XPathFuncExpr;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Manager for remote query datums; get/answer user prompts and build
 * resulting query url.
 *
 * @author Phillip Mates (pmates@dimagi.com)
 */
public class RemoteQuerySessionManager {
    private final RemoteQueryDatum queryDatum;
    private final Hashtable<String, String> userAnswers = new Hashtable<String, String>();
    private final EvaluationContext evaluationContext;

    public RemoteQuerySessionManager(RemoteQueryDatum queryDatum, EvaluationContext evaluationContext) {
        this.queryDatum = queryDatum;
        this.evaluationContext = evaluationContext;
    }

    public Hashtable<String, DisplayUnit> getNeededUserInputDisplays() {
        return queryDatum.getUserQueryPrompts();
    }

    public Hashtable<String, String> getUserAnswers() {
        return userAnswers;
    }

    public void clearAnswers() {
        userAnswers.clear();
    }

    public void answerUserPrompt(String key, String answer) {
        userAnswers.put(key, answer);
    }

    public String getBaseUrl() {
        return queryDatum.getValue();
    }

    public String getStorageInstanceName() {
        return queryDatum.getDataId();
    }

    public Hashtable<String, String> getRawQueryParams() {
        Hashtable<String, String> params = new Hashtable<String, String>();
        Hashtable<String, XPathExpression> hiddenQueryValues = queryDatum.getHiddenQueryValues();
        for (Enumeration e = hiddenQueryValues.keys(); e.hasMoreElements(); ) {
            String key = (String)e.nextElement();
            String evaluatedExpr = calculateHidden(hiddenQueryValues.get(key), evaluationContext);
            params.put(key, evaluatedExpr);
        }
        for (Enumeration e = userAnswers.keys(); e.hasMoreElements(); ) {
            String key = (String)e.nextElement();
            params.put(key, userAnswers.get(key));
        }
        return params;
    }

    public static String calculateHidden(XPathExpression expr,
                                         EvaluationContext evaluationContext) {
        return XPathFuncExpr.toString(expr.eval(evaluationContext));
    }

    public boolean areAllUserPromptsAnswered() {
        for (Enumeration e = queryDatum.getUserQueryPrompts().keys(); e.hasMoreElements(); ) {
            String key = (String)e.nextElement();
            if (!userAnswers.containsKey(key)) {
                return false;
            }
        }
        return true;
    }
}

@@ -567,24 +567,79 @@
 queryDatum;%0A
+    private final EvaluationContext evaluationContext;%0A
     private 
@@ -683,16 +683,28 @@
 nswers =
+%0A           
  new Has
@@ -732,63 +732,8 @@
 %3E();
-%0A    private final EvaluationContext evaluationContext;
 %0A%0A  
@@ -795,16 +795,53 @@
 ryDatum,
+%0A                                    
  Evaluat
@@ -2486,332 +2486,7 @@
    %7D
-%0A%0A    public boolean areAllUserPromptsAnswered() %7B%0A        for (Enumeration e = queryDatum.getUserQueryPrompts().keys(); e.hasMoreElements(); ) %7B%0A            String key = (String)e.nextElement();%0A            if (!userAnswers.containsKey(key)) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D
 %0A%7D%0A

Remove unused method