// Copyright (c) 2014 Eugene Zhuk.
// Use of this source code is governed by the MIT license that can be found
// in the LICENSE file.

package com.ezhuk.wear;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Wearable;

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


public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        MessageApi.MessageListener,
        DataApi.DataListener {
    private static final String TAG = "Mobile.MainActivity";
    public static final String MESSAGE_PATH = "/message";
    public static final String DATA_PATH = "/data";
    private static final int TIMEOUT = 30;

    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Wearable.API)
                .build();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (null != mGoogleApiClient && !mGoogleApiClient.isConnected()) {
            mGoogleApiClient.connect();
        }
    }

    @Override
    protected void onStop() {
        if (null != mGoogleApiClient && mGoogleApiClient.isConnected()) {
            Wearable.MessageApi.removeListener(mGoogleApiClient, this);
            Wearable.DataApi.removeListener(mGoogleApiClient, this);
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Wearable.MessageApi.addListener(mGoogleApiClient, this);
        Wearable.DataApi.addListener(mGoogleApiClient, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        // empty
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // empty
    }

    private void showMessage(String message) {
        final String param = message;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), param, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        if (messageEvent.getPath().equals(MESSAGE_PATH)) {
            showMessage(new String(messageEvent.getData()));
        }
    }

    private void showAsset(Bitmap bitmap) {
        final Bitmap param = bitmap;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ImageView view = (ImageView) findViewById(R.id.image_view);
                view.setImageBitmap(param);
            }
        });
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        for (DataEvent event : dataEvents) {
            if (event.getDataItem().getUri().getPath().equals(DATA_PATH)) {
                if (DataEvent.TYPE_CHANGED == event.getType()) {
                    DataMapItem item = DataMapItem.fromDataItem(event.getDataItem());
                    Asset asset = item.getDataMap().getAsset("data");

                    ConnectionResult result = mGoogleApiClient
                            .blockingConnect(TIMEOUT, TimeUnit.SECONDS);
                    if (result.isSuccess()) {
                        InputStream stream = Wearable.DataApi
                                .getFdForAsset(mGoogleApiClient, asset)
                                .await().getInputStream();
                        if (null != asset) {
                            Bitmap bitmap = BitmapFactory.decodeStream(stream);
                            showAsset(bitmap);
                        }
                    }
                } else if (DataEvent.TYPE_DELETED == event.getType()) {
                    // TODO
                }
            }
        }
    }
}

@@ -1089,60 +1089,8 @@
 ks,%0A
-        GoogleApiClient.OnConnectionFailedListener,%0A
     
@@ -1732,20 +1732,46 @@
 istener(
-this
+new ConnectionFailedListener()
 )%0A      
@@ -3326,32 +3326,147 @@
 // empty%0A    %7D%0A%0A
+    private class ConnectionFailedListener implements%0A            GoogleApiClient.OnConnectionFailedListener %7B%0A    
     @Override%0A  
@@ -3455,32 +3455,36 @@
       @Override%0A
+    
     public void 
@@ -3529,32 +3529,36 @@
 sult) %7B%0A        
+    
 // empty%0A    %7D%0A%0A
@@ -3542,32 +3542,42 @@
        // empty%0A
+        %7D%0A
     %7D%0A%0A    priva

Move connection failed listener into a separate class.