// 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.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Wearable;

import static com.ezhuk.wear.NotificationUtils.*;
import static com.ezhuk.wear.MessageUtils.*;


public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    public static final String MESSAGE_PATH = "/message";

    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                Button buttonShow = (Button) stub.findViewById(R.id.button_show);
                buttonShow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        showNotifications();
                        moveTaskToBack(true);
                    }
                });

                Button buttonSendMsg = (Button) stub.findViewById(R.id.button_send_msg);
                buttonSendMsg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sendMessage(mGoogleApiClient, MESSAGE_PATH,
                                getString(R.string.message_text));
                    }
                });

                Button buttonSendData = (Button) stub.findViewById(R.id.button_send_data);
                buttonSendData.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // TODO
                    }
                });
            }
        });

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

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

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

    @Override
    public void onConnected(Bundle bundle) {
        // empty
    }

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

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

    private void showNotifications() {
        showNotification(this);
        showNotificationNoIcon(this);
        showNotificationMinPriority(this);
        showNotificationBigTextStyle(this);
        showNotificationBigPictureStyle(this);
        showNotificationInboxStyle(this);
        showNotificationWithPages(this);
        showNotificationWithAction(this);
        showNotificationWithInputForPrimaryAction(this);
        showNotificationWithInputForSecondaryAction(this);
        showGroupNotifications(this);
    }
}

@@ -183,16 +183,87 @@
 tivity;%0A
+import android.graphics.Bitmap;%0Aimport android.graphics.BitmapFactory;%0A
 import a
@@ -280,16 +280,16 @@
 Bundle;%0A
-
 import a
@@ -502,16 +502,62 @@
 Client;%0A
+import com.google.android.gms.wearable.Asset;%0A
 import c
@@ -598,16 +598,55 @@
 rable;%0A%0A
+import java.io.ByteArrayOutputStream;%0A%0A
 import s
@@ -878,16 +878,16 @@
 tener %7B%0A
-
     publ
@@ -935,16 +935,68 @@
 essage%22;
+%0A    public static final String DATA_PATH = %22/data%22;
 %0A%0A    pr
@@ -2542,15 +2542,447 @@
     
-// TODO
+Bitmap bitmap = BitmapFactory.decodeResource(getResources(),%0A                                R.drawable.background);%0A                        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();%0A                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);%0A                        sendData(mGoogleApiClient, DATA_PATH,%0A                                Asset.createFromBytes(byteStream.toByteArray()));
 %0A   

Send bitmap as an asset.