let isHighlight = localStorage.getItem('highlightChecked') !== 'false'; // default to true

function cancelBtnHandler() {
    cancelHighlighting();
    document.getElementById("text-content").removeEventListener("click", fillInWord, false);
    document.getElementById("text-content").removeEventListener("touchstart", fillInWord, false);
    document.getElementById("text-content").addEventListener("click", fillInWord2, false);
    document.getElementById("text-content").addEventListener("touchstart", fillInWord2, false);
}

function showBtnHandler() {
    document.getElementById("text-content").removeEventListener("click", fillInWord2, false);
    document.getElementById("text-content").removeEventListener("touchstart", fillInWord2, false);
    document.getElementById("text-content").addEventListener("click", fillInWord, false);
    document.getElementById("text-content").addEventListener("touchstart", fillInWord, false);
    highLight();
}

function getWord() {
    return window.getSelection ? window.getSelection() : document.selection.createRange().text;
}

function highLight() {
    if (!isHighlight) return;
    let word = (getWord() + "").trim();
    let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式,且适配不同的浏览器
    let pickedWords = document.getElementById("selected-words");  // words picked to the text area
    let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list
    let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value;
    const list = allWords.split(" "); // 将所有的生词放入一个list中
    if(word !== null && word !== "" && word !== " "){
        let articleContent_fb2 = articleContent;
        if(localStorage.getItem("nowWord").indexOf(word) !== -1){
            while(articleContent_fb2.toLowerCase().indexOf(word.toLowerCase()) !== -1){
                // 找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
                const index = articleContent_fb2.toLowerCase().indexOf(word.toLowerCase());
                word = articleContent_fb2.substring(index, index + word.length);
                articleContent_fb2 = articleContent_fb2.substring(index + word.length);    // 使用副本中list[i]之后的子串替换掉副本
                articleContent = articleContent.replace(new RegExp("<mark>"+word+"</mark>", "g"), word)
            }
            pickedWords.value = localStorage.getItem("nowWord").replace(word,"");
            document.getElementById("article").innerHTML = articleContent;
            return;
        }
    }
    let totalSet = new Set();
    for (let i = 0; i < list.length; ++i) {
        list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符
        list[i] = list[i].replace('|', "");
        list[i] = list[i].replace('?', "");
        if (list[i] != "" && !totalSet.has(list[i]) && "<mark>".indexOf(list[i]) === -1 && "</mark>".indexOf(list[i]) === -1) {
            // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。
            let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi")));
            let articleContent_fb = articleContent;  //文章副本
            if (matches.has("mark")) {
                // 优先处理单词为 "mark" 的情况
                totalSet = new Set(["mark", ...totalSet]);
            }
            while(articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){
                //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
                const index = articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase());
                list[i] = articleContent_fb.substring(index, index + list[i].length);
                articleContent_fb = articleContent_fb.substring(index + list[i].length);    // 使用副本中list[i]之后的子串替换掉副本
                articleContent = articleContent.replace(new RegExp("\\b"+list[i]+"\\b","g"),"<mark>" + list[i] + "</mark>");
            }
            totalSet = new Set([...totalSet, ...matches]);
        }
    }
    // 删除所有的mark标签,防止标签发生嵌套
    articleContent = articleContent.replace(/<(mark)[^>]*>/gi, "");
    articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, "");
    // 将文章中所有出现该单词word的地方改为:"<span class='highlighted'>" + word + "</span>"。
    for (let word of totalSet) {
        articleContent = articleContent.replace(new RegExp("\\b" + word + "\\b", "g"), "<span class='highlighted'>" + word + "</span>");
    }
    document.getElementById("article").innerHTML = articleContent;

}

function cancelHighlighting() {
    let articleContent = document.getElementById("article").innerHTML;
    articleContent = articleContent.replace(/<(mark)[^>]*>/gi, "");
    articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, "");
    document.getElementById("article").innerHTML = articleContent;
}

function fillInWord() {
    highLight();
}

function fillInWord2() {
    cancelHighlighting();
}

function toggleHighlighting() {
    if (isHighlight) {
        isHighlight = false;
        cancelHighlighting();
    } else {
        isHighlight = true;
        highLight();
    }
     localStorage.setItem('highlightChecked', isHighlight);
}

showBtnHandler();