// Copyright 2018 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.devtools.build.lib.runtime;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.Root;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import net.starlark.java.syntax.Location;

/** Converts {@link Location} objects to a human-friendly string. */
public final class LocationPrinter {
  private final boolean attemptToPrintRelativePaths;
  @Nullable private final PathFragment workspacePathFragment;
  private final AtomicReference<ImmutableList<Root>> packagePathRootsRef =
      new AtomicReference<>(ImmutableList.of());

  public LocationPrinter(
      boolean attemptToPrintRelativePaths, @Nullable PathFragment workspacePathFragment) {
    this.attemptToPrintRelativePaths = attemptToPrintRelativePaths;
    this.workspacePathFragment = workspacePathFragment;
  }

  public void packageLocatorCreated(PathPackageLocator packageLocator) {
    packagePathRootsRef.set(packageLocator.getPathEntries());
  }

  public String getLocationString(Location location) {
    return attemptToPrintRelativePaths
        ? getRelativeLocationString(location, workspacePathFragment, packagePathRootsRef.get())
        : location.toString();
  }

  @VisibleForTesting
  static String getRelativeLocationString(
      Location location,
      @Nullable PathFragment workspacePathFragment,
      ImmutableList<Root> packagePathRoots) {
    PathFragment relativePathToUse = null;
    PathFragment locationPathFragment = PathFragment.create(location.file());
    if (locationPathFragment.isAbsolute()) {
      if (workspacePathFragment != null && locationPathFragment.startsWith(workspacePathFragment)) {
        relativePathToUse = locationPathFragment.relativeTo(workspacePathFragment);
      } else {
        for (Root packagePathRoot : packagePathRoots) {
          if (packagePathRoot.contains(locationPathFragment)) {
            relativePathToUse = packagePathRoot.relativize(locationPathFragment);
            break;
          }
        }
      }
    }

    StringBuilder b = new StringBuilder();
    b.append(relativePathToUse != null ? relativePathToUse : locationPathFragment);
    int line = location.line();
    if (line != 0) {
      b.append(':').append(line);
      int column = location.column();
      if (column != 0) {
        b.append(':').append(column);
      }
    }
    return b.toString();
  }
}
