package org.xdty.phone.number.net.cloud;

import android.content.Context;

import org.xdty.phone.number.model.INumber;
import org.xdty.phone.number.model.NumberHandler;
import org.xdty.phone.number.util.App;
import org.xdty.phone.number.util.Utils;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public final class CloudHandler implements NumberHandler<CloudNumber>, CloudService {

    private final static String API_URL = "https://backend.xdty.org/api/v1/";
    public final MediaType JSON;

    @Inject OkHttpClient mOkHttpClient;

    @Inject Context mContext;

    public CloudHandler() {
        App.getAppComponent().inject(this);
        JSON = MediaType.parse("application/json; charset=utf-8");
    }

    @Override
    public String url() {
        return API_URL;
    }

    public String userId() {
        return "hPMOqSPMu4";
    }

    @Override
    public String key() {
        return "vHc7RS5f66uIeUu5z95B";
    }

    @Override
    public CloudNumber find(String number) {
        return null;
    }

    @Override
    public boolean isOnline() {
        return false;
    }

    @Override
    public int getApiId() {
        return INumber.API_ID_CLOUD;
    }

    @Override
    public boolean put(CloudNumber number) {
        String url = API_URL + "caller";
        RequestBody body = RequestBody.create(JSON, Utils.gson().toJson(number));
        Request.Builder request = new Request.Builder()
                .url(url)
                .addHeader("X-LC-Id", userId())
                .addHeader("X-LC-Key", key())
                .post(body);
        try {
            Response response = mOkHttpClient.newCall(request.build()).execute();
            return response.code() == 201;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public CloudNumber get(String number) {
        return null;
    }
}

@@ -243,16 +243,199 @@
 Utils;%0A%0A
+import java.security.InvalidKeyException;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.util.Formatter;%0A%0Aimport javax.crypto.Mac;%0Aimport javax.crypto.spec.SecretKeySpec;%0A
 import j
@@ -753,16 +753,82 @@
 i/v1/%22;%0A
+    private static final String HMAC_SHA1_ALGORITHM = %22HmacSHA1%22;%0A
     publ
@@ -852,17 +852,16 @@
 e JSON;%0A
-%0A
     @Inj
@@ -892,17 +892,16 @@
 Client;%0A
-%0A
     @Inj
@@ -1061,32 +1061,718 @@
 utf-8%22);%0A    %7D%0A%0A
+    private static String toHexString(byte%5B%5D bytes) %7B%0A        Formatter formatter = new Formatter();%0A%0A        for (byte b : bytes) %7B%0A            formatter.format(%22%2502x%22, b);%0A        %7D%0A%0A        return formatter.toString();%0A    %7D%0A%0A    private static String hmac(String data, String key) %7B%0A        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);%0A        try %7B%0A            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);%0A            mac.init(signingKey);%0A            return toHexString(mac.doFinal(data.getBytes()));%0A        %7D catch (NoSuchAlgorithmException %7C InvalidKeyException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return %22%22;%0A    %7D%0A%0A
     @Override%0A  
@@ -2325,16 +2325,67 @@
 aller%22;%0A
+        String data = Utils.gson().toJson(number);%0A
         
@@ -2432,35 +2432,12 @@
 ON, 
-Utils.gson().toJson(number)
+data
 );%0A 
@@ -2549,73 +2549,58 @@
 er(%22
-X-LC-Id%22, userId())%0A                .addHeader(%22X-LC-Key%22
+Authorization%22, userId() + %22:%22 + hmac(data
 , key())
 %0A   
@@ -2595,16 +2595,17 @@
 , key())
+)
 %0A       

add hmac-sha1 auth support