package digitalwallet;

import java.util.ArrayList;
import java.util.List;

public enum TemplateTypeEnum {
    BOARDING_PASS(1L, "boardingPass", "Boarding Pass", 1),
    COUPON(2L, "coupon", "Coupon", 1),
    EVENT_TICKET(3L, "eventTicket", "Event Ticket", 1),
    GENERIC(4L, "generic", "Generic", 1),
    STORE_CARD(5L, "storeCard", "Store Card", 1),
    LOYALTY_1(6L, "loyalty1", "Loyalty1", 2),
    LOYALTY_2(7L, "loyalty2", "Loyalty2", 2),
    OFFER_1(8L, "offer1", "Offer1", 2),
    GENERIC_1(9L, "generic1", "Generic1", 2),
    GENERIC_2(10L, "generic2", "Generic2", 2),
    GENERIC_3(11L, "generic3", "Generic3", 2);

    private final Long code;
    private final String jsonName;
    private final String displayName;
    private final int vendorId;

    private static List<TemplateTypeEnum> appleTemplateTypes;
    private static List<TemplateTypeEnum> googleTemplateTypes;

    static {
        appleTemplateTypes = new ArrayList<TemplateTypeEnum>();
        for (TemplateTypeEnum current : TemplateTypeEnum.values()) {
            if (current.getVendorId() == 1) {
                appleTemplateTypes.add(current);
            }
        }

        googleTemplateTypes = new ArrayList<TemplateTypeEnum>();
        for (TemplateTypeEnum current : TemplateTypeEnum.values()) {
            if (current.getVendorId() == 2) {
                googleTemplateTypes.add(current);
            }
        }
    }

    private TemplateTypeEnum(Long code, String jsonName, String displayName, int vendorId) {
        this.vendorId = vendorId;
        this.code = code;
        this.jsonName = jsonName;
        this.displayName = displayName;
    }

    public Long getCode() {
        return code;
    }

    public String getJsonName() {
        return jsonName;
    }

    public String getDisplayName() {
        return displayName;
    }


    public int getVendorId() {
        return vendorId;
    }

    public static String jsonKeyName(Long code) {
        for (TemplateTypeEnum current : TemplateTypeEnum.values()) {
            if (current.code.equals(code)) {
                return current.jsonName;
            }
        }

        throw new RuntimeException("invalid template type " + code);
    }

    public static String jsonKeyName(Long code, int vendorId) {
        for (TemplateTypeEnum current : TemplateTypeEnum.values()) {
            if (current.code.equals(code) && (vendorId == current.vendorId)) {
                return current.jsonName;
            }
        }

        switch (vendorId) {
            case 2:
                return LOYALTY_1.jsonName;
            case 1:
            default:
                return GENERIC.jsonName;
        }
    }

    public static String displayName(Long code) {
        for (TemplateTypeEnum current : TemplateTypeEnum.values()) {
            if (current.code.equals(code)) {
                return current.displayName;
            }
        }

        throw new RuntimeException("invalid template type " + code);
    }

    public static TemplateTypeEnum fromJsonKeyName(String json) {
        for (TemplateTypeEnum current : TemplateTypeEnum.values()) {
            if (current.jsonName.equals(json) || current.displayName.equals(json)) {
                return current;
            }
        }

        throw new RuntimeException("invalid template type " + json);
    }

    public static TemplateTypeEnum fromCode(Long code) {
        for (TemplateTypeEnum current : TemplateTypeEnum.values()) {
            if (current.code.equals(code)) {
                return current;
            }
        }

        throw new RuntimeException("invalid template code " + code);
    }

    public static TemplateTypeEnum fromCode(Long code, int vendorId) {
        for (TemplateTypeEnum current : TemplateTypeEnum.values()) {
            if (current.code.equals(code) && (current.vendorId == vendorId)) {
                return current;
            }
        }

        switch (vendorId) {
            case 2:
                return LOYALTY_1;
            case 1:
            default:
                return GENERIC;
        }
    }

    public static TemplateTypeEnum getRandomAppleTemplateType() {
        List<TemplateTypeEnum> types = getAppleTemplateTypes();
        int ndx = (int)(Math.random() * types.size());
        return types.get(ndx);

    }

    public static TemplateTypeEnum getRandomGoogleTemplateType() {
        List<TemplateTypeEnum> types = getGoogleTemplateTypes();
        int ndx = (int)(Math.random() * types.size());
        return types.get(ndx);
    }

    private static List<TemplateTypeEnum> getGoogleTemplateTypes() {
        return googleTemplateTypes;

    }

    private static List<TemplateTypeEnum> getAppleTemplateTypes() {
        return appleTemplateTypes;
    }
}

@@ -62,20 +62,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Apubli
@@ -931,85 +931,371 @@
 es;%0A
-%0A    static %7B%0A        appleTemplateTypes = new ArrayList%3CTemplateTypeEnum%3E();
+    private static Map%3CLong, String%3E jsonNameLookup;%0A    private static Map%3CLong, String%3E displayNameLookup;%0A%0A    static %7B%0A        appleTemplateTypes = new ArrayList%3CTemplateTypeEnum%3E();%0A        googleTemplateTypes = new ArrayList%3CTemplateTypeEnum%3E();%0A        jsonNameLookup = new HashMap%3CLong, String%3E();%0A        displayNameLookup = new HashMap%3CLong, String%3E();%0A
 %0A   
@@ -1460,32 +1460,71 @@
 );%0A            %7D
+ else if (current.getVendorId() == 2) %7B
 %0A        %7D%0A%0A    
@@ -1508,35 +1508,32 @@
 == 2) %7B%0A        
-%7D%0A%0A
         googleTe
@@ -1547,224 +1547,191 @@
 ypes
- = new ArrayList%3CTemplateTypeEnum%3E();%0A        for (TemplateTypeEnum current : TemplateTypeEnum.values()) %7B%0A            if (current.getVendorId() == 2) %7B%0A                googleTemplateTypes.add(current);%0A            %7D
+.add(current);%0A            %7D%0A%0A            jsonNameLookup.put(current.getCode(), current.getJsonName());%0A            displayNameLookup.put(current.getCode(), current.getDisplayName());
 %0A   
@@ -2298,104 +2298,38 @@
     
-for (TemplateTypeEnum current : TemplateTypeEnum.values()) %7B%0A            if (current.code.equals
+if (jsonNameLookup.containsKey
 (cod
@@ -2350,50 +2350,40 @@
     
-    
 return 
-current.jsonName;%0A            %7D
+jsonNameLookup.get(code);
 %0A   
@@ -2996,104 +2996,41 @@
     
-for (TemplateTypeEnum current : TemplateTypeEnum.values()) %7B%0A            if (current.code.equals
+if (displayNameLookup.containsKey
 (cod
@@ -3047,35 +3047,23 @@
         
-    
 return 
-current.
 displayN
@@ -3061,39 +3061,41 @@
  displayName
-;%0A            %7D
+Lookup.get(code);
 %0A        %7D%0A%0A

Clean up enum stuff a little