package com.example.android.sunshine.utilities;

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

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;

/**
 *
 * Created by Semmiverian on 3/26/17.
 */

public class WatchFaceUtilites {
    private static final String MAX_TEMP = "com.example.android.sunshine.max_temp";
    private static final String MIN_TEMP = "com.example.android.sunshine.min_temp";
    /**
     * Send Data to Watch Face
     *
     * @param mContext context to set the Google Api Client
     * @param weatherValue Weather Value that being fetch from the server
     */
    public static void sendDataToWatch(Context mContext, ContentValues weatherValue) {
        Log.d("Watch", "sendDataToWatch: " + weatherValue);


        GoogleApiClient  mGoogleApiClient = new GoogleApiClient.Builder(mContext)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(@Nullable Bundle bundle) {
                        Log.d("Watch", "onConnected: connect ");
                    }

                    @Override
                    public void onConnectionSuspended(int i) {

                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

                    }
                })
                .addApi(Wearable.API)
                .build();

        mGoogleApiClient.connect();

        PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/sync");
        putDataMapReq.getDataMap().putString(MAX_TEMP, "111");
        putDataMapReq.getDataMap().putString(MIN_TEMP, "100");
        PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
        putDataReq.setUrgent();

        Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq).setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
            @Override
            public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                if (dataItemResult.getStatus().isSuccess()) {
                    Log.d("Watch", "onResult: Berhasil kirim");
                } else {
                    Log.e("Watch", "onResult: Failed");
                }
            }
        });

    }
}

@@ -112,16 +112,87 @@
 ontext;%0A
+import android.graphics.Bitmap;%0Aimport android.graphics.BitmapFactory;%0A
 import a
@@ -205,24 +205,24 @@
 .os.Bundle;%0A
-
 import andro
@@ -492,16 +492,62 @@
 llback;%0A
+import com.google.android.gms.wearable.Asset;%0A
 import c
@@ -749,16 +749,55 @@
 rable;%0A%0A
+import java.io.ByteArrayOutputStream;%0A%0A
 /**%0A *%0A 
@@ -1035,24 +1035,100 @@
 .min_temp%22;%0A
+    private static final String ICON = %22com.example.android.sunshine.icon%22;%0A
     /**%0A    
@@ -1451,16 +1451,272 @@
 alue);%0A%0A
+        // TODO change the ID with actual weather ID%0A        int weatherIconId = SunshineWeatherUtils%0A                .getSmallArtResourceIdForWeatherCondition(1);%0A        Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), weatherIconId);%0A
 %0A       
@@ -2766,24 +2766,103 @@
 MP, %22100%22);%0A
+        putDataMapReq.getDataMap().putAsset(ICON, creatAssetFromBitmap(icon));%0A
         PutD
@@ -3446,11 +3446,292 @@
 ;%0A%0A    %7D
+%0A%0A    private static Asset creatAssetFromBitmap(Bitmap bitmap) %7B%0A        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();%0A        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);%0A        return Asset.createFromBytes(byteStream.toByteArray());%0A    %7D
 %0A%7D%0A

Update to Send Icon to the watch