/*
 * Copyright 2016-2018 Talsma ICT
 *
 * 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 nl.talsmasoftware.umldoclet.html;

import nl.talsmasoftware.umldoclet.configuration.Configuration;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;
import static nl.talsmasoftware.umldoclet.logging.Message.DEBUG_POSTPROCESSING_FILE;
import static nl.talsmasoftware.umldoclet.logging.Message.DEBUG_SKIPPING_FILE;

/**
 * Abstraction for a single HTML file generated by the Standard doclet.
 *
 * @author Sjoerd Talsma
 */
final class HtmlFile {

    private final Configuration config;
    private final Path path;

    HtmlFile(Configuration config, Path path) {
        this.config = requireNonNull(config, "Configuration is <null>.");
        this.path = requireNonNull(path, "HTML file is <null>.");
    }

    static boolean matches(Path path) {
        return Optional.ofNullable(path).map(Path::toFile)
                .filter(File::isFile).filter(File::canRead)
                .map(File::getName).filter(name -> name.endsWith(".html"))
                .isPresent();
    }

    boolean process(Collection<Diagram> diagrams) {
        return diagrams.stream()
                .map(diagram -> diagram.relativePathFrom(path))
                .filter(Optional::isPresent).map(Optional::get)
                .findFirst()
                .map(this::process)
                .orElseGet(this::skip);
    }

    private boolean skip() {
        config.logger().debug(DEBUG_SKIPPING_FILE, path);
        return true;
    }

    private boolean process(String relativeDiagramPath) {
        try {
            // TODO actually postprocess the HTML
            Stream<String> lines = readHtml();
            return true;
        } catch (IOException ioe) {
            throw new IllegalStateException("I/O error processing " + path + ": " + ioe.getMessage(), ioe);
        }
    }

    private Stream<String> readHtml() throws IOException {
        config.logger().debug(DEBUG_POSTPROCESSING_FILE, path);
        return Files.lines(path, config.htmlCharset());
    }

}

@@ -2305,92 +2305,117 @@
     
-// TODO actually postprocess the HTML%0A            Stream%3CString%3E lines = readHtml();
+Stream%3CString%3E lines = readHtml();%0A            // TODO Stream to a new file and change %3Chr%3E to %3Chr%3E%5Cn%3Cimg...%3E
 %0A   

Update TODO so I don't forget where I am