package com.nestedworld.nestedworld.gcm;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.nestedworld.nestedworld.database.models.Combat;
import com.nestedworld.nestedworld.gcm.handler.GcmHandler;
import com.nestedworld.nestedworld.gcm.model.NotificationMessage;
import com.nestedworld.nestedworld.helpers.gcm.GcmHelper;
import com.nestedworld.nestedworld.network.socket.implementation.SocketMessageType;
import com.nestedworld.nestedworld.network.socket.models.message.combat.AvailableMessage;
import com.nestedworld.nestedworld.ui.launch.LaunchActivity;

import org.msgpack.value.Value;

import java.util.HashMap;
import java.util.Map;

public final class NestedWorldGcm {
    private final static Map<SocketMessageType.MessageKind, GcmHandler> mHandlers = buildHandlers();

    /*
    ** Constructor
     */
    private NestedWorldGcm() {
        //Private constructor for avoiding this class to be construct
    }

    /*
    ** Public method
     */
    public static void onMessageReceived(@NonNull final Context context, @NonNull final Map<Value, Value> message, @NonNull final SocketMessageType.MessageKind messageKind, @Nullable final SocketMessageType.MessageKind idKind) {
        NotificationMessage notificationMessage = new NotificationMessage();
        notificationMessage.type = messageKind;
        notificationMessage.content = message;

        handleMessage(context, notificationMessage, messageKind, idKind);
    }

    /*
    ** Internal method
     */
    private static Map<SocketMessageType.MessageKind, GcmHandler> buildHandlers() {
        Map<SocketMessageType.MessageKind, GcmHandler> handlers = new HashMap<>();
        handlers.put(SocketMessageType.MessageKind.TYPE_COMBAT_AVAILABLE, new GcmHandler() {
            @Override
            public void handle(@NonNull Context context, @NonNull NotificationMessage notification, @NonNull SocketMessageType.MessageKind messageKind, @Nullable SocketMessageType.MessageKind idKind) {
                AvailableMessage availableMessage = new AvailableMessage(notification.content, messageKind, idKind);
                Combat combat = availableMessage.saveAsCombat();

                //Display notification
                GcmHelper.displayNotification(context, "Un combat est diposnible : " + combat.origin, LaunchActivity.class);
            }
        });
        return handlers;
    }

    private static void handleMessage(@NonNull final Context context, @NonNull final NotificationMessage notification, @NonNull final SocketMessageType.MessageKind messageKind, @Nullable final SocketMessageType.MessageKind idKind) {
        mHandlers.get(notification.type).handle(context, notification, messageKind, idKind);
    }
}

@@ -153,16 +153,41 @@
 ullable;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -776,16 +776,92 @@
 ldGcm %7B%0A
+    private final static String TAG = NestedWorldGcm.class.getSimpleName();%0A
     priv
@@ -2603,32 +2603,70 @@
 Context context,
+%0A                                     
  @NonNull final 
@@ -2690,32 +2690,70 @@
 ge notification,
+%0A                                     
  @NonNull final 
@@ -2786,32 +2786,70 @@
 ind messageKind,
+%0A                                     
  @Nullable final
@@ -2881,32 +2881,224 @@
 eKind idKind) %7B%0A
+%0A        GcmHandler gcmHandler = mHandlers.get(notification.type);%0A        if (gcmHandler == null) %7B%0A            Log.d(TAG, %22Unsuported messageType%22 + notification.type);%0A        %7D else %7B%0A    
         mHandler
@@ -3170,20 +3170,30 @@
 d, idKind);%0A
+        %7D%0A
     %7D%0A%7D%0A

enhance log and prevent crash