package com.github.kubatatami.judonetworking.internals.wear;

import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okio.BufferedSink;

/**
 * Created by Kuba on 28/09/15.
 */
public class HandheldListenerService extends WearableListenerService {

    protected MessageUtils messageUtils;

    protected OkHttpClient baseClient = new OkHttpClient();

    @Override
    public void onCreate() {
        super.onCreate();
        messageUtils = new MessageUtils(this);
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        if (messageEvent.getPath().contains(MessageUtils.MSG_PATH)) {
            try {
                String id = messageEvent.getPath().substring(MessageUtils.MSG_PATH.length());
                WearRequest request = messageUtils.readObject(messageEvent.getData(), WearRequest.class);
                WearResponse response = send(request);
                messageUtils.sendMessage(id, messageEvent.getSourceNodeId(), response);
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            super.onMessageReceived(messageEvent);
        }
    }

    protected WearResponse send(final WearRequest request) throws IOException {
        RequestBody requestBody = null;
        OkHttpClient client = baseClient.clone();
        client.setConnectTimeout(request.getConnectTimeout(), TimeUnit.MILLISECONDS);
        client.setReadTimeout(request.getReadTimeout(), TimeUnit.MILLISECONDS);
        Request.Builder builder = new Request.Builder();
        if (request.getBody() != null) {
            requestBody = new RequestBody() {
                @Override
                public MediaType contentType() {
                    return MediaType.parse(request.getMimeType());
                }

                @Override
                public void writeTo(BufferedSink sink) throws IOException {
                    sink.write(request.getBody());
                }

                @Override
                public long contentLength() throws IOException {
                    return request.getBody().length;
                }
            };
        }
        builder.method(request.getMethodName(), requestBody);
        for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }
        builder.url(request.getUrl());
        Response response = client.newCall(builder.build()).execute();
        WearResponse wearResponse = new WearResponse();
        wearResponse.setSuccessful(response.isSuccessful());
        wearResponse.setCode(response.code());
        wearResponse.setMessage(response.message());
        wearResponse.setBody(response.body().bytes());
        Map<String, List<String>> map = new HashMap<>();
        for (String name : response.headers().names()) {
            map.put(name, response.headers(name));
        }
        wearResponse.setHeaders(map);
        return wearResponse;
    }
}

@@ -55,16 +55,77 @@
 .wear;%0A%0A
+import com.github.kubatatami.judonetworking.logs.JudoLogger;%0A
 import c
@@ -1092,18 +1092,38 @@
     
-try %7B%0A    
+WearResponse response = null;%0A
     
@@ -1204,24 +1204,42 @@
 .length());%0A
+            try %7B%0A
             
@@ -1344,37 +1344,24 @@
             
-WearResponse 
 response = s
@@ -1370,24 +1370,129 @@
 d(request);%0A
+            %7D catch (IOException e) %7B%0A                JudoLogger.log(e);%0A            %7D%0A            try %7B%0A
             
@@ -1625,26 +1625,24 @@
     
-e.printStackTrace(
+JudoLogger.log(e
 );%0A 
@@ -1646,33 +1646,32 @@
 ;%0A            %7D%0A
-%0A
         %7D else %7B

send response when connection error