document.getElementById("submitComment").addEventListener("click", submitComment);
document.getElementById("clearComment").addEventListener("click", clearComment);
document.getElementById("displayCommentsCheckbox").addEventListener("change", updateDisplayComment);

function submitComment(e) {
    e.preventDefault();
    const screenshot = document.getElementById('commentScreenshot').value;
    if (screenshot) {
        chrome.runtime.sendMessage({
            kind: "takeScreenshot"
        });
    }
    
    const type = document.getElementById('commentType').value;
    const value = document.getElementById('commentDescription').value;
    chrome.runtime.sendMessage({
        kind: "pushComment",
        type,
        value
    }, {}, response => {
        if (response !== undefined) {
            document.getElementById("commentSuccessul").style.display = 'block'
            document.getElementById('commentDescription').value = "";
        } else {
            console.log('Error sendMessage')
            if (chrome.runtime.lastError && chrome.runtime.lastError.message) {
                console.log('lastError:',chrome.runtime.lastError.message);
            }
        }
    });

}

function clearComment(e) {
    e.preventDefault();
    document.getElementById('commentDescription').value = "";
}

function updateDisplayComment(e) {
    const isTabScriptDisplayingComments = e.target.checked
    chrome.runtime.sendMessage({
        kind: "setIsTabScriptDiplayingComments",
        isTabScriptDisplayingComments
    }, {}, () => {
    });
}
