package io.honeyqa.client.network;

import java.util.concurrent.TimeUnit;


import android.os.Handler;
import android.os.Message;
import android.util.Log;

import io.honeyqa.client.auth.Encryptor;
import io.honeyqa.client.data.HoneyQAData;
import io.honeyqa.client.network.okhttp.MediaType;
import io.honeyqa.client.network.okhttp.OkHttpClient;
import io.honeyqa.client.network.okhttp.Request;
import io.honeyqa.client.network.okhttp.RequestBody;
import io.honeyqa.client.network.okhttp.Response;

public class Network extends Thread {

    /**
     * HTTP Method
     * GET / POST
     */
    public enum Method {
        GET, POST
    }

    /**
     * Media type for HTTP header
     */
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");

    // Variables for communicate with server
    private boolean isEncrypt;
    private Method method;
    private String data, url;
    private Handler handler;

    /**
     * Check options (url / data / method)
     *
     * @throws IllegalStateException when url / data / method not set
     */
    private void checkAssert() {
        if (url == null || data == null || method == null)
            throw new IllegalStateException("you might miss setNetworkOption method");
    }

    /**
     * Set network options
     *
     * @param url       API Server URL
     * @param data
     * @param method
     * @param isEncrypt
     */
    public void setNetworkOption(String url, String data, Method method, boolean isEncrypt) {
        this.url = url;
        this.data = data;
        this.method = method;
        this.isEncrypt = isEncrypt;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        switch (method) {
            case GET:
                requestGet();
                break;
            case POST:
                requestPost();
                break;
        }
    }

    private void requestGet() {
        try {
            checkAssert();
            OkHttpClient client = new OkHttpClient();
            setTimeout(client);
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            Response response = client.newCall(request).execute();
            if (handler != null) {
                Message msg = new Message();
                msg.obj = response.body().string();
                handler.sendMessage(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void requestPost() {
        try {
            checkAssert();
            OkHttpClient client = new OkHttpClient();
            setTimeout(client);
            RequestBody body = RequestBody.create(JSON, data);
            Request.Builder r = new Request.Builder()
                    .header("Content-Type", "application/json; charset=utf-8")
                    .addHeader("version", "1.0.0")
                    .url(url)
                    .post(body);
            if (isEncrypt && Encryptor.baseKey != null && Encryptor.token != null) {
                r.addHeader("HoneyQA-Encrypt-Opt", "aes-256-cbc-pkcs5padding+base64");
                data = Encryptor.encrypt(data);
                Log.e(HoneyQAData.HONEYQA_SDK_LOG, data);
            }
            Response response = client.newCall(r.build()).execute();
            if (handler != null) {
                Message msg = new Message();
                msg.obj = response.body().string();
                handler.sendMessage(msg);
            }
            int statusCode = response.code();
            Log.e(HoneyQAData.HONEYQA_SDK_LOG, String.format("HoneyQA Response Code : %d", statusCode));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setTimeout(OkHttpClient client) {
        client.setConnectTimeout(5, TimeUnit.SECONDS);
        client.setReadTimeout(5, TimeUnit.SECONDS);
    }

}

@@ -273,16 +273,19 @@
 .okhttp.
+HQ_
 MediaTyp
@@ -327,16 +327,19 @@
 .okhttp.
+HQ_
 OkHttpCl
@@ -380,24 +380,27 @@
 work.okhttp.
+HQ_
 Request;%0Aimp
@@ -436,16 +436,19 @@
 .okhttp.
+HQ_
 RequestB
@@ -492,16 +492,19 @@
 .okhttp.
+HQ_
 Response
@@ -722,16 +722,19 @@
 c final 
+HQ_
 MediaTyp
@@ -754,16 +754,19 @@
       = 
+HQ_
 MediaTyp
@@ -2060,32 +2060,35 @@
 ();%0A            
+HQ_
 OkHttpClient cli
@@ -2089,32 +2089,35 @@
 nt client = new 
+HQ_
 OkHttpClient();%0A
@@ -2156,24 +2156,27 @@
             
+HQ_
 Request requ
@@ -2181,24 +2181,27 @@
 quest = new 
+HQ_
 Request.Buil
@@ -2270,32 +2270,35 @@
 ();%0A            
+HQ_
 Response respons
@@ -2692,16 +2692,19 @@
         
+HQ_
 OkHttpCl
@@ -2721,16 +2721,19 @@
 t = new 
+HQ_
 OkHttpCl
@@ -2780,24 +2780,27 @@
             
+HQ_
 RequestBody 
@@ -2806,16 +2806,19 @@
  body = 
+HQ_
 RequestB
@@ -2853,16 +2853,19 @@
         
+HQ_
 Request.
@@ -2880,16 +2880,19 @@
 r = new 
+HQ_
 Request.
@@ -3398,16 +3398,19 @@
         
+HQ_
 Response
@@ -3762,16 +3762,19 @@
 HoneyQA 
+HQ_
 Response
@@ -3910,16 +3910,19 @@
 Timeout(
+HQ_
 OkHttpCl

update network library (okhttp)