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 GoogleApiClient mGoogleApiClient;
    private Vibrator mVibrator;

    private boolean mShouldLaunchFinish;

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

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        Log.d(TAG, "onDataChanged: " + dataEvents);
        if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting()) {
            ConnectionResult result = mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);
            if (!result.isSuccess()) {
                Log.e(TAG, "onDataChanged: Failed to connect to GoogleApiClient, error: "
                        + result.getErrorCode());
                return;
            }
        }

        for (DataEvent event : dataEvents) {
            switch (event.getDataItem().getUri().getPath()) {
                case Consts.Paths.GAME_FINISH:
                    if (mShouldLaunchFinish) {
                        Intent finish = new Intent(this, FinishActivity.class);
                        DataMapItem item = DataMapItem.fromDataItem(event.getDataItem());
                        GameInformation info = GameInformation.fromDataMap(item.getDataMap());
                        finish.putExtra(Consts.Keys.GAME_DATA, info);
                        finish.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(finish);
                        GameState.getInstance().stop();
                        mShouldLaunchFinish = false;
                        NotificationManager.remove(this);
                        Log.d(TAG, "onDataChanged: launching Finish activity");
                    }
                    break;
            }
        }
    }

    @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);
                } else {
                    mShouldLaunchFinish = true;
                }
                startActivity(intent);
                break;
            case Consts.Paths.GAME_FINISH:
                mShouldLaunchFinish = true;
                mVibrator.cancel();
                break;
            case Consts.Paths.GAME_INFORMATION:
                GameState.getInstance().setIsShotTime(false);
                mVibrator.cancel();
                break;
        }
        super.onMessageReceived(event);
    }
}

@@ -1087,127 +1087,38 @@
 0%7D;%0A
-%0A    private GoogleApiClient mGoogleApiClient;%0A    private Vibrator mVibrator;%0A%0A    private boolean mShouldLaunchFinish
+    private Vibrator mVibrator
 ;%0A%0A 
@@ -1185,32 +1185,48 @@
 eate();%0A        
+GoogleApiClient 
 mGoogleApiClient
@@ -1448,1521 +1448,8 @@
  %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                    if (mShouldLaunchFinish) %7B%0A                        Intent finish = new Intent(this, FinishActivity.class);%0A                        DataMapItem item = DataMapItem.fromDataItem(event.getDataItem());%0A                        GameInformation info = GameInformation.fromDataMap(item.getDataMap());%0A                        finish.putExtra(Consts.Keys.GAME_DATA, info);%0A                        finish.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);%0A                        startActivity(finish);%0A                        GameState.getInstance().stop();%0A                        mShouldLaunchFinish = false;%0A                        NotificationManager.remove(this);%0A                        Log.d(TAG, %22onDataChanged: launching Finish activity%22);%0A                    %7D%0A                    break;%0A            %7D%0A        %7D%0A    %7D%0A%0A
     
@@ -2175,81 +2175,8 @@
 1);%0A
-                %7D else %7B%0A                    mShouldLaunchFinish = true;%0A
     
@@ -2314,69 +2314,459 @@
     
-mShouldLaunchFinish = true;%0A                mVibrator.cancel(
+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 

Fix bug with sending finish activity