/*
 * Copyright 2014-present Facebook, 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.facebook.buck.core.rules.knowntypes;

import com.facebook.buck.config.BuckConfig;
import com.facebook.buck.config.FakeBuckConfig;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.plugin.impl.BuckPluginManagerFactory;
import com.facebook.buck.sandbox.SandboxExecutionStrategyFactory;
import com.facebook.buck.sandbox.TestSandboxExecutionStrategyFactory;
import com.facebook.buck.toolchain.ToolchainProvider;
import com.facebook.buck.util.FakeProcess;
import com.facebook.buck.util.FakeProcessExecutor;
import com.facebook.buck.util.ProcessExecutor;
import com.facebook.buck.util.ProcessExecutorParams;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.pf4j.PluginManager;

public final class KnownBuildRuleTypesTestUtil {

  private KnownBuildRuleTypesTestUtil() {
    // Utility class.
  }

  private static final ProcessExecutorParams XCODE_SELECT_PARAMS =
      ProcessExecutorParams.builder()
          .setCommand(ImmutableList.of("xcode-select", "--print-path"))
          .build();
  private static final FakeProcess XCODE_SELECT_PROCESS = new FakeProcess(0, "/path/to/xcode", "");

  private static final ImmutableMap<String, String> PYTHONS =
      ImmutableMap.of(
          "python", "2.6",
          "python2", "2.6",
          "python3", "3.5");

  protected static ImmutableMap<ProcessExecutorParams, FakeProcess> getPythonProcessMap(
      List<String> paths) {
    Set<String> uniquePaths = new HashSet<>(paths);
    ImmutableMap.Builder<ProcessExecutorParams, FakeProcess> processMap = ImmutableMap.builder();
    for (Map.Entry<String, String> python : PYTHONS.entrySet()) {
      for (String path : uniquePaths) {
        for (String extension : new String[] {"", ".exe", ".EXE"}) {
          processMap.put(
              ProcessExecutorParams.builder()
                  .setCommand(
                      ImmutableList.of(path + File.separator + python.getKey() + extension, "-"))
                  .build(),
              new FakeProcess(0, "CPython " + python.getValue(), ""));
        }
      }
    }
    return processMap.build();
  }

  @VisibleForTesting
  public static List<String> getPaths(ImmutableMap<String, String> environemnt) {
    String pathEnv = environemnt.get("PATH");
    if (pathEnv == null) {
      return Collections.emptyList();
    }

    return Arrays.asList(pathEnv.split(File.pathSeparator));
  }

  public static KnownBuildRuleTypes getDefaultKnownBuildRuleTypes(
      ProjectFilesystem filesystem,
      ToolchainProvider toolchainProvider,
      ImmutableMap<String, String> environment)
      throws InterruptedException, IOException {
    BuckConfig config = FakeBuckConfig.builder().setFilesystem(filesystem).build();
    List<String> paths = getPaths(environment);
    ProcessExecutor executor =
        new FakeProcessExecutor(
            ImmutableMap.<ProcessExecutorParams, FakeProcess>builder()
                .put(XCODE_SELECT_PARAMS, XCODE_SELECT_PROCESS)
                .putAll(getPythonProcessMap(paths))
                .build());

    return createInstance(config, toolchainProvider, executor);
  }

  @VisibleForTesting
  static KnownBuildRuleTypes createInstance(
      BuckConfig config, ToolchainProvider toolchainProvider, ProcessExecutor processExecutor) {

    PluginManager pluginManager = BuckPluginManagerFactory.createPluginManager();

    SandboxExecutionStrategyFactory sandboxExecutionStrategyFactory =
        new TestSandboxExecutionStrategyFactory();

    return KnownBuildRuleTypes.createInstance(
        config, processExecutor, toolchainProvider, pluginManager, sandboxExecutionStrategyFactory);
  }
}

@@ -698,114 +698,8 @@
 ig;%0A
-import com.facebook.buck.config.FakeBuckConfig;%0Aimport com.facebook.buck.io.filesystem.ProjectFilesystem;%0A
 impo
@@ -749,32 +749,32 @@
 ManagerFactory;%0A
+
 import com.faceb
@@ -994,59 +994,8 @@
 ss;%0A
-import com.facebook.buck.util.FakeProcessExecutor;%0A
 impo
@@ -1266,36 +1266,8 @@
 le;%0A
-import java.io.IOException;%0A
 impo
@@ -1565,306 +1565,8 @@
  %7D%0A%0A
-  private static final ProcessExecutorParams XCODE_SELECT_PARAMS =%0A      ProcessExecutorParams.builder()%0A          .setCommand(ImmutableList.of(%22xcode-select%22, %22--print-path%22))%0A          .build();%0A  private static final FakeProcess XCODE_SELECT_PROCESS = new FakeProcess(0, %22/path/to/xcode%22, %22%22);%0A%0A
   pr
@@ -2754,16 +2754,16 @@
 %0A    %7D%0A%0A
+
     retu
@@ -2824,731 +2824,8 @@
  %7D%0A%0A
-  public static KnownBuildRuleTypes getDefaultKnownBuildRuleTypes(%0A      ProjectFilesystem filesystem,%0A      ToolchainProvider toolchainProvider,%0A      ImmutableMap%3CString, String%3E environment)%0A      throws InterruptedException, IOException %7B%0A    BuckConfig config = FakeBuckConfig.builder().setFilesystem(filesystem).build();%0A    List%3CString%3E paths = getPaths(environment);%0A    ProcessExecutor executor =%0A        new FakeProcessExecutor(%0A            ImmutableMap.%3CProcessExecutorParams, FakeProcess%3Ebuilder()%0A                .put(XCODE_SELECT_PARAMS, XCODE_SELECT_PROCESS)%0A                .putAll(getPythonProcessMap(paths))%0A                .build());%0A%0A    return createInstance(config, toolchainProvider, executor);%0A  %7D%0A%0A
   @V

Remove unused KnownBuildRuleTypesTestUtil.getDefaultKnownBuildRuleTypes