// Copyright 2016 Google Inc. All Rights Reserved.
package com.google.copybara.testing;

import com.google.common.base.StandardSystemProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.jimfs.Jimfs;
import com.google.copybara.GeneralOptions;
import com.google.copybara.Option;
import com.google.copybara.Options;
import com.google.copybara.WorkflowOptions;
import com.google.copybara.folder.FolderDestinationOptions;
import com.google.copybara.git.GerritOptions;
import com.google.copybara.git.GitOptions;
import com.google.copybara.testing.TestingModule.TestingOptions;
import com.google.copybara.util.console.Console;
import com.google.copybara.util.console.LogConsole;
import java.io.IOException;
import java.nio.file.FileSystems;

/**
 * Allows building complete and sane {@link Options} instances succinctly.
 */
public class OptionsBuilder {

  public GeneralOptions general =
      new GeneralOptions(Jimfs.newFileSystem(),
          /*verbose=*/true,
          LogConsole.readWriteConsole(System.in, System.out),
          /*skylark=*/
          /*validate=*/false,
          StandardSystemProperty.USER_DIR.value(),
          StandardSystemProperty.USER_HOME.value());

  public FolderDestinationOptions localDestination =
      new FolderDestinationOptions();
  public GitOptions git = new GitOptions();
  public GerritOptions gerrit = new GerritOptions();
  public WorkflowOptions workflowOptions = new WorkflowOptions(
      /*changeBaseline=*/null, /*lastRevision=*/ null, "default");

  public TestingOptions testingOptions = new TestingOptions();

  public final OptionsBuilder setWorkdirToRealTempDir() throws IOException {
    general = new GeneralOptions(FileSystems.getDefault(), /*verbose=*/true,
        LogConsole.readWriteConsole(System.in, System.out));
    return this;
  }

  public final OptionsBuilder setConsole(Console newConsole) {
    general = new GeneralOptions(general.getFileSystem(), general.isVerbose(), newConsole,
        general.isValidate(), general.getCwd().toString(),
        general.getHomeDir().toString());
    return this;
  }

  public final OptionsBuilder setCurrentWorkDir(String cwd) {
    general = new GeneralOptions(
        general.getFileSystem(), general.isVerbose(), general.console(),
        general.isValidate(), cwd,general.getHomeDir().toString());
    return this;
  }

  public final OptionsBuilder setHomeDir(String homeDir) {
    general = new GeneralOptions(
        general.getFileSystem(), general.isVerbose(), general.console(),
        general.isValidate(), general.getCwd().toString(), homeDir);
    return this;
  }

  public final OptionsBuilder setWorkflowName(String workflowName) {
    workflowOptions = new WorkflowOptions(
        workflowOptions.getChangeBaseline(), workflowOptions.getLastRevision(), workflowName);
    return this;
  }

  public final OptionsBuilder setChangeBaseline(String changeBaseline) {
    workflowOptions = new WorkflowOptions(
        changeBaseline, workflowOptions.getLastRevision(), workflowOptions.getChangeBaseline());
    return this;
  }

  public final OptionsBuilder setLastRevision(String lastRevision) {
    workflowOptions = new WorkflowOptions(
        workflowOptions.getChangeBaseline(), lastRevision, workflowOptions.getChangeBaseline());
    return this;
  }

  /**
   * Returns all options to include in the built {@link Options} instance. This can be overridden by
   * child classes, in which case it should also include the superclass' instances.
   */
  protected Iterable<Option> allOptions() {
    return ImmutableList
        .of(general, localDestination, git, gerrit, workflowOptions, testingOptions);
  }

  public final Options build() {
    return new Options(ImmutableList.copyOf(allOptions()));
  }
}

@@ -2104,267 +2104,8 @@
  %7D%0A%0A
-  public final OptionsBuilder setCurrentWorkDir(String cwd) %7B%0A    general = new GeneralOptions(%0A        general.getFileSystem(), general.isVerbose(), general.console(),%0A        general.isValidate(), cwd,general.getHomeDir().toString());%0A    return this;%0A  %7D%0A%0A
   pu
@@ -2776,37 +2776,35 @@
 wOptions.get
-ChangeBaselin
+WorkflowNam
 e());%0A    re
@@ -3009,29 +3009,27 @@
 ions.get
-ChangeBaselin
+WorkflowNam
 e());%0A  

Fix OptionsBuilder bug and delete unused code.