/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *
 *  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.apps.forscience.whistlepunk;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.MenuItem;
import com.google.android.apps.forscience.whistlepunk.accounts.AppAccount;
import com.google.android.apps.forscience.whistlepunk.accounts.NonSignedInAccount;
import com.google.common.base.Preconditions;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/** Displays settings, dev options or about fragments as activity. */
public class SettingsActivity extends AppCompatActivity {

  private static final String KEY_ACCOUNT_KEY = "accountKey";
  private static final String KEY_TYPE = "type";
  private static final String KEY_TITLE = "title";

  // The different types of display.
  @IntDef({TYPE_SETTINGS, TYPE_DEV_OPTIONS, TYPE_ABOUT})
  @Retention(RetentionPolicy.SOURCE)
  public @interface SettingsType {}

  public static final int TYPE_SETTINGS = 0;
  public static final int TYPE_DEV_OPTIONS = 1;
  public static final int TYPE_ABOUT = 2;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    AppAccount appAccount = WhistlePunkApplication.getAccount(this, getIntent(), KEY_ACCOUNT_KEY);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
    setTitle(getIntent().getStringExtra(KEY_TITLE));
    int settingsType = getIntent().getIntExtra(KEY_TYPE, TYPE_SETTINGS);
    Fragment fragment;
    switch (settingsType) {
      case TYPE_ABOUT:
        fragment = SettingsFragment.newInstance(appAccount, TYPE_ABOUT);
        break;
      case TYPE_SETTINGS:
        fragment = SettingsFragment.newInstance(appAccount, TYPE_SETTINGS);
        break;
      case TYPE_DEV_OPTIONS:
        fragment = DevOptionsFragment.newInstance();
        break;
      default:
        throw new IllegalArgumentException("Unknown settings type " + settingsType);
    }
    getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      // TODO: use onNavigateUp instead?
      finish();
    }
    return super.onOptionsItemSelected(item);
  }

  public static Intent getLaunchIntent(
      Context context, CharSequence title, @SettingsType int type) {
    // Make sure this version is not used for TYPE_SETTINGS. We need the current AppAccount for
    // TYPE_SETTINGS.
    Preconditions.checkArgument(type != TYPE_SETTINGS);
    return getLaunchIntent(context, NonSignedInAccount.getInstance(context), title, type);
  }

  public static Intent getLaunchIntent(
      Context context, AppAccount appAccount, CharSequence title, @SettingsType int type) {
    Intent intent = new Intent(context, SettingsActivity.class);
    intent.putExtra(KEY_ACCOUNT_KEY, appAccount.getAccountKey());
    intent.putExtra(KEY_TYPE, type);
    intent.putExtra(KEY_TITLE, title);
    return intent;
  }
}

@@ -920,16 +920,41 @@
 tivity;%0A
+import android.util.Log;%0A
 import a
@@ -975,16 +975,16 @@
 nuItem;%0A
-
 import c
@@ -1125,32 +1125,210 @@
 ignedInAccount;%0A
+import com.google.android.apps.forscience.whistlepunk.filemetadata.ExperimentLibraryManager;%0Aimport com.google.android.apps.forscience.whistlepunk.filemetadata.LocalSyncManager;%0A
 import com.googl
@@ -1731,16 +1731,72 @@
 %22title%22;
+%0A  private static final String TAG = %22SettingsActivity%22;
 %0A%0A  // T
@@ -2347,16 +2347,51 @@
 NT_KEY);
+%0A    logExperimentInfo(appAccount);
 %0A%0A    Ac
@@ -4163,24 +4163,24 @@
 LE, title);%0A
-
     return i
@@ -4189,11 +4189,775 @@
 ent;%0A  %7D
+%0A%0A  // Added for b/129700680%0A  private void logExperimentInfo(AppAccount account) %7B%0A    if (Log.isLoggable(TAG, Log.INFO)) %7B%0A      LocalSyncManager lsm = AppSingleton.getInstance(this).getLocalSyncManager(account);%0A      ExperimentLibraryManager elm =%0A          AppSingleton.getInstance(this).getExperimentLibraryManager(account);%0A%0A      int expCount = 0;%0A      int delCount = 0;%0A      int syncCount = 0;%0A      for (String id : elm.getKnownExperiments()) %7B%0A        expCount++;%0A        if (elm.isDeleted(id)) %7B%0A          delCount++;%0A        %7D%0A        if (lsm.hasExperiment(id)) %7B%0A          syncCount++;%0A        %7D%0A      %7D%0A      Log.i(TAG, %22Experiments: %22 + expCount);%0A      Log.i(TAG, %22Deleted: %22 + delCount);%0A      Log.i(TAG, %22SyncManager: %22 + syncCount);%0A    %7D%0A  %7D
 %0A%7D%0A

Add logging of count of experiments in ExperimentLibraryManager and LocalSyncManager.