/**
 * @fileOverview Vietnamese(VNIV5.0) keyboard driver.
 * @author <a href="mailto:drachels@drachels.com">AL Rachels</a>
 * @version 5.0
 * @since 20241026
 */

/**
 * Check for combined character.
 * @param {string} chr The combined character.
 * @returns {string} The character.
 */
function isCombined(chr) {
    return false;
}

/**
 * Process keyup for combined character.
 * @param {string} e The combined character.
 * @returns {bolean} The result.
 */
function keyupCombined(e) {
    return false;
}

/**
 * Process keyupFirst.
 * @param {string} event Type of event.
 * @returns {bolean} The event.
 */
function keyupFirst(event) {
    return false;
}

/**
 * Check for character typed so flags can be set.
 * @param {string} ltr The current letter.
 */
function keyboardElement(ltr) {
    this.chr = ltr.toLowerCase();
    this.shiftleft = false;
    this.shiftright = false;
    this.alt = false;
    this.accent = false;

        // Phpcs:ignore
    if (ltr.match(/[ƯƠÔYUIOP|HJKL:~_+"NM<>?]/)) {
        this.shiftleft = true;
        this.alt = false;
        this.accent = false;
        // Phpcs:ignore
    } else if (ltr.match(/[ĂÂÊĐQWERTASDFGZXCVB]/)) {
        this.shiftright = true;
        this.alt = false;
        this.accent = false;

    // Phpcs:ignore
    } else if (ltr.match(/[!@#$%]/i)) {
        this.shiftright = true;
        this.alt = true;
        // Phpcs:ignore
    } else if (ltr.match(/[^&*(){}]/)) {
        this.shiftleft = true;
        this.alt = true;
        // Phpcs:ignore
    } else if (ltr.match(/[`ăâêộ̀̉̃́đ-₫\\;',./]/)) {
        this.shiftleft = false;
        this.shiftright = false;
        this.alt = false;
        this.accent = false;


        // Phpcs:ignore
    } else if (ltr.match(/[1234567890=[\]]/i)) {
        this.shiftleft = false;
        this.shiftright = false;
        this.alt = true;
        // Phpcs:ignore
    } else if (ltr.match(/[^&*()]/i)) {
        this.shiftleft = true;
        this.alt = true;
        this.tilde = false;
    } else if (ltr.match(/[!@#$%]/i)) {
        this.shiftright = true;
        this.alt = true;
        this.tilde = false;
    } else {
        this.shiftleft = false;
        this.shiftright = false;
        this.alt = false;
        this.tilde = false;
    }

    // Highlight the next character to type.
    this.turnOn = function() {
        if (isLetter(this.chr)) {
            document.getElementById(getKeyID(this.chr)).className = "next" + thenFinger(this.chr.toLowerCase());
        } else if (this.chr === ' ') {
            document.getElementById(getKeyID(this.chr)).className = "nextSpace";
        } else {
            document.getElementById(getKeyID(this.chr)).className = "next" + thenFinger(this.chr.toLowerCase());
        }
        if (this.chr === '\n' || this.chr === '\r\n' || this.chr === '\n\r' || this.chr === '\r') {
            document.getElementById('jkeyenter').className = "next4";
        }
        if (this.shiftleft) {
            document.getElementById('jkeyshiftl').className = "next4";
        }
        if (this.shiftright) {
            document.getElementById('jkeyshiftr').className = "next4";
        }
        if (this.alt) {
            document.getElementById('jkeyaltgr').className = "nextSpace";
        }
    };
    // Reset the highlight of the just typed character.
    this.turnOff = function() {
        if (isLetter(this.chr)) {
            // Phpcs:ignore
            if (this.chr.match(/[asdfjkl;]/i)) {
                document.getElementById(getKeyID(this.chr)).className = "finger" + thenFinger(this.chr.toLowerCase());
            } else {
                document.getElementById(getKeyID(this.chr)).className = "normal";
            }
        } else {
            document.getElementById(getKeyID(this.chr)).className = "normal";
        }
        if (this.chr === '\n' || this.chr === '\r\n' || this.chr === '\n\r' || this.chr === '\r') {
            document.getElementById('jkeyenter').className = "normal";
        }
        if (this.shiftleft) {
            document.getElementById('jkeyshiftl').className = "normal";
        }
        if (this.shiftright) {
            document.getElementById('jkeyshiftr').className = "normal";
        }
        if (this.alt) {
            document.getElementById('jkeyaltgr').className = "normal";
        }
    };
}

/**
 * Set color flag based on current character.
 * @param {string} tCrka The current character.
 * @returns {number}.
 */
function thenFinger(tCrka) {
    if (tCrka === ' ') {
        return 5; // Highlight the spacebar.
        // phpcs:ignore
    } else if (tCrka.match(/[`~ă1!'"qa;:đ0)zp\[{/?\-_\]}₫ươ=+\\|]/i)) {
        return 4; // Highlight the correct key above in red.
        // phpcs:ignore
    } else if (tCrka.match(/[â2@slwx.>oq̣9(]/i)) {
        return 3; // Highlight the correct key above in green.
        // phpcs:ignore
    } else if (tCrka.match(/[ê3#,<ediḱ8*c]/i)) {
        return 2; // Highlight the correct key above in yellow.
        // phpcs:ignore
    } else if (tCrka.match(/[ô4$vrjnuk5̀%ỷ6^fb̃7&tghm]/i)) {
        return 1; // Highlight the correct key above in blue.
    } else {
        return 6; // Do not change any highlight.
    }
}

/**
 * Get ID of key to highlight based on current character.
 * @param {string} tCrka The current character.
 * @returns {string}.
 */
function getKeyID(tCrka) {
    if (tCrka === ' ') {
        return "jkeyspace";
    } else if (tCrka === ',') {
        return "jkeycomma";
    } else if (tCrka === '\n') {
        return "jkeyenter";
    } else if (tCrka === '.') {
        return "jkeyperiod";
    } else if (tCrka === '-' || tCrka === '_') {
        return "jkeyminus";
    } else if (tCrka === '`') {
        return "jkeybackquote";
    } else if (tCrka === '!' || tCrka === 'ă') {
        return "jkey1";
    } else if (tCrka === '@' || tCrka === 'â') {
        return "jkey2";
    } else if (tCrka === '#' || tCrka === 'ê') {
        return "jkey3";
    } else if (tCrka === '$' || tCrka === 'ô') {
        return "jkey4";
    } else if (tCrka === '%' || tCrka === '̀') {
        return "jkey5";
    } else if (tCrka === '^' || tCrka === '̉') {
        return "jkey6";
    } else if (tCrka === '&' || tCrka === '̃') {
        return "jkey7";
    } else if (tCrka === '*' || tCrka === '́') {
        return "jkey8";
    } else if (tCrka === '(' || tCrka === '̣') {
        return "jkey9";
    } else if (tCrka === ')' || tCrka === 'đ') {
        return "jkey0";
    } else if (tCrka === '-' || tCrka === '_') {
        return "jkeyminus";
    } else if (tCrka === '[' || tCrka === '{' || tCrka === 'ư') {
        return "jkeybracketl";
    } else if (tCrka === ']' || tCrka === '}' || tCrka === 'ơ') {
        return "jkeybracketr";
    } else if (tCrka === ';' || tCrka === ':') {
        return "jkeysemicolon";
    } else if (tCrka === "'" || tCrka === '"') {
        return "jkeycrtica";
    } else if (tCrka === "\\" || tCrka === '|') {
        return "jkeybackslash";
    } else if (tCrka === ',' || tCrka === '<') {
        return "jkeycomma";
    } else if (tCrka === '.' || tCrka === '>') {
        return "jkeyperiod";
    } else if (tCrka === '=' || tCrka === '+' || tCrka === '₫') {
        return "jkeyequals";
    } else if (tCrka === '?' || tCrka === '/') {
        return "jkeyslash";
    } else if (tCrka === '~' || tCrka === '`') {
        return "jkeybackquote";
    } else {
        return "jkey" + tCrka;
    }
}

/**
 * Is the typed letter part of the current alphabet.
 * @param {string} str The current letter.
 * @returns {(number|Array)}.
 */
function isLetter(str) {
    return str.length === 1 && str.match(/[!-ﻼ]/i);
}
