/*
 * Copyright 2017 Google Inc.
 *
 * 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.google.android.libraries.remixer.storage;

import android.content.Context;
import com.google.android.libraries.remixer.Remixer;
import com.google.android.libraries.remixer.Variable;
import com.google.android.libraries.remixer.sync.LocalValueSyncing;
import com.google.android.libraries.remixer.sync.SynchronizationMechanism;
import java.lang.ref.WeakReference;

/**
 * A {@link SynchronizationMechanism} that syncs up to a firebase Remote Controller.
 *
 * <p>This synchronization mechanism assumes that the local host is the source of truth, so it does
 * very little in terms of conflict resolution.
 */
public class FirebaseRemoteControllerSyncer extends LocalValueSyncing {

  WeakReference<Object> context;

  public FirebaseRemoteControllerSyncer(Context context) {
    super();
  }

  /**
   * Syncs a variable up to the remote controller.
   */
  private void syncVariableToRemoteController (Variable variable) {
    // TODO(miguely): write controller sync logic
  }

  /**
   * Clears all the data in a remote
   */
  private void clearVariablesInRemoteController() {
    // TODO(miguely): write controller sync logic
  }

  @Override
  public void onAddingVariable(Variable variable) {
    super.onAddingVariable(variable);
    syncVariableToRemoteController(variable);
  }

  @Override
  public void onValueChanged(Variable variable) {
    super.onValueChanged(variable);
    syncVariableToRemoteController(variable);
  }

  @Override
  public void onContextChanged(Object currentContext) {
    super.onContextChanged(currentContext);
    if ((context == null && currentContext != null) ||
        (context != null && currentContext != context.get())) {
      context = new WeakReference<Object>(context);
      clearVariablesInRemoteController();
      for (Variable<?> var : Remixer.getInstance().getVariablesWithContext(currentContext)) {
        syncVariableToRemoteController(var);
      }
    }
  }

  @Override
  public void onContextRemoved(Object currentContext) {
    super.onContextRemoved(currentContext);
    if (context != null && context.get() == currentContext) {
      clearVariablesInRemoteController();
    }
  }
}

@@ -1203,20 +1203,238 @@
 lution.%0A
+ *%0A * %3Cp%3EThis implementation currently ignores bringing new values in from the remote controller, that%0A * will be done in firebase callbacks in a future PR, so it is a different one that introduces said%0A * dependency.%0A
  */%0A
-
 public c
@@ -1576,23 +1576,8 @@
 cer(
-Context context
 ) %7B%0A
@@ -1651,16 +1651,369 @@
 roller.%0A
+   *%0A   * %3Cp%3ESince the local app is the source of truth, this ignores any differences there may be%0A   * between the local data and the remote data and just rewrites any remote data. This should not%0A   * happen in practice though, since variables in remote controllers are tied to a single instance%0A   * of the app (one specific device running the app).%0A
    */%0A  
@@ -2135,24 +2135,24 @@
 %0A  %7D%0A%0A  /**%0A
-
    * Clears 
@@ -2175,16 +2175,17 @@
 a remote
+.
 %0A   */%0A 

Improve documentations for firebase remote