package ru.mail.park.chat.api.websocket;

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

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;

import ru.mail.park.chat.models.Contact;
import ru.mail.park.chat.models.Message;

/**
 * Created by Михаил on 10.07.2016.
 */
public class DispatcherOfDialog implements IDispatcher {
    private static final String TAG = DispatcherOfDialog.class.getSimpleName();
    @Nullable
    private IChatListener chatListener;
    private Context context;

    public DispatcherOfDialog(@NonNull Context context) {
        this.context = context;
    }

    public void setChatListener(@Nullable IChatListener chatListener) {
        this.chatListener = chatListener;
    }

    public Context getContext() {
        return context;
    }

    public void dispatchJSON(@NonNull final String method, final JSONObject jsonIncome) {
        try{
            switch (method) {
                case "newMessage":
                    if (jsonIncome.has("user"))
                        dispatchNewMessage(jsonIncome);
                    else
                        dispatchAckSend(jsonIncome);
                    break;
                case "DELETE":
                    dispatchDelete(jsonIncome);
                    break;
                case "GET":
                    dispatchGet(jsonIncome);
                    break;
                case "writeMessage":
                    dispatchWriteMessage(jsonIncome);
                    break;
            }
        } catch (JSONException e) {
            Log.w(TAG + ".dispatchJSON", e.getLocalizedMessage());
        }
    }

    private void dispatchAckSend(JSONObject income) throws JSONException {
        JSONObject data = income.getJSONObject("data");

        if (chatListener != null) {
            Message message = new Message(income, getContext());
            chatListener.onAcknowledgeSendMessage(message);
        }
    }

    private void dispatchNewMessage(JSONObject income) throws JSONException {
        JSONObject data = income.getJSONObject("data");

        if (chatListener != null) {
            Message message = new Message(income, getContext());
            chatListener.onIncomeMessage(message);
        }
    }

    private void dispatchDelete(JSONObject income) throws JSONException {
        int mid = income.getInt("mid");

        if (chatListener != null)
            chatListener.onActionDeleteMessage(mid);
    }

    private void dispatchGet(JSONObject income) {

    }

    private void dispatchWriteMessage(JSONObject income) throws JSONException {
        JSONObject data = income.getJSONObject("data");

        String cid = data.getString("idChat");
        JSONObject user = data.getJSONObject("user");

        if (chatListener != null) {
            try {
                Contact contact = new Contact(user, getContext());
                chatListener.onWrite(cid, contact);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}

@@ -1942,38 +1942,36 @@
 e = new Message(
-income
+data
 , getContext());
@@ -2261,22 +2261,20 @@
 Message(
-income
+data
 , getCon
@@ -2722,34 +2722,34 @@
 t data = income.
-ge
+op
 tJSONObject(%22dat
@@ -2746,32 +2746,64 @@
 bject(%22data%22);%0A%0A
+        if (data != null) %7B%0A    
         String c
@@ -2833,16 +2833,20 @@
 Chat%22);%0A
+    
         
@@ -2892,32 +2892,36 @@
 ser%22);%0A%0A        
+ 
+   
 if (chatListener
@@ -2948,14 +2948,22 @@
     
+    
 try %7B%0A
+    
     
@@ -3033,32 +3033,36 @@
                 
+    
 chatListener.onW
@@ -3085,32 +3085,36 @@
 t);%0A            
+    
 %7D catch (ParseEx
@@ -3142,16 +3142,20 @@
         
+  
+  
 e.printS
@@ -3163,24 +3163,42 @@
 ackTrace();%0A
+                %7D%0A
             

fix chats api errors