package org.openhab.binding.heos.resources;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class HeosDeserializerPayload implements JsonDeserializer<HeosResponsePayload> {

    private HeosResponsePayload responsePayload = new HeosResponsePayload();

    @Override
    public HeosResponsePayload deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {

        boolean arrayTrue = false;
        List<HashMap<String, String>> mapList = new ArrayList<HashMap<String, String>>();

        JsonObject jsonObject = json.getAsJsonObject();

        // Checks if behind "payload" an array starts or not. Feature might be implemented much better...... :)
        String check = String.valueOf(json);
        int payPor = check.indexOf("payload");
        // jsonObject.get("payload").isArray(); Maybe use this one.....
        if (String.valueOf(check.charAt(payPor + 9)).equals("[")) {
            arrayTrue = true;
        }

        if (jsonObject.has("payload") && arrayTrue) {

            JsonArray jsonArray = jsonObject.get("payload").getAsJsonArray();

            for (int i = 0; i < jsonArray.size(); i++) {
                HashMap<String, String> payload = new HashMap<String, String>();
                JsonObject object = jsonArray.get(i).getAsJsonObject();
                for (Entry<String, JsonElement> entry : object.entrySet()) {
                    payload.put(entry.getKey(), entry.getValue().getAsString());
                    // System.out.println(entry.getKey()+ ": " + entry.getValue());
                }
                mapList.add(payload);
            }
        } else if (jsonObject.has("payload") && !jsonObject.isJsonArray()) {
            HashMap<String, String> payload = new HashMap<String, String>();
            JsonObject jsonPayload = jsonObject.get("payload").getAsJsonObject();

            for (Entry<String, JsonElement> entry : jsonPayload.entrySet()) {

                payload.put(entry.getKey(), entry.getValue().getAsString());
                // System.out.println(entry.getKey()+ ": " + entry.getValue());
            }
            mapList.add(payload);

        } else {
            HashMap<String, String> payload = new HashMap<String, String>();
            payload.put("No Payload", "No Payload");
            mapList.add(payload);
        }
        responsePayload.setPayload(mapList);
        return responsePayload;
    }

    public void itterateValues() {

    }
}

@@ -1313,24 +1313,164 @@
 %0A        %7D%0A%0A
+        // if (jsonObject.has(%22payload%22)) %7B%0A        // JsonArray obj = jsonObject.get(%22payload%22).getAsJsonArray();%0A        //%0A        // %7D%0A%0A
         if (
@@ -1507,24 +1507,24 @@
 rrayTrue) %7B%0A
-
 %0A           
@@ -1800,16 +1800,143 @@
 bject();
+%0A%0A                System.out.println(%22contains Player: %22 + object.has(%22players%22));%0A                // object.remove(%22players%22);
 %0A       
@@ -2001,24 +2001,25 @@
 trySet()) %7B%0A
+%0A
             
@@ -2101,32 +2101,58 @@
               //
+ Debug%0A                   
  System.out.prin
@@ -2161,32 +2161,33 @@
 n(entry.getKey()
+ 
 + %22: %22 + entry.g
@@ -2246,32 +2246,33 @@
 t.add(payload);%0A
+%0A
             %7D%0A  
@@ -2261,32 +2261,33 @@
 %0A%0A            %7D%0A
+%0A
         %7D else i
@@ -2344,16 +2344,25 @@
 Array())
+%0A%0A       
  %7B%0A     

Support for nested HEOS Groups commands