package hudson.plugins.git.browser;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.plugins.git.GitRepositoryBrowser;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;

import hudson.scm.browsers.QueryBuilder;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Collection;

import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

/**
 * Git Browser URLs
 */
public class GithubWeb extends GitRepositoryBrowser {

    private static final long serialVersionUID = 1L;
    private final URL url;

    @DataBoundConstructor
    public GithubWeb(String url) throws MalformedURLException {
        this.url = normalizeToEndWithSlash(new URL(url));
    }

    public URL getUrl() {
        return url;
    }

    @Override
    public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
        return new URL(url, url.getPath()+"commit/" + changeSet.getId().toString());
    }

    private QueryBuilder param() {
        return new QueryBuilder(url.getQuery());
    }

    /**
     * Creates a link to the file diff.
     * http://[GitWeb URL]?a=blobdiff;f=[path];fp=[path];h=[dst];hp=[src];hb=[commit];hpb=[parent commit]
     *
     * @param path affected file path
     * @return diff link
     * @throws IOException
     */
    @Override
    public URL getDiffLink(Path path) throws IOException {
        if (path.getEditType() != EditType.EDIT || path.getSrc() == null || path.getDst() == null
                || path.getChangeSet().getParentCommit() == null) {
            return null;
        }
        final GitChangeSet changeSet = path.getChangeSet();
        final Collection<String> affectedPaths = changeSet.getAffectedPaths();
        int i = 0;
        final String pathAsString = path.getPath();
        for (String affectedPath : affectedPaths) {
            if (affectedPath.equals(pathAsString)) {
                return new URL(url, url.getPath()+"commit/" + changeSet.getId().toString() + "#diff-" + String.valueOf(i));
            } else {
                i++;
            }
        }
        return null;
    }

    /**
     * Creates a link to the file.
     * http://[GitHib URL]/blob/573670a3bb1f3b939e87f1dee3e99b6bfe281fcb/src/main/java/hudson/plugins/git/browser/GithubWeb.java
     * http://[GitHub URL]?a=blob;f=[path];h=[dst, or src for deleted files];hb=[commit]
     * @param path file
     * @return file link
     * @throws IOException
     */
    @Override
    public URL getFileLink(Path path) throws IOException {
        String h = (path.getDst() != null) ? path.getDst() : path.getSrc();
        final String spec = "blob/" + path.getChangeSet().getId() + "/" + path.getPath();
//        String spec2 = param().add("a=blob").add("f=" + path.getPath())
//                             .add("h=" + h).add("hb=" + path.getChangeSet().getId()).toString();
        return new URL(url, url.getPath()+spec);
    }

    @Extension
    public static class GithubWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
        public String getDisplayName() {
            return "githubweb";
        }

        @Override
		public GithubWeb newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
			return req.bindParameters(GithubWeb.class, "githubweb.");
		}
	}

}

@@ -290,49 +290,8 @@
 r;%0A%0A
-import hudson.scm.browsers.QueryBuilder;%0A
 impo
@@ -306,32 +306,32 @@
 io.IOException;%0A
+
 import java.net.
@@ -1087,99 +1087,8 @@
  %7D%0A%0A
-    private QueryBuilder param() %7B%0A        return new QueryBuilder(url.getQuery());%0A    %7D%0A%0A
     
@@ -2331,63 +2331,47 @@
    *
- http://%5BGitHub URL%5D?a=blob;f=%5Bpath%5D;h=%5Bdst, or src for
+%0A     * @todo Do not know how to handle
  del
@@ -2380,29 +2380,17 @@
 ed files
-%5D;hb=%5Bcommit%5D
+.
 %0A     * 
@@ -2543,84 +2543,8 @@
 n %7B%0A
-        String h = (path.getDst() != null) ? path.getDst() : path.getSrc();%0A
     
@@ -2633,181 +2633,8 @@
 ();%0A
-//        String spec2 = param().add(%22a=blob%22).add(%22f=%22 + path.getPath())%0A//                             .add(%22h=%22 + h).add(%22hb=%22 + path.getChangeSet().getId()).toString();%0A
     

Clean up the browser a bit