package com.trevorhalvorson.quickstatus;

import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.Profile;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;

/**
 * Created by Trevor Halvorson on 1/5/2016.
 */
public class MobileListenerService extends WearableListenerService {

    private static final String TAG = MobileListenerService.class.getSimpleName();
    private static final String WEARABLE_DATA_PATH = "/wearable/data/path";

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        if (messageEvent.getPath().equals(WEARABLE_DATA_PATH)) {
            final String message = new String(messageEvent.getData());
            Bundle params = new Bundle();
            params.putString("message", message);
            new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/me/feed",
                    params,
                    HttpMethod.POST,
                    new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {
                            String title = "Facebook post successful";
                            String content = "Your Facebook status has been updated.";

                            if (response.getError() != null) {
                                title = "Error publishing post";
                                content = "An error occurred while updating your status. Please try again";
                            }

                            int notificationId = 0;
                            String facebookUrl = "https://www.facebook.com/" + Profile.getCurrentProfile().getId();
                            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl));
                            PendingIntent pendingIntent =
                                    PendingIntent.getActivity(MobileListenerService.this, 0, intent, 0);

                            NotificationCompat.Builder builder =
                                    new NotificationCompat.Builder(MobileListenerService.this)
                                            .setSmallIcon(R.mipmap.ic_launcher)
                                            .setContentTitle(title)
                                            .setContentText(content)
                                            .addAction(R.drawable.ic_person, getString(R.string.notification_action_text), pendingIntent);

                            NotificationManagerCompat manager =
                                    NotificationManagerCompat.from(MobileListenerService.this);
                            manager.notify(notificationId, builder.build());
                        }
                    }
            ).executeAsync();
        } else {
            super.onMessageReceived(messageEvent);
        }
    }
}

@@ -959,16 +959,32 @@
 TA_PATH)
+ && isLoggedIn()
 ) %7B%0A    
@@ -1497,121 +1497,59 @@
 e = 
-%22Facebook post successful%22;%0A                            String content = %22Your Facebook status has been updated.%22
+getString(R.string.post_success_notification_title)
 ;%0A%0A 
@@ -1654,139 +1654,57 @@
 e = 
-%22Error publishing 
+getString(R.string.
 post
-%22;%0A                                content = %22An error occurred while updating your status. Please try again%22
+_error_notification_title)
 ;%0A  
@@ -2388,26 +2388,25 @@
 n(R.
-mipmap.ic_launcher
+drawable.ic_watch
 )%0A  
@@ -2475,77 +2475,8 @@
 le)%0A
-                                            .setContentText(content)%0A
     
@@ -2959,56 +2959,875 @@
     
-super.onMessageReceived(messageEvent);%0A        %7D
+int notificationId = 1;%0A            NotificationCompat.Builder builder =%0A                    new NotificationCompat.Builder(MobileListenerService.this)%0A                            .setSmallIcon(R.drawable.ic_watch)%0A                            .setContentTitle(getString(R.string.post_error_notification_title))%0A                            .setContentText(getString(R.string.post_error_notification_content))%0A                            .setVibrate(new long%5B%5D%7B1000, 1000%7D);%0A%0A            NotificationManagerCompat manager =%0A                    NotificationManagerCompat.from(MobileListenerService.this);%0A            manager.notify(notificationId, builder.build());%0A        %7D%0A        super.onMessageReceived(messageEvent);%0A    %7D%0A%0A    public boolean isLoggedIn() %7B%0A        AccessToken accessToken = AccessToken.getCurrentAccessToken();%0A        return accessToken != null;
 %0A   

Fix crash caused when attempting to post without being logged in