/*
 * Copyright 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.muzei;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;

import com.google.android.apps.muzei.api.Artwork;
import com.google.android.apps.muzei.render.BitmapRegionLoader;
import com.google.android.apps.muzei.render.ImageUtil;
import com.google.android.apps.muzei.util.LogUtil;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.Wearable;

import java.io.ByteArrayOutputStream;
import java.util.concurrent.TimeUnit;

import static com.google.android.apps.muzei.util.LogUtil.LOGW;

/**
 * Controller for working with the Android Wear API. Also in charge of dealing with
 * Google Play Services.
 */
public class WearableController {
    private static final String TAG = LogUtil.makeLogTag(WearableController.class);

    public static synchronized void updateDataLayer(Context context,
                                                    Artwork artwork, BitmapRegionLoader bitmapRegionLoader) {
        if (ConnectionResult.SUCCESS != GooglePlayServicesUtil.isGooglePlayServicesAvailable(context)) {
            return;
        }
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(Wearable.API)
                .build();
        ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
        if (!connectionResult.isSuccess()) {
            LOGW(TAG, "onConnectionFailed: " + connectionResult);
            return;
        }
        Rect rect = new Rect();
        int width = bitmapRegionLoader.getWidth();
        int height = bitmapRegionLoader.getHeight();
        rect.set(0, 0, width, height);
        BitmapFactory.Options options = new BitmapFactory.Options();
        if (width > height) {
            options.inSampleSize = ImageUtil.calculateSampleSize(height, 320);
        } else {
            options.inSampleSize = ImageUtil.calculateSampleSize(width, 320);
        }
        Bitmap image = bitmapRegionLoader.decodeRegion(rect, options);
        if (image != null) {
            final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
            Asset asset = Asset.createFromBytes(byteStream.toByteArray());
            PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/artwork");
            dataMapRequest.getDataMap().putDataMap("artwork", DataMap.fromBundle(artwork.toBundle()));
            dataMapRequest.getDataMap().putAsset("image", asset);
            Wearable.DataApi.putDataItem(googleApiClient, dataMapRequest.asPutDataRequest()).await();
        }
        googleApiClient.disconnect();
    }
}

@@ -1498,16 +1498,79 @@
 il.LOGW;
+%0Aimport static com.google.android.apps.muzei.util.LogUtil.LOGV;
 %0A%0A/**%0A *
@@ -2396,24 +2396,227 @@
 uccess()) %7B%0A
+            if (connectionResult.getErrorCode() != ConnectionResult.API_UNAVAILABLE) %7B%0A                LOGV(TAG, %22Wearable API unavailable, cancelling updateDataLayer request%22);%0A            %7D else %7B%0A    
             
@@ -2665,24 +2665,38 @@
 ionResult);%0A
+            %7D%0A
             

Remove Wearable.API warning messages when API is unavailable