var _varSpace = require("varSpace.js");
var varSpace = _varSpace.varSpace;


var keyDevice = require("keyDevice.js");
keyDevice.psbInit();

setInterval(doOneSecond, 100)

var keyValue = 0
var crtTime = 0
var longTimeNop = 0
var longTimeNopMax = 60
function checkKeypad() {
    var keypadAction = keyDevice.psbGetValue("keypad");
    keyDevice.psbClearValue("keypad");
    if (keyValue != keypadAction) {
        activeScreen()
        keyValue = keypadAction
        if (keypadAction != 0) {
            //console.log("keypadAction:" + keypadAction);
            doKeyPadMatter(keypadAction);
        }
        //end
    }
}
function activeScreen() {
    longTimeNop = crtTime
}
function checkLongTimeNop() {
    // //console.log("crtTime=" + crtTime + " longTimeNop=" + longTimeNop)
    if (crtTime - longTimeNop > longTimeNopMax) {
        globalThis.eventEmitter.emit("logicLongTimeNop", "logic-checkLongTimeNop");
    }
}
function doOneSecond() {
    crtTime++

    checkLongTimeNop()
    checkKeypad()
}

function doKeyPadMatter(keyValue) {
    switch (keyValue) {
        case 13: {
            globalThis.eventEmitter.emit("logicKeypadTouched1", "logic-doKeyPadMatter");
            //console.log("No.1 has been pressed.")
            break; 
        }
        case 15: {
            globalThis.eventEmitter.emit("logicKeypadTouched2", "logic-doKeyPadMatter");
            //console.log("No.2 has been pressed.")
            break;
        }
        case 17: {
            globalThis.eventEmitter.emit("logicKeypadTouched3", "logic-doKeyPadMatter");
            //console.log("No.3 has been pressed.")
            break;
        }
        default:
            break;
    }
}
initConnect()
function emitUIDeviceGroupTouched1() {
    activeScreen()
    varSpace.lightGroup1 = (varSpace.lightGroup1 == 0 ? 1 : 0)
}
function emitUIDeviceGroupTouched2() {
    activeScreen()
    varSpace.lightGroup2 = (varSpace.lightGroup2 == 0 ? 1 : 0)
}
function emitUIDeviceGroupTouched3() {
    activeScreen()
    varSpace.lightGroup3 = (varSpace.lightGroup3 == 0 ? 1 : 0)
}
function initConnect() {
    globalThis.eventEmitter.on("uiDeviceGroupTouched1", emitUIDeviceGroupTouched1);
    globalThis.eventEmitter.on("uiDeviceGroupTouched2", emitUIDeviceGroupTouched2);
    globalThis.eventEmitter.on("uiDeviceGroupTouched3", emitUIDeviceGroupTouched3);
}
module.exports = {
    activeScreen: activeScreen,
}
