package com.nestedworld.nestedworld.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.ContextCompat;

import com.nestedworld.nestedworld.R;
import com.nestedworld.nestedworld.event.socket.OnAvailableMessageEvent;
import com.nestedworld.nestedworld.helpers.log.LogHelper;
import com.nestedworld.nestedworld.models.Combat;
import com.nestedworld.nestedworld.network.socket.implementation.NestedWorldSocketAPI;
import com.nestedworld.nestedworld.network.socket.implementation.SocketMessageType;
import com.nestedworld.nestedworld.network.socket.listener.ConnectionListener;
import com.nestedworld.nestedworld.network.socket.models.message.combat.AvailableMessage;
import com.nestedworld.nestedworld.ui.launch.LaunchActivity;

import org.greenrobot.eventbus.EventBus;
import org.msgpack.value.Value;

import java.util.Map;

public class SocketService extends Service {

    public final static String TAG = SocketService.class.getSimpleName();
    private final IBinder mBinder = new LocalBinder();
    private NestedWorldSocketAPI mNestedWorldSocketAPI = null;

    /*
    ** Life cycle
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //Display some log
        LogHelper.d(TAG, "onBind()");

        return mBinder;
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {

        //Display some log
        LogHelper.d(TAG, "onStartCommand()");

        //Instantiate a socketConnection listener
        NestedWorldSocketAPI.getInstance(new ConnectionListener() {
            @Override
            public void onConnectionReady(@NonNull NestedWorldSocketAPI nestedWorldSocketAPI) {
                mNestedWorldSocketAPI = nestedWorldSocketAPI;
            }

            @Override
            public void onConnectionLost() {
                mNestedWorldSocketAPI = null;

                //Clean API
                NestedWorldSocketAPI.reset();

                //Re-init API
                onStartCommand(intent, flags, startId);
            }

            @Override
            public void onMessageReceived(@NonNull SocketMessageType.MessageKind kind, @NonNull Map<Value, Value> content) {
                //Do internal job
                parseMessage(kind, content);
            }
        });

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        NestedWorldSocketAPI.reset();
    }

    /*
    ** Public method (for client)
     */
    @Nullable
    public NestedWorldSocketAPI getApiInstance() {
        return mNestedWorldSocketAPI;
    }

    /*
    ** Internal method
     */
    private void parseMessage(@NonNull SocketMessageType.MessageKind kind, @NonNull Map<Value, Value> content) {
        //Do internal job
        switch (kind) {
            case TYPE_CHAT_USER_JOINED:
                break;
            case TYPE_CHAT_USER_PARTED:
                break;
            case TYPE_CHAT_MESSAGE_RECEIVED:
                break;
            case TYPE_COMBAT_START:
                break;
            case TYPE_COMBAT_AVAILABLE:
                AvailableMessage availableMessage = new AvailableMessage(content);
                Combat combat = availableMessage.saveAsCombat();

                displayNotification("Un combat est diposnible : " + combat.origin);

                EventBus.getDefault().post(new OnAvailableMessageEvent(availableMessage));
                break;
            case TYPE_COMBAT_MONSTER_KO:
                break;
            case TYPE_COMBAT_ATTACK_RECEIVED:
                break;
            case TYPE_COMBAT_MONSTER_REPLACED:
                break;
            case TYPE_COMBAT_END:
                break;
            case TYPE_GEO_PLACES_CAPTURED:
                break;
            case TYPE_AUTHENTICATE:
                break;
            case TYPE_CHAT_JOIN_CHANNEL:
                break;
            case TYPE_CHAT_PART_CHANNEL:
                break;
            case TYPE_CHAT_SEND_MESSAGE:
                break;
            case TYPE_COMBAT_SEND_ATTACK:
                break;
            case TYPE_COMBAT_MONSTER_KO_CAPTURE:
                break;
            case TYPE_COMBAT_MONSTER_KO_REPLACE:
                break;
            case TYPE_COMBAT_FLEE:
                break;
            case TYPE_COMBAT_ASK:
                break;
            case TYPE_RESULT:
                break;
        }
    }

    private void displayNotification(@NonNull final String title) {
        //Build notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle(getString(R.string.app_name))
                .setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.apptheme_color))
                .setContentText(title);

        //Display notification
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    }


    /*
    ** Binder
     */
    public class LocalBinder extends Binder {
        public SocketService getService() {
            // Return this instance of SocketService so clients can call public methods
            return SocketService.this;
        }
    }
}

@@ -70,16 +70,23 @@
 fication
+Manager
 ;%0Aimport
@@ -98,35 +98,29 @@
 oid.app.
-NotificationManager
+PendingIntent
 ;%0Aimport
@@ -398,56 +398,8 @@
 at;%0A
-import android.support.v4.app.TaskStackBuilder;%0A
 impo
@@ -5180,24 +5180,316 @@
 xt(title);%0A%0A
+        //Add action on notification%0A        Intent intentTarget = new Intent(this, LaunchActivity.class);%0A        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, intentTarget, PendingIntent.FLAG_CANCEL_CURRENT);%0A        builder.setContentIntent(resultPendingIntent);%0A%0A
         //Di
@@ -5499,32 +5499,32 @@
 ay notification%0A
-
         Notifica
@@ -5687,16 +5687,15 @@
 ));%0A
+
     %7D%0A%0A
-%0A
     

fix notification action issue