package org.agmip.dome;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.agmip.ace.AcePathfinder;
import org.agmip.ace.util.AcePathfinderUtil;
import org.agmip.util.MapUtil;
import org.agmip.common.Functions;

// Temporary imports to integrate @MengZhang codebase from another project
import org.agmip.functions.ExperimentHelper;
import org.agmip.functions.SoilHelper;


public class Generate {
    public static final Logger log = LoggerFactory.getLogger(Generate.class);

    public static ArrayList<HashMap<String, String>> run(HashMap m, String[] args, ArrayList<HashMap<String, String>> modifiers) {
        ArrayList<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
        String fun = args[0].toUpperCase();
        String[] newArgs =  Arrays.copyOfRange(args, 1, args.length);

        if (modifiers.isEmpty()) {
            HashMap<String, ArrayList<String>> genResults = execute(m, fun, newArgs);
            for (Map.Entry<String, ArrayList<String>> entry : genResults.entrySet()) {
                String key = entry.getKey();
                int i = 0;
                for (String value : entry.getValue()) {
                    HashMap<String, String> output = new HashMap<String, String>();
                    output.put(key, value);
                    results.add(output);
                }
            }
        } else {
            // Safety to not lose what already exists.
            log.error("Multiple generators are unsupported in this version.");
            results = modifiers;
        }

        return results;
    }

    protected static void applyGeneratedRules(HashMap m, HashMap<String, String> rules, String id) {
        // Just apply each entry as a REPLACE ASSUME
        // Clear out existing events, since they are invalid!
        String exname = MapUtil.getValueOr(m, "exname", "");
        if (exname.contains("__")) {
            exname = exname.substring(0,exname.indexOf("__"));
        }
        String[] exvalue = {exname+"__"+id};
        Assume.run(m, "exname", exvalue, true);
        for (Map.Entry<String, String> rule : rules.entrySet()) {
            String[] value = {rule.getValue()};
            Assume.run(m, rule.getKey(), value, true);
        }
    }

    private static HashMap<String, ArrayList<String>> execute(HashMap m, String fun, String[] args) {
        if (fun.equals("AUTO_PDATE()")) {
            if (args.length < 4) {
                log.error("Not enough arguments for {}", fun);
                return new HashMap<String, ArrayList<String>>();
            }
            return ExperimentHelper.getAutoPlantingDate(m, args[0], args[1], args[2], args[3]);
        } else {
            log.error("DOME Function {} unsupported.", fun);
            return new HashMap<String, ArrayList<String>>();
        }
    }
}

@@ -2693,22 +2693,60 @@
         
-return
+HashMap%3CString, ArrayList%3CString%3E%3E results =
  Experim
@@ -2815,16 +2815,1608 @@
 gs%5B3%5D);%0A
+            ArrayList%3CString%3E pdates = MapUtil.getObjectOr(results, %22pdate%22, new ArrayList());%0A            if (pdates.isEmpty()) %7B%0A                ArrayList%3CHashMap%3CString, String%3E%3E events = MapUtil.getBucket(m, %22management%22).getDataList();%0A                int idx = 0;%0A                HashMap template = null;%0A                for (String pdate : pdates) %7B%0A                    // Locate the planting event%0A                    while(idx %3C events.size()) %7B%0A                        String eventType = MapUtil.getValueOr(events.get(idx), %22event%22, %22%22);%0A                        if (eventType.equals(%22planting%22)) %7B%0A                            if (template == null) %7B%0A                                template = new HashMap();%0A                                template.putAll(events.get(idx));%0A                            %7D%0A                            break;%0A                        %7D%0A                        idx++;%0A                    %7D%0A                    // Set default template if all no planting event available in the array%0A                    if (template == null) %7B%0A                        template = new HashMap();%0A                    %7D%0A                    // Set calculated padte into array%0A                    if (idx %3E= events.size()) %7B%0A                        HashMap tmp = new HashMap();%0A                        tmp.putAll(template);%0A                        tmp.put(%22date%22, pdate);%0A                        events.add(tmp);%0A                    %7D else %7B%0A                        events.get(idx).put(%22date%22, pdate);%0A                    %7D%0A                %7D%0A            %7D%0A            return m;%0A
         

Add handling for the result of auto_planting function call