// Copyright 2014 Palantir Technologies
//
// 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.palantir.stash.stashbot.webpanel;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.util.Map;

import org.slf4j.Logger;

import com.atlassian.plugin.web.model.WebPanel;
import com.atlassian.stash.content.Changeset;
import com.atlassian.stash.repository.Repository;
import com.palantir.stash.stashbot.config.ConfigurationPersistenceManager;
import com.palantir.stash.stashbot.config.RepositoryConfiguration;
import com.palantir.stash.stashbot.jobtemplate.JobType;
import com.palantir.stash.stashbot.logger.PluginLoggerFactory;
import com.palantir.stash.stashbot.urlbuilder.StashbotUrlBuilder;

public class RetriggerLinkWebPanel implements WebPanel {

    private final ConfigurationPersistenceManager cpm;
    private final StashbotUrlBuilder ub;
    private final Logger log;

    public RetriggerLinkWebPanel(ConfigurationPersistenceManager cpm,
        StashbotUrlBuilder ub, PluginLoggerFactory lf) {
        this.cpm = cpm;
        this.ub = ub;
        this.log = lf.getLoggerForThis(this);
    }

    @Override
    public String getHtml(Map<String, Object> context) {
        Writer holdSomeText = new StringWriter();
        try {
            writeHtml(holdSomeText, context);
        } catch (IOException e) {
            log.error("Error occured rendering web panel", e);
            return "Error occured loading text";
        }
        return holdSomeText.toString();
    }

    @Override
    public void writeHtml(Writer writer, Map<String, Object> context)
        throws IOException {
        try {
            Repository repo = (Repository) context.get("repository");
            RepositoryConfiguration rc = cpm
                .getRepositoryConfigurationForRepository(repo);

            if (!rc.getCiEnabled()) {
                // No link
                return;
            }

            Changeset changeset = (Changeset) context.get("changeset");
            String url = ub.getJenkinsTriggerUrl(repo, JobType.VERIFY_COMMIT,
                changeset.getId(), null);

            writer.append("<a href=\"" + url + "\">Re-verify</a>");
        } catch (SQLException e) {
            throw new IOException(e);
        }
    }
}

@@ -2663,16 +2663,200 @@
  null);%0A
+            String pubUrl = ub.getJenkinsTriggerUrl(repo, JobType.PUBLISH,%0A                changeset.getId(), null);%0A            // TODO: add ?reason=%3CbuildRef%3E somehow to end of URLs?
 %0A       
@@ -2875,16 +2875,27 @@
 append(%22
+Trigger: ( 
 %3Ca href=
@@ -2914,12 +2914,9 @@
 %22%5C%22%3E
-Re-v
+V
 erif
@@ -2920,16 +2920,90 @@
 rify%3C/a%3E
+ %7C %22);%0A            writer.append(%22%3Ca href=%5C%22%22 + pubUrl + %22%5C%22%3EPublish%3C/a%3E )
 %22);%0A    

Add publish link