// Copyright (C) 2015 The Android Open Source Project
//
// 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.googlesource.gerrit.plugins.gitiles;

import com.google.common.base.MoreObjects;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.common.WebLinkInfo;
import com.google.gerrit.extensions.webui.BranchWebLink;
import com.google.gerrit.extensions.webui.FileHistoryWebLink;
import com.google.gerrit.extensions.webui.FileWebLink;
import com.google.gerrit.extensions.webui.ParentWebLink;
import com.google.gerrit.extensions.webui.PatchSetWebLink;
import com.google.gerrit.extensions.webui.ProjectWebLink;
import com.google.gerrit.extensions.webui.TagWebLink;
import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.lib.Config;

@Singleton
public class GitilesWeblinks
    implements BranchWebLink,
        FileWebLink,
        PatchSetWebLink,
        ProjectWebLink,
        FileHistoryWebLink,
        ParentWebLink,
        TagWebLink {
  private final String name;
  private final String baseUrl;
  private final String target;

  @Inject
  public GitilesWeblinks(@PluginName String pluginName, PluginConfigFactory configFactory) {
    Config config = configFactory.getGlobalPluginConfig("gitiles");
    name = MoreObjects.firstNonNull(config.getString("gerrit", null, "linkname"), "browse");
    baseUrl = "plugins/" + pluginName;

    target = MoreObjects.firstNonNull(config.getString("gerrit", null, "target"), Target.BLANK);
  }

  @Override
  public WebLinkInfo getProjectWeblink(String projectName) {
    return new WebLinkInfo(name, null, String.format("%s/%s", baseUrl, projectName), target);
  }

  @Override
  public WebLinkInfo getPatchSetWebLink(String projectName, String commit) {
    return new WebLinkInfo(
        name, null, String.format("%s/%s/+/%s", baseUrl, projectName, commit), target);
  }

  @Override
  public WebLinkInfo getParentWebLink(String projectName, String commit) {
    return new WebLinkInfo(
        name, null, String.format("%s/%s/+/%s", baseUrl, projectName, commit), target);
  }

  @Override
  public WebLinkInfo getFileWebLink(String projectName, String revision, String fileName) {
    return new WebLinkInfo(
        name,
        null,
        String.format("%s/%s/+/%s/%s", baseUrl, projectName, revision, fileName),
        target);
  }

  @Override
  public WebLinkInfo getBranchWebLink(String projectName, String branchName) {
    return new WebLinkInfo(
        name, null, String.format("%s/%s/+/%s", baseUrl, projectName, branchName), target);
  }

  @Override
  public WebLinkInfo getTagWebLink(String projectName, String tagName) {
    return new WebLinkInfo(
        name, null, String.format("%s/%s/+/%s", baseUrl, projectName, tagName), target);
  }

  @Override
  public WebLinkInfo getFileHistoryWebLink(String projectName, String revision, String fileName) {
    return new WebLinkInfo(
        name,
        null,
        String.format("%s/%s/+log/%s/%s", baseUrl, projectName, revision, fileName),
        target);
  }
}

@@ -695,16 +695,58 @@
 bjects;%0A
+import com.google.gerrit.common.Nullable;%0A
 import c
@@ -797,16 +797,16 @@
 inName;%0A
-
 import c
@@ -1247,32 +1247,88 @@
 bui.TagWebLink;%0A
+import com.google.gerrit.server.config.CanonicalWebUrl;%0A
 import com.googl
@@ -1440,16 +1440,76 @@
 gleton;%0A
+import java.net.MalformedURLException;%0Aimport java.net.URL;%0A
 import o
@@ -1877,16 +1877,23 @@
 eblinks(
+%0A      
 @PluginN
@@ -1918,45 +1918,309 @@
 ame,
- PluginConfigFactory configFactory) %7B
+%0A      @Nullable @CanonicalWebUrl String gerritUrl,%0A      PluginConfigFactory configFactory)%0A      throws MalformedURLException %7B%0A%0A    String baseGerritUrl;%0A    if (gerritUrl != null) %7B%0A      URL u = new URL(gerritUrl);%0A      baseGerritUrl = u.getPath();%0A    %7D else %7B%0A      baseGerritUrl = %22/%22;%0A    %7D%0A
 %0A   
@@ -2373,24 +2373,24 @@
  %22browse%22);%0A
-
     baseUrl 
@@ -2390,16 +2390,32 @@
 aseUrl =
+ baseGerritUrl +
  %22plugin

Fix URI for new UI