/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pyamsoft.powermanager.dagger.observer.state;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;
import java.util.List;
import java.util.concurrent.TimeUnit;
import timber.log.Timber;

class WearStateObserver extends StateObserver {

  @NonNull private final GoogleApiClient googleApiClient;

  WearStateObserver(@NonNull Context context) {
    super(context);
    googleApiClient =
        new GoogleApiClient.Builder(context.getApplicationContext()).addApiIfAvailable(Wearable.API)
            .build();
  }

  /**
   * Return true if stream should continue, false otherwise
   */
  @WorkerThread @Override public boolean is() {
    Timber.d("Check if wearable is connected");
    final ConnectionResult connectionResult = googleApiClient.blockingConnect(4, TimeUnit.SECONDS);
    boolean result;
    if (connectionResult.isSuccess()) {
      Timber.d("Connect Google APIs");
      final NodeApi.GetConnectedNodesResult nodesResult =
          Wearable.NodeApi.getConnectedNodes(googleApiClient).await(4, TimeUnit.SECONDS);
      Node wearableNode = null;
      final List<Node> nodes = nodesResult.getNodes();
      Timber.d("Search node list of size : %d", nodes.size());
      for (final Node node : nodes) {
        if (node.isNearby()) {
          Timber.d("Wearable node: %s %s", node.getDisplayName(), node.getId());
          wearableNode = node;
          break;
        }
      }

      if (wearableNode == null) {
        Timber.w("No wearable node was found");
        result = true;
      } else {
        Timber.d("Found a wearable node");
        result = false;
      }
    } else {
      Timber.e("Could not connect to Google APIs");
      result = true;
    }
    return result;
  }

  private void disconnectGoogleApiClient() {
    if (googleApiClient.isConnected()) {
      Timber.d("Disconnect Google Api Client");
      googleApiClient.disconnect();
    }
  }

  @Override public void register(@NonNull String tag, @Nullable SetCallback setCallback,
      @Nullable UnsetCallback unsetCallback) {
    Timber.w("Cannot monitor for state changes on Wearables");
    disconnectGoogleApiClient();
  }

  @Override public void unregister(@NonNull String tag) {
    Timber.w("Cannot monitor for state changes on Wearables");
    disconnectGoogleApiClient();
  }
}

@@ -688,16 +688,63 @@
 ontext;%0A
+import android.support.annotation.CheckResult;%0A
 import a
@@ -1545,375 +1545,79 @@
 %0A%0A  
-/**%0A   * Return true if stream should continue, false otherwise%0A   */%0A  @WorkerThread @Override public boolean is() %7B%0A    Timber.d(%22Check if wearable is connected%22);%0A    final ConnectionResult connectionResult = googleApiClient.blockingConnect(4, TimeUnit.SECONDS);%0A    boolean result;%0A    if (connectionResult.isSuccess()) %7B%0A      Timber.d(%22Connect Google APIs%22);%0A  
+@WorkerThread @CheckResult private boolean isWearableNodeConnected() %7B%0A
     
@@ -1664,26 +1664,24 @@
 desResult =%0A
-  
         Wear
@@ -1760,18 +1760,16 @@
 S);%0A    
-  
 Node wea
@@ -1786,18 +1786,16 @@
 = null;%0A
-  
     fina
@@ -1839,26 +1839,24 @@
 odes();%0A    
-  
 Timber.d(%22Se
@@ -1896,26 +1896,24 @@
 es.size());%0A
-  
     for (fin
@@ -1938,26 +1938,24 @@
 es) %7B%0A      
-  
 if (node.isN
@@ -1969,26 +1969,24 @@
 ) %7B%0A        
- 
- 
 Timber.d(%22We
@@ -2052,18 +2052,16 @@
         
-  
 wearable
@@ -2081,18 +2081,16 @@
         
-  
 break;%0A 
@@ -2094,22 +2094,18 @@
 ;%0A      
- 
- 
 %7D%0A
-  
     %7D%0A%0A 
@@ -2102,27 +2102,51 @@
 %7D%0A    %7D%0A%0A   
+ final boolean result;%0A
    
+ 
 if (wearable
@@ -2157,26 +2157,24 @@
  == null) %7B%0A
-  
       Timber
@@ -2205,34 +2205,32 @@
  found%22);%0A      
-  
 result = true;%0A 
@@ -2228,18 +2228,16 @@
 = true;%0A
-  
     %7D el
@@ -2239,34 +2239,32 @@
  %7D else %7B%0A      
-  
 Timber.d(%22Found 
@@ -2292,33 +2292,496 @@
     
-  result = false;%0A      %7D
+result = false;%0A    %7D%0A%0A    disconnectGoogleApiClient();%0A    return result;%0A  %7D%0A%0A  /**%0A   * Return true if stream should continue, false otherwise%0A   */%0A  @WorkerThread @Override public boolean is() %7B%0A    Timber.d(%22Check if wearable is connected%22);%0A    final ConnectionResult connectionResult = googleApiClient.blockingConnect(4, TimeUnit.SECONDS);%0A    boolean result;%0A    if (connectionResult.isSuccess()) %7B%0A      Timber.d(%22Connect Google APIs%22);%0A      result = isWearableNodeConnected();
 %0A   

Split up functions