/*
 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


/*
 * @test
 * @bug 8267574
 * @summary check stylesheet names against HtmlStyle
 * @modules jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.markup
 *          jdk.javadoc/jdk.javadoc.internal.doclets.toolkit.resources:open
 */

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;

public class CheckStylesheetClasses {
    public static void main(String... args) throws Exception {
        CheckStylesheetClasses c = new CheckStylesheetClasses();
        c.run();
    }

    void run() throws Exception {
        Set<String> htmlStyleNames = getHtmlStyleNames();
        Set<String> styleSheetNames = getStylesheetNames();

        System.err.println("found " + htmlStyleNames.size() + " names in HtmlStyle");
        System.err.println("found " + styleSheetNames.size() + " names in stylesheet");

        // Consider removing "known" or "expected" differences from each of the two sets,
        // before the comparison, such as: all the *-page entries in HtmlStyle, or "png"
        // in the stylesheet.
        // There's a gray area of entries like "striped", "borderless", "plain" in the
        // stylesheet, which are not generated by the doclet but which are provided for
        // use in user doc-comments. If we think they are standard, they could be documented
        // in HtmlStyle;  if we think they are for JDK only, they could be put in a JDK-specific
        // stylesheet.

        boolean ok = check(htmlStyleNames, "HtmlStyle", styleSheetNames, "stylesheet")
                    & check(styleSheetNames, "stylesheet", htmlStyleNames, "HtmlStyle");

        if (!ok) {
            throw new Exception("differences found");
        }
    }

    boolean check(Set<String> s1, String l1, Set<String> s2, String l2) {
        boolean equal = true;
        for (String s : s1) {
            if (!s2.contains(s)) {
                System.err.println("In " + l1 + " but not " + l2 + ": " + s);
                equal = false;
            }
        }
        return equal;
    }

    Set<String> getHtmlStyleNames() {
        return Arrays.stream(HtmlStyle.values())
                .map(s -> s.cssName())
                .collect(Collectors.toCollection(TreeSet::new));
    }

    Set<String> getStylesheetNames() throws IOException {
        Set<String> names = new TreeSet<>();
        String stylesheet = "/jdk/javadoc/internal/doclets/toolkit/resources/stylesheet.css";
        URL url = HtmlStyle.class.getResource(stylesheet);
        readStylesheet(url, names);
        return names;
    }

    private void readStylesheet(URL resource, Set<String> names) throws IOException {
        try (InputStream in = resource.openStream()) {
            if (in == null) {
                throw new AssertionError("Cannot find or access resource " + resource);
            }
            String s = new String(in.readAllBytes());
            Pattern p = Pattern.compile("(?i)(\\s|([a-z][a-z0-9-]*))\\.(?<name>[a-z0-9-]+)\\b");
            Matcher m = p.matcher(s);
            while (m.find()) {
                names.add(m.group("name"));
            }
        }
    }
}