/*
 * Copyright 2014 the original author or authors.
 *
 * 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 org.gradle.api.reporting.dependencies.internal;

import com.googlecode.jatl.Html;
import org.gradle.api.Project;
import org.gradle.api.Transformer;
import org.gradle.reporting.HtmlPageBuilder;
import org.gradle.reporting.ReportRenderer;
import org.gradle.util.GradleVersion;

import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.Set;

public class ProjectsPageRenderer extends ReportRenderer<Set<Project>, HtmlPageBuilder<Writer>> {
    private final Transformer<String, Project> namingScheme;

    public ProjectsPageRenderer(Transformer<String, Project> namingScheme) {
        this.namingScheme = namingScheme;
    }

    @Override
    public void render(final Set<Project> projects, final HtmlPageBuilder<Writer> builder) throws IOException {
        final String baseCssLink = builder.requireResource(getClass().getResource("/org/gradle/reporting/base-style.css"));
        final String cssLink = builder.requireResource(getClass().getResource("/org/gradle/api/tasks/diagnostics/htmldependencyreport/style.css"));

        new Html(builder.getOutput()) {{
            html();
                head();
                    meta().httpEquiv("Content-Type").content("text/html; charset=utf-8");
                    meta().httpEquiv("x-ua-compatible").content("IE=edge");
                    link().rel("stylesheet").type("text/css").href(baseCssLink).end();
                    link().rel("stylesheet").type("text/css").href(cssLink).end();
                    title().text("Dependency reports").end();
                end();
                body();
                    div().id("content");
                        h1().text("Dependency Reports").end();
                        div().classAttr("tab");
                            table();
                                thead();
                                    tr();
                                        th().text("Project").end();
                                        th().text("Description").end();
                                    end();
                                end();
                                tbody();
                                for (Project project : projects) {
                                    tr();
                                        td().a().href(namingScheme.transform(project)).text(project.toString()).end().end();
                                        td().text(project.getDescription()).end();
                                    end();
                                }
                                end();
                            end();
                        end();
                        div().id("footer");
                            p();
                                text("Generated by ");
                                a().href("http://www.gradle.org").text(GradleVersion.current().toString()).end();
                                text(String.format(" at %s", builder.formatDate(new Date())));
                            end();
                        end();
                    end();
                end();
            endAll();
        }};
    }
}
