package ca.hoogit.powerhour.wear.DataLayer;

import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
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.MessageEvent;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;

import java.util.concurrent.TimeUnit;

import ca.hoogit.powerhour.wear.Game.FinishActivity;
import ca.hoogit.powerhour.wear.Game.GameActivity;
import ca.hoogit.powerhour.wear.Game.GameState;
import ca.hoogit.powerhourshared.DataLayer.Consts;

public class GameListenerService extends WearableListenerService {

    private static final String TAG = GameListenerService.class.getSimpleName();

    private static final long[] VIBRATE_PATTERN = {200, 300, 200, 300, 200, 300, 300, 300, 200};
    private Vibrator mVibrator;

    @Override
    public void onCreate() {
        super.onCreate();
        GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .build();
        mGoogleApiClient.connect();
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    }

    @Override
    public void onMessageReceived(MessageEvent event) {
        String path = event.getPath();
        Log.d(TAG, "onMessageReceived: Path " + path);

        switch (path) {
            case Consts.Paths.START_ACTIVITY:
            case Consts.Paths.GAME_SHOT:
                boolean isGameShot = Consts.Paths.GAME_SHOT.equals(path);
                Intent intent = new Intent(this, GameActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Consts.Game.FLAG_GAME_IS_SHOT_TIME, isGameShot);

                GameState.getInstance().setIsShotTime(isGameShot);
                if (isGameShot) {
                    mVibrator.vibrate(VIBRATE_PATTERN, -1);
                }
                startActivity(intent);
                break;
            case Consts.Paths.GAME_FINISH:
                Intent finish = new Intent(this, FinishActivity.class);
                finish.putExtra(Consts.Keys.GAME_DATA, GameState.getInstance().get());
                finish.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                GameState.getInstance().stop();
                NotificationManager.remove(this);
                mVibrator.cancel();

                startActivity(finish);
                Log.d(TAG, "onDataChanged: launching Finish activity");
                break;
            case Consts.Paths.GAME_INFORMATION:
                GameState.getInstance().setIsShotTime(false);
                mVibrator.cancel();
                break;
        }
        super.onMessageReceived(event);
    }
}

@@ -982,24 +982,71 @@
 pleName();%0A%0A
+    private GoogleApiClient mGoogleApiClient;%0A%0A
     private 
@@ -1130,16 +1130,16 @@
 , 200%7D;%0A
-
     priv
@@ -1240,32 +1240,16 @@
         
-GoogleApiClient 
 mGoogleA
@@ -1475,16 +1475,1472 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void onDataChanged(DataEventBuffer dataEvents) %7B%0A        Log.d(TAG, %22onDataChanged: %22 + dataEvents);%0A        if (!mGoogleApiClient.isConnected() %7C%7C !mGoogleApiClient.isConnecting()) %7B%0A            ConnectionResult result = mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);%0A            if (!result.isSuccess()) %7B%0A                Log.e(TAG, %22onDataChanged: Failed to connect to GoogleApiClient, error: %22%0A                        + result.getErrorCode());%0A                return;%0A            %7D%0A        %7D%0A%0A        for (DataEvent event : dataEvents) %7B%0A            switch (event.getDataItem().getUri().getPath()) %7B%0A                case Consts.Paths.GAME_FINISH:%0A                    DataMapItem item = DataMapItem.fromDataItem(event.getDataItem());%0A                    GameInformation info = GameInformation.fromDataMap(item.getDataMap());%0A                    GameState.getInstance().update(info);%0A%0A                    Intent finish = new Intent(this, FinishActivity.class);%0A                    finish.putExtra(Consts.Keys.GAME_DATA, info);%0A                    finish.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);%0A%0A                    GameState.getInstance().stop();%0A                    NotificationManager.remove(this);%0A                    mVibrator.cancel();%0A%0A                    Log.d(TAG, %22onDataChanged: launching Finish activity%22);%0A                    startActivity(finish);%0A                    break;%0A            %7D%0A        %7D%0A    %7D%0A%0A
     @Ove
@@ -3668,32 +3668,32 @@
               %7D%0A
+
                 
@@ -3742,544 +3742,8 @@
 ak;%0A
-            case Consts.Paths.GAME_FINISH:%0A                Intent finish = new Intent(this, FinishActivity.class);%0A                finish.putExtra(Consts.Keys.GAME_DATA, GameState.getInstance().get());%0A                finish.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);%0A%0A                GameState.getInstance().stop();%0A                NotificationManager.remove(this);%0A                mVibrator.cancel();%0A%0A                startActivity(finish);%0A                Log.d(TAG, %22onDataChanged: launching Finish activity%22);%0A                break;%0A
     

Fix finish activity on Wear