package com.nestedworld.nestedworld.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.nestedworld.nestedworld.helpers.log.LogHelper;
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 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();

    /*
    ** Binder
     */
    public class LocalBinder extends Binder {
        public SocketService getService() {
            return SocketService.this;
        }
    }

    /*
    ** Life cycle
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogHelper.d(TAG, "onBind()");
        return mBinder;
    }

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

        LogHelper.d(TAG, "onStartCommand()");

        NestedWorldSocketAPI.getInstance(new ConnectionListener() {
            @Override
            public void onConnectionReady(@NonNull NestedWorldSocketAPI nestedWorldSocketAPI) {
                //Can send message here
            }

            @Override
            public void onConnectionLost() {
                NestedWorldSocketAPI.reset();
                onStartCommand(intent, flags, startId);
            }

            @Override
            public void onMessageReceived(@NonNull SocketMessageType.MessageKind kind, @NonNull Map<Value, Value> content) {
                if (kind == SocketMessageType.MessageKind.TYPE_COMBAT_AVAILABLE) {
                    AvailableMessage availableMessage = new AvailableMessage(content);
                    availableMessage.saveAsCombat();
                }
            }
        });

        return START_NOT_STICKY;
    }

    @Override
    public void onCreate() {
        LogHelper.d(TAG, "onCreate()");
    }

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

@@ -667,24 +667,75 @@
 lue.Value;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import java.
@@ -1036,24 +1036,112 @@
 Service() %7B%0A
+            // Return this instance of SocketService so clients can call public methods%0A
             
@@ -1280,32 +1280,59 @@
 ntent intent) %7B%0A
+        //Display some log%0A
         LogHelpe
@@ -1349,24 +1349,25 @@
 onBind()%22);%0A
+%0A
         retu
@@ -1485,24 +1485,51 @@
 startId) %7B%0A%0A
+        //Display some log%0A
         LogH
@@ -1555,32 +1555,82 @@
 rtCommand()%22);%0A%0A
+        //Instantiate a socketConnection listener%0A
         NestedWo
@@ -1817,17 +1817,35 @@
       //
-C
+Do what you want (c
 an send 
@@ -1855,13 +1855,9 @@
 sage
- here
+)
 %0A   
@@ -1931,24 +1931,52 @@
 ionLost() %7B%0A
+                //Clean API%0A
             
@@ -2004,24 +2004,55 @@
 API.reset();
+%0A%0A                //Re-init API
 %0A           
@@ -2278,232 +2278,70 @@
     
-if (kind == SocketMessageType.MessageKind.TYPE_COMBAT_AVAILABLE) %7B%0A                    AvailableMessage availableMessage = new AvailableMessage(content);%0A                    availableMessage.saveAsCombat();%0A                %7D
+//Do internal job%0A                parseMessage(kind, content);
 %0A   
@@ -2436,22 +2436,23 @@
  void on
-Create
+Destroy
 () %7B%0A   
@@ -2460,128 +2460,430 @@
     
-LogHelper.d(TAG, %22onCreate()%22);%0A    %7D%0A%0A    @Override%0A    public void onDestroy() %7B%0A        NestedWorldSocketAPI.reset();
+NestedWorldSocketAPI.reset();%0A    %7D%0A%0A    /*%0A    ** Internal method%0A     */%0A    private void parseMessage(@NonNull SocketMessageType.MessageKind kind, @NonNull Map%3CValue, Value%3E content) %7B%0A        //Do internal job%0A        if (kind == SocketMessageType.MessageKind.TYPE_COMBAT_AVAILABLE) %7B%0A            AvailableMessage availableMessage = new AvailableMessage(content);%0A            availableMessage.saveAsCombat();%0A        %7D
 %0A   

improve socketService maintenability