/*
 * Copyright (c) 2012-2018 University Corporation for Atmospheric Research/Unidata
 * See LICENSE for license information.
 */

package edu.ucar.unidata.rosetta.util;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import edu.ucar.unidata.rosetta.domain.RosettaGlobalAttribute;
import edu.ucar.unidata.rosetta.domain.Template;
import edu.ucar.unidata.rosetta.domain.VariableInfo;
import ucar.nc2.Attribute;

public class TemplateUtils {

    /**
     * Extract the global attributes from a template
     *
     * Attribute are retuned in a map, where group is the key, and the value is a list of
     * netCDF-Java Attribute objects.
     *
     * @param template The template object which holds RosettaGlobalAttributes
     * @return a map with groupName keys corresponding to a list of attributes
     */
    public static Map<String, ArrayList<Attribute>> getGlobalAttrsMap(Template template) {
        HashMap<String, ArrayList<Attribute>> globalAttrsMap = new HashMap<>();

        for (RosettaGlobalAttribute globalAttr : template.getGlobalMetadata()) {
            String groupName = globalAttr.getGroup();
            ArrayList<Attribute> groupAttrs = globalAttrsMap.getOrDefault(groupName, new ArrayList<>());
            Attribute attr = RosettaGlobalAttributeUtils.getAttributeFromGlobalAttr(globalAttr);
            groupAttrs.add(attr);
            globalAttrsMap.put(groupName, groupAttrs);

        }

        return globalAttrsMap;
    }

    /**
     * Find a unique variable name given the variable names present in a template
     *
     * The names encoded in the Template's VariableInfo objects are not guaranteed to be unique.
     * This method checks a name against those names stored in the VariableInfo objects of a
     * template, and returns a unique value if the name is already in use. A unique name is made by
     * appending an integer value to the input name until a unique version is found.
     *
     * @param name     the name to make unique
     * @param template template containing a series of VarInfo objects, which may already contain
     *                 name.
     * @return a unique version of the input name
     */
    public static String findUniqueName(String name, Template template) {
        boolean uniqueName = true;
        int pass = 0;
        while (uniqueName) {
            for (VariableInfo vi : template.getVariableInfoList()) {
                if (vi.getName().equals(name)) {
                    pass = pass + 1;
                    name = name + String.valueOf(pass);
                    uniqueName = false;
                }
            }
        }
        return name;
    }


    /**
     * Copy a template object
     *
     * Make a copy of a Template object. This is done by serializing the object to json, and then
     * returning a new object based on the serialized version.
     *
     * @param in template to copy
     * @return copy of the input template
     */
    public static Template copy(Template in) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(in);
        return mapper.readValue(json, Template.class);
    }

}

@@ -2361,77 +2361,177 @@
 ean 
-uniqueName = true;%0A        int pass = 0;%0A        while (uniqueName) %7B
+keepLooking = true; // assume it is found%0A        int pass = 0;%0A        while (keepLooking) %7B%0A            boolean uniqueNameFound = true; // assume we have a unique name
 %0A   
@@ -2734,24 +2734,77 @@
 ueOf(pass);%0A
+                    // well, heck, not a unique name%0A
             
@@ -2821,16 +2821,21 @@
 iqueName
+Found
  = false
@@ -2859,32 +2859,119 @@
  %7D%0A            %7D
+%0A%0A            if (uniqueNameFound) %7B%0A                keepLooking = false;%0A            %7D
 %0A        %7D%0A     

update unqiue name finding code