/*
 * Copyright 2016 The Bazel Authors. 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.idea.blaze.android.run.runner;

import com.android.tools.idea.run.ValidationError;
import com.android.tools.idea.run.editor.AndroidDebugger;
import com.android.tools.idea.run.editor.AndroidDebuggerState;
import com.android.tools.idea.run.editor.AndroidJavaDebugger;
import com.android.tools.ndk.run.editor.NativeAndroidDebuggerState;
import com.google.common.collect.ImmutableList;
import com.google.idea.blaze.android.cppimpl.debug.BlazeAutoAndroidDebugger;
import com.google.idea.blaze.android.run.state.DebuggerSettingsState;
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
import com.intellij.openapi.project.Project;
import java.util.List;
import javax.annotation.Nullable;
import org.jetbrains.android.facet.AndroidFacet;

/** Manages android debugger state for the run configurations. */
public final class BlazeAndroidRunConfigurationDebuggerManager {
  private final DebuggerSettingsState debuggerSettings;

  public BlazeAndroidRunConfigurationDebuggerManager(DebuggerSettingsState debuggerSettings) {
    this.debuggerSettings = debuggerSettings;
  }

  public List<ValidationError> validate(AndroidFacet facet) {
    // All of the AndroidDebuggerState classes implement a validate that
    // either does nothing or is specific to gradle so there is no point
    // in calling validate on our AndroidDebuggerState.
    return ImmutableList.of();
  }

  @Nullable
  AndroidDebugger getAndroidDebugger() {
    String debuggerID = getDebuggerID();

    // Note: AndroidDebugger.EP_NAME includes native debugger(s).
    for (AndroidDebugger androidDebugger : AndroidDebugger.EP_NAME.getExtensions()) {
      if (androidDebugger.getId().equals(debuggerID)) {
        return androidDebugger;
      }
    }
    return null;
  }

  @Nullable
  AndroidDebuggerState getAndroidDebuggerState(Project project) {
    AndroidDebugger debuggerToUse = getAndroidDebugger();
    if (debuggerToUse == null) {
      return null;
    }

    AndroidDebuggerState androidDebuggerState = debuggerToUse.createState();
    // Set our working directory to our workspace root for native debugging.
    if (androidDebuggerState instanceof NativeAndroidDebuggerState) {
      NativeAndroidDebuggerState nativeState = (NativeAndroidDebuggerState) androidDebuggerState;
      String workingDirPath = WorkspaceRoot.fromProject(project).directory().getPath();
      nativeState.setWorkingDir(workingDirPath);
    }
    return androidDebuggerState;
  }

  private String getDebuggerID() {
    return debuggerSettings.isNativeDebuggingEnabled()
        ? BlazeAutoAndroidDebugger.ID
        : AndroidJavaDebugger.ID;
  }
}

@@ -1274,16 +1274,61 @@
 roject;%0A
+import com.intellij.openapi.util.SystemInfo;%0A
 import j
@@ -2707,85 +2707,8 @@
 ();%0A
-    // Set our working directory to our workspace root for native debugging.%0A
     
@@ -2769,24 +2769,24 @@
 gerState) %7B%0A
+
       Native
@@ -2955,24 +2955,24 @@
 .getPath();%0A
-
       native
@@ -3007,16 +3007,568 @@
 irPath);
+%0A%0A      // b/151821788 %22/proc/self/cwd%22 used by linux built binaries isn't valid on MacOS.%0A      if (SystemInfo.isMac) %7B%0A        String sourceMapToWorkspaceRootCommand =%0A            %22settings set target.source-map /proc/self/cwd/ %22 + workingDirPath;%0A        ImmutableList%3CString%3E startupCommands =%0A            ImmutableList.%3CString%3Ebuilder()%0A                .addAll(nativeState.getUserStartupCommands())%0A                .add(sourceMapToWorkspaceRootCommand)%0A                .build();%0A        nativeState.setUserStartupCommands(startupCommands);%0A      %7D
 %0A    %7D%0A 

Set target.source-map to workspace directory for ASwB on Mac