package com.github.wovnio.wovnjava;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class HtmlChecker {
    public boolean canTranslateContentType(String type) {
        return type == null || type.toLowerCase().contains("html");
    }

    public boolean canTranslatePath(String path) {
        if (path == null) {
            return true;
        }

        // Reduce strings for performance and keep a simple case
        path = path.replaceFirst("^.*/", "/"); // strip directries
        path = path.replaceFirst("[?#].*$", ""); // strip query or/and hash
        path = path.toLowerCase();

        return !IMAGE_FILE_PATTERN.matcher(path).find()
            && !AUDIO_FILE_PATTERN.matcher(path).find()
            && !VIDEO_FILE_PATTERN.matcher(path).find()
            && !DOC_FILE_PATTERN.matcher(path).find();
    }

    public boolean canTranslateContent(String html) {
        if (html == null) {
            return false;
        }

        String head = getHead(html).toLowerCase();
        return isHtml(head) && !isAmp(head);
    }

    private boolean isAmp(String head) {
        head = REMOVE_QUOTED_ATTRIBUTES.matcher(head).replaceAll("");
        Matcher m = HTML_TAG_ATTRIBUTE_PATTERN.matcher(head);
        if (!m.find()) {
            return false;
        }
        String attributes = m.group();
        System.out.println(attributes);
        return attributes.contains(" ⚡")
            || attributes.contains(" amp");
    }

    private boolean isHtml(String head) {
        return head.contains("<?xml")
            || head.contains("<!doctype")
            || head.contains("<html")
            || head.contains("<xhtml");
    }

    private String getHead(String html) {
        return html.substring(0, Math.min(html.length(), BUFFER_SIZE));
    }

    private final int BUFFER_SIZE = 256;

    private final Pattern HTML_TAG_ATTRIBUTE_PATTERN = Pattern.compile("<html([^>]+)>");
    private final Pattern REMOVE_QUOTED_ATTRIBUTES = Pattern.compile("(?:\"[^\"]*\")|(?:'[^']*')");

    // The pattern come from WOVN.php/src/wovnio/wovnphp/Utils.php
    private final Pattern IMAGE_FILE_PATTERN = Pattern.compile("(\\.((?!jp$)jpe?g?|bmp|gif|png|btif|tiff?|psd|djvu?|xif|wbmp|webp|p(n|b|g|p)m|rgb|tga|x(b|p)m|xwd|pic|ico|fh(c|4|5|7)?|xif|f(bs|px|st)))$");
    private final Pattern AUDIO_FILE_PATTERN = Pattern.compile("(\\.(mp(3|2)|m(p?2|3|p?4|pg)a|midi?|kar|rmi|web(m|a)|aif(f?|c)|w(ma|av|ax)|m(ka|3u)|sil|s3m|og(a|g)|uvv?a))$");
    private final Pattern VIDEO_FILE_PATTERN = Pattern.compile("(\\.(m(x|4)u|fl(i|v)|3g(p|2)|jp(gv|g?m)|mp(4v?|g4|e?g)|m(1|2)v|ogv|m(ov|ng)|qt|uvv?(h|m|p|s|v)|dvb|mk(v|3d|s)|f4v|as(x|f)|w(m(v|x)|vx)))$");
    private final Pattern DOC_FILE_PATTERN = Pattern.compile("(\\.(zip|tar|ez|aw|atom(cat|svc)?|(cc)?xa?ml|cdmi(a|c|d|o|q)?|epub|g(ml|px|xf)|jar|js|ser|class|json(ml)?|do(c|t)m?|xps|pp(a|tx?|s)m?|potm?|sldm|mp(p|t)|bin|dms|lrf|mar|so|dist|distz|m?pkg|bpk|dump|rtf|tfi|pdf|pgp|apk|o(t|d)(b|c|ft?|g|h|i|p|s|t)))$");
}

@@ -1336,48 +1336,8 @@
 ();%0A
-        System.out.println(attributes);%0A
     

Delete debug print