package io.wovn.wovnjava;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Set;

import net.arnx.jsonic.JSON;

class Values {
    private LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,ArrayList<LinkedHashMap<String,String>>>>>
        values;

    Values(String json) {
        values = JSON.decode(json);
    }

    ArrayList<String> getLangs() {
        Set<String> set = new HashSet<String>();

        // Get languages from text data.
        LinkedHashMap<String,LinkedHashMap<String,ArrayList<LinkedHashMap<String,String>>>> textVals
            = this.values.get("text_vals");

        if (textVals != null) {
            for (LinkedHashMap<String, ArrayList<LinkedHashMap<String,String>>> v : textVals.values()) {
                set.addAll(v.keySet());
            }
        }

        // Get languages form image data.
        LinkedHashMap<String,LinkedHashMap<String,ArrayList<LinkedHashMap<String,String>>>> imgVals
            = this.values.get("img_vals");

        if (imgVals != null) {
            for (LinkedHashMap<String, ArrayList<LinkedHashMap<String,String>>> v : imgVals.values()) {
                set.addAll(v.keySet());
            }
        }

        // Remove blank.
        set.remove("");

        // Change variable type and sort.
        ArrayList<String> langs = new ArrayList<String>();
        langs.addAll(set);
        Collections.sort(langs);

        return langs;
    }

    @Nullable
    private String getCommon(String type, String text, String lang) {
        if (this.values.get(type) == null) {
            return null;
        }
        if (this.values.get(type).containsKey(text)) {
            if (this.values.get(type).get(text).containsKey(lang)) {
                if (this.values.get(type).get(text).get(lang).size() > 0) {
                    return this.values.get(type).get(text).get(lang).get(0).get("data");
                }
            }
        }
        return null;
    }

    String getText(String text, String lang) {
        return this.getCommon("text_vals", text, lang);
    }

    String getImage(String src, String lang) {
        return this.getCommon("img_vals", src, lang);
    }
}

@@ -81,25 +81,17 @@
 va.util.
-ArrayList
+*
 ;%0Aimport
@@ -105,99 +105,53 @@
 til.
-Collections;%0Aimport java.util.HashSet;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Set
+regex.Matcher;%0Aimport java.util.regex.Pattern
 ;%0A%0Ai
@@ -367,34 +367,1322 @@
     
-values = JSON.decode(json)
+LinkedHashMap%3CString,LinkedHashMap%3CString,LinkedHashMap%3CString,ArrayList%3CLinkedHashMap%3CString,String%3E%3E%3E%3E%3E v%0A                = JSON.decode(json);%0A        this.values = removeJsessionid(v);%0A    %7D%0A%0A    private static LinkedHashMap%3CString,LinkedHashMap%3CString,LinkedHashMap%3CString,ArrayList%3CLinkedHashMap%3CString,String%3E%3E%3E%3E%3E%0A        removeJsessionid(LinkedHashMap%3CString,LinkedHashMap%3CString,LinkedHashMap%3CString,ArrayList%3CLinkedHashMap%3CString,String%3E%3E%3E%3E%3E v)%0A    %7B%0A        if (!v.containsKey(%22img_vals%22)) %7B%0A            return v;%0A        %7D%0A%0A        LinkedHashMap%3CString,LinkedHashMap%3CString,ArrayList%3CLinkedHashMap%3CString,String%3E%3E%3E%3E newImgVals%0A                = new LinkedHashMap%3CString,LinkedHashMap%3CString,ArrayList%3CLinkedHashMap%3CString,String%3E%3E%3E%3E();%0A%0A        for (Map.Entry%3CString, LinkedHashMap%3CString, ArrayList%3CLinkedHashMap%3CString, String%3E%3E%3E%3E map : v.get(%22img_vals%22).entrySet()) %7B%0A            String key = map.getKey();%0A            Matcher m = Pattern.compile(%22;jsessionid=%5B%5E=%5D+$%22, Pattern.CASE_INSENSITIVE).matcher(key);%0A            if (!m.find()) %7B%0A                newImgVals.put(key, map.getValue());%0A            %7D else %7B%0A                String newKey = m.replaceFirst(%22%22);%0A                newImgVals.put(newKey, map.getValue());%0A            %7D%0A        %7D%0A%0A        v.put(%22img_vals%22, newImgVals);%0A        return v
 ;%0A  

Remove jsessionid after retrieving data from API server. #11