package de.holisticon.bpm.sbr.dmn;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import de.holisticon.bpm.sbr.api.CustomerStatus;
import de.holisticon.bpm.sbr.dmn.api.CandidateResult;
import de.holisticon.bpm.sbr.dmn.api.SkillBasedRoutingService;
import org.camunda.bpm.dmn.engine.DmnDecision;
import org.camunda.bpm.dmn.engine.DmnDecisionOutput;
import org.camunda.bpm.dmn.engine.DmnDecisionResult;
import org.camunda.bpm.dmn.engine.DmnEngine;
import org.camunda.bpm.dmn.engine.impl.DmnEngineConfigurationImpl;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

@Stateless
@Remote(SkillBasedRoutingService.class)
public class SkillBasedRoutingServiceBean implements SkillBasedRoutingService {
  private final Logger logger = getLogger(this.getClass());

  private final static String DMN_RESOURCE = "findApprover.dmn";
  private DmnDecision decision;
  private DmnEngine dmnEngine;

  @PostConstruct
  public void loadDecision() {

    dmnEngine = new DmnEngineConfigurationImpl().buildEngine();
    final InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(DMN_RESOURCE);
    decision = dmnEngine.parseDecision(resourceAsStream);
    logger.info("created decision for " + DMN_RESOURCE);
  }

  @SuppressWarnings("unchecked")
  public <T> T evaluateSingleResult(final Map<String, Object> context, String resultName) {
    final DmnDecisionResult result = dmnEngine.evaluate(decision, context);
    if (result != null && !result.isEmpty()) {
      final DmnDecisionOutput output = result.get(0);
      return (T) output.get(resultName);
    }
    return null;
  }


  @Override
  public CandidateResult evaluate(final DelegateTask task) {
    final CandidateResult candidateResult = new CandidateResult();
    final Map<String, Object> context = new HashMap<String, Object>();

    String customerStatus = (String) task.getVariable("customerStatus");
    double approvalSum = (double) task.getVariable("aprovalSum");
    String

    context.put("sheet", new ApprovalSheet("",0.0, CustomerStatus.BRONZE));
    String candidateGroup = evaluateSingleResult(context, "group");
    if (candidateGroup != null) {
      candidateResult.getCandidateGroups().add(candidateGroup);
    }
    logger.info("Candidate group: {}", candidateGroup);
    return candidateResult;
  }
}

@@ -2020,16 +2020,45 @@
 ct%3E();%0A%0A
+    CustomerStatus c = null;%0A
     Stri
@@ -2118,24 +2118,115 @@
 erStatus%22);%0A
+    if (customerStatus != null) %7B%0A      c = CustomerStatus.valueOf(customerStatus);%0A    %7D%0A%0A
     double a
@@ -2289,16 +2289,74 @@
   String
+ customerCode = (String) task.getVariable(%22customerCode%22);
 %0A%0A    co
@@ -2396,37 +2396,35 @@
 eet(
-%22%22,0.0, CustomerStatus.BRONZE
+customerCode,approvalSum, c
 ));%0A

implement dmn bean