/*
 * Copyright 2015 Synced Synapse. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xbmc.kore.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

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

/**
 * Misc util methods for use with JSON
 */
public class JsonUtils {

    public static String stringFromJsonNode(JsonNode node, String key) {
        // Duplicate code for performance resons
        if (node == null) return null;
        JsonNode value = node.get(key);
        if (value == null) return null;
        return value.textValue();
    }

    public static String stringFromJsonNode(JsonNode node, String key, String defaultValue) {
        if (node == null) return defaultValue;
        JsonNode value = node.get(key);
        if (value == null) return defaultValue;
        return value.textValue();
    }

    public static double doubleFromJsonNode(JsonNode node, String key) {
        return doubleFromJsonNode(node, key, 0);
    }

    public static double doubleFromJsonNode(JsonNode node, String key, double defaultValue) {
        if (node == null) return defaultValue;
        JsonNode value = node.get(key);
        if (value == null) return defaultValue;
        return value.asDouble();
    }

    public static int intFromJsonNode(JsonNode node, String key) {
        // Duplicate code for performance resons
        if (node == null) return 0;
        JsonNode value = node.get(key);
        if (value == null) return 0;
        return value.asInt();
    }

    public static int intFromJsonNode(JsonNode node, String key, int defaultValue) {
        if (node == null) return defaultValue;
        JsonNode value = node.get(key);
        if (value == null) return defaultValue;
        return value.asInt();
    }

    public static boolean booleanFromJsonNode(JsonNode node, String key) {
        return booleanFromJsonNode(node, key, false);
    }

    public static boolean booleanFromJsonNode(JsonNode node, String key, boolean defaultValue) {
        if (node == null) return defaultValue;
        JsonNode value = node.get(key);
        if (value == null) return defaultValue;
        return value.asBoolean();
    }

    public static List<String> stringListFromJsonNode(JsonNode node, String key) {
        if (node == null) return new ArrayList<String>(0);
        JsonNode value = node.get(key);
        if (value == null) return new ArrayList<String>(0);

        ArrayNode arrayNode = (ArrayNode)value;
        ArrayList<String> result = new ArrayList<String>(arrayNode.size());
        for (JsonNode innerNode : arrayNode) {
            result.add(innerNode.textValue());
        }
        return result;
    }

    public static List<Integer> integerListFromJsonNode(JsonNode node, String key) {
        if (node == null) return new ArrayList<Integer>(0);
        JsonNode value = node.get(key);
        if (value == null) return new ArrayList<Integer>(0);

        ArrayNode arrayNode = (ArrayNode)value;
        ArrayList<Integer> result = new ArrayList<Integer>(arrayNode.size());
        for (JsonNode innerNode : arrayNode) {
            result.add(innerNode.asInt());
        }
        return result;
    }
}

@@ -2993,24 +2993,93 @@
 tring%3E(0);%0A%0A
+        ArrayList%3CString%3E result;%0A        if (value.isArray()) %7B%0A    
         Arra
@@ -3099,32 +3099,33 @@
 de = (ArrayNode)
+ 
 value;%0A        A
@@ -3123,33 +3123,19 @@
         
-ArrayList%3CString%3E
+   
  result 
@@ -3177,32 +3177,36 @@
 ize());%0A        
+ 
+   
 for (JsonNode in
@@ -3232,32 +3232,36 @@
 ) %7B%0A            
+    
 result.add(inner
@@ -3275,24 +3275,234 @@
 xtValue());%0A
+            %7D%0A        %7D else %7B%0A            // This isn't exactly what we're expecting, but we can return the text value%0A            result = new ArrayList%3CString%3E(1);%0A            result.add(value.textValue());%0A
         %7D%0A  
@@ -3772,24 +3772,94 @@
 teger%3E(0);%0A%0A
+        ArrayList%3CInteger%3E result;%0A        if (value.isArray()) %7B%0A    
         Arra
@@ -3887,16 +3887,17 @@
 rayNode)
+ 
 value;%0A 
@@ -3903,34 +3903,19 @@
         
-ArrayList%3CInteger%3E
+   
  result 
@@ -3950,32 +3950,36 @@
 ayNode.size());%0A
+    
         for (Jso
@@ -4013,32 +4013,36 @@
 ) %7B%0A            
+    
 result.add(inner
@@ -4052,24 +4052,142 @@
 e.asInt());%0A
+            %7D%0A        %7D else %7B%0A            result = new ArrayList%3CInteger%3E(1);%0A            result.add(value.asInt());%0A
         %7D%0A  

Fix ClassCastException on getting lists from json rpc