/*
 * Copyright 2011 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.reporting;

import org.gradle.internal.html.SimpleHtmlWriter;
import org.gradle.util.GradleVersion;

import java.io.IOException;
import java.net.URL;
import java.util.Date;

public abstract class TabbedPageRenderer<T> extends ReportRenderer<T, HtmlPageBuilder<SimpleHtmlWriter>> {
    private static final URL BASE_STYLE_URL = TabbedPageRenderer.class.getResource("/org/gradle/reporting/base-style.css");
    private static final URL REPORT_JS_URL = TabbedPageRenderer.class.getResource("/org/gradle/reporting/report.js");

    private T model;

    protected T getModel() {
        return model;
    }

    protected abstract String getTitle();

    protected abstract ReportRenderer<T, SimpleHtmlWriter> getHeaderRenderer();

    protected abstract ReportRenderer<T, SimpleHtmlWriter> getContentRenderer();

    protected String getPageTitle() {
        return getTitle();
    }

    protected abstract URL getStyleUrl();

    @Override
    public void render(final T model, HtmlPageBuilder<SimpleHtmlWriter> builder) throws IOException {
        this.model = model;

        String title = getTitle();
        String baseStyleLink = builder.requireResource(BASE_STYLE_URL);
        String reportJsLink = builder.requireResource(REPORT_JS_URL);
        String styleLink = builder.requireResource(getStyleUrl());

        SimpleHtmlWriter htmlWriter = builder.getOutput();

        htmlWriter.startElement("head")
            .startElement("meta").attribute("http-equiv", "Content-Type").attribute("content", "text/html; charset=utf-8").endElement()
            .startElement("meta").attribute("http-equiv", "x-ua-compatible").attribute("content", "IE=edge").endElement()
            .startElement("title").characters(getPageTitle()).endElement()
            .startElement("link").attribute("href", baseStyleLink).attribute("rel", "stylesheet").attribute("type", "text/css").endElement()
            .startElement("link").attribute("href", styleLink).attribute("rel", "stylesheet").attribute("type", "text/css").endElement()
            .startElement("script").attribute("src", reportJsLink).attribute("type", "text/javascript").characters("").endElement() //html does not like <a name="..."/>
        .endElement();

        htmlWriter.startElement("body")
            .startElement("div").attribute("id", "content");
                if (!title.isEmpty()) {
                    htmlWriter.startElement("h1").characters(title).endElement();
                }

                getHeaderRenderer().render(model, htmlWriter);
                getContentRenderer().render(model, htmlWriter);

                htmlWriter.startElement("div").attribute("id", "footer")
                    .startElement("p")
                        .startElement("div")
                            .startElement("label")
                                .attribute("class", "hidden")
                                .attribute("id", "label-for-line-wrapping-toggle")
                                .attribute("for", "line-wrapping-toggle")
                                .characters("Wrap lines")
                                .startElement("input")
                                    .attribute("id", "line-wrapping-toggle")
                                    .attribute("type", "checkbox")
                                    .attribute("autocomplete", "off")
                                .endElement()
                            .endElement()
                        .endElement()
                    .characters("Generated by ")
                        .startElement("a").attribute("href", "https://www.gradle.org").characters("Gradle " + GradleVersion.current().getVersion()).endElement()
                        .characters(String.format(" at %s", builder.formatDate(new Date())))
                    .endElement()
                .endElement()
            .endElement()
        .endElement();
    }
}
