
var _userdata = require("userdata.js");
var userData = _userdata.userData;
const fs = require("@system.fs");
var systemCtrl = require("systemCtrl.js");
var lvgl = require("@native.lvgl7");
var id_testRtcTimer = null;
var that;
var thatList
var jumpPageName;
var id_testAtuoTimer;

//   globalThis.factoryTest.color = 0;
//   globalThis.factoryTest.touch = 0;
//   globalThis.factoryTest.wifi = 0;
//   globalThis.factoryTest.sound = 0;
//   globalThis.factoryTest.uartComm = 0;
//   globalThis.factoryTest.bright = 0;
//   globalThis.factoryTest.licenseAuthor = 0;
//   globalThis.factoryTest.rtc = 0;
//   globalThis.factoryTest.burnIn = 0;
//   globalThis.factoryTest.systemCheck = 0;





var flageMap = {
    "factoryTestColor": "color",
    "factoryTestTouch": "touch",
    "factoryTestWifi": "wifi",
    "factoryTestSound": "sound",
    "factoryTestUartComm": "uartComm",
    "factoryTestBright": "bright",
    "factoryTestLicenseAuthor": "licenseAuthor",
    "factoryTestRtc": "rtc",
    "factoryTestBurnIn": "burnIn",
    "factoryTestsystemCheck": "systemCheck",
    "factoryTestClearAll": "clearAll",
    "factoryTestStart": "start",
    "factoryTestExit": "exit"
};



function autoTestCheck() {
    // 获取当前页面名称
    var currentTest = getCurrentTestName();
    if (currentTest) {
        // 检查当前测试是否完成
        //console.log("!!Current test: " + globalThis.factoryTest[flageMap[currentTest]]);
        if (globalThis.factoryTest[flageMap[currentTest]] === 1) {
            // 获取下一个测试页面名称
            var nextTest = getNextTest(currentTest);
            //console.log("!!Next test: " + nextTest);
            if (nextTest) {
                // 跳转到下一个测试页面
                router.push({
                    path: nextTest,
                });
                //console.log("Auto jumping to: " + nextTest);
            }
        }
    }
}


function getNextTest(currentTest) {
    // 获取当前测试项的索引
    var currentIndex = thatList.appItem.findIndex(item => item.startup === currentTest);
    //console.log("currentIndex: " + currentIndex);
    if (currentIndex !== -1) {
        // 获取下一个测试项
        var nextIndex = (currentIndex + 1) % thatList.appItem.length;
          //console.log("nextIndex: " + nextIndex);
        return thatList.appItem[nextIndex].startup;
    }
    return null;
}

function atuoFactoryTest( pageName) {

    // jumpPageName = pageName;
    // // 启动定时器，每5秒检查一次测试状态并跳转
    // if (id_testAtuoTimer != null) {
    //     clearInterval(id_testAtuoTimer);
    //     id_testAtuoTimer == null;
    // }
    // id_testAtuoTimer = setInterval(() => {
    //     autoTestCheck();
    // }, 2000);
}


function getCurrentTestName() {
    // 获取当前页面名称，这里假设有一个方法可以获取当前页面名称
    // 例如，可以通过路由信息获取
    return jumpPageName; // 假设 this.$route.path 返回当前页面路径
}

function initMianPage(obj) {
    //console.log("!!!!!!!!initMianPage!!!!!!!!");
    thatList = obj;
}



module.exports = {
    atuoFactoryTest: atuoFactoryTest,
    initMianPage: initMianPage

}