var m_retailmodeTimer = null;
var RETAILMODE_FLAG_FILE = "retailmode";
var retailmodeTimeout = 15 * 1000;
var currentMode = 0;
var startEposFlag = false;
/*
 * 打开epos
 * @param
 *   show: true-open epos page and change RETAILMODE_FLAG_FILE to open; false-change RETAILMODE_FLAG_FILE to close;
 * */
var startEPOS = false;
function startePos(enable) {
    DBG_INFO('startePos show:' + enable);
    startEposFlag = true;
    try {
        currentMode = model.system.getUserMode();
    }
    catch (ex) {
        DBG_ERROR(ex.message);
    }
//    if(currentMode == SystemModelDefines.SL2_TVAPI_STR_SYSTEM_USER_MODE_HOME) return;
//    if(currentMode == 1 || currentMode ==2) {
        if (enable) {
            try {
                UI.popAllModule();
                startEPOS = true;
                var timerInterval = 600;
                if (checkIsAppOn()) {
                    timerInterval = 6000;
                }
                changeSourceForEPOS();
                UI.lockAllKeys("epos");
                var timerCheckApp = setInterval(function () {
                    //when launch ePos, have to stop current running app
//                    model.sound.setMainMute(0);
//                model.video.setEnumPictureMode(VideoModelDefines.SL2_TVAPI_I32_VIDEO_ENUM_PICTURE_VIVID); //EPOS
                    //打开新的浏览器
                    try {
//                    startThirdPartyApp(APPNAME.EPOS, null, CMDType.NATIVEAPP, CMDMethod.COMMANDURL, false);
                        startThirdPartyApp(APPNAME.EPOS, null, CMDType.HISENSEPAGE, CMDMethod.COMMANDURL, false);

                    } catch (e) {
                        DBG_ERROR(e.message);
                    }
                    writeFileToNative(RETAILMODE_FLAG_FILE, "open", 0);
                    if (model.system.getUserMode() == 2) {
                        DBG_INFO("current user mode is Store Mode Play 4k Video");
                        writeFileToNative("enable4kVideo", "enable", 0);
                    } else if (model.system.getUserMode() == 1) {
                        DBG_INFO("current user mode is Store Mode");
                        writeFileToNative("enable4kVideo", "disable", 0);
                    }
                    resetPQAQ(true);
//                    setHalfAnHourTimer(true);
                    model.sound.setMainMute(0);
                    clearInterval(timerCheckApp);
                    UI.unlockAllKeys("epos");
                }, timerInterval);
                startEposFlag = false;
            }
            catch (ex) {
                startEposFlag = false;
                DBG_INFO("startePos:" + ex.message);
            }
        }
//    }
}

function startRetailmodeTimer() {
    DBG_INFO('startRetailmodeTimer to start epos!! ');
    try {
        currentMode = model.system.getUserMode();
    }
    catch (ex) {
        DBG_ERROR(ex.message);
    }
    DBG_INFO('retail mode timer starting……currentMode is ' + currentMode);
    try {
        if (1 != GLOBAL.FACTORY) {
            var data = readFileFromNative(RETAILMODE_FLAG_FILE, 0);
            var channelScanning = model.channelSearch.getRunning();
            DBG_INFO("[configure]channelScanning: " + channelScanning + " data:" + data ) ;
            if ((data == "close" || data == null) && channelScanning != 1 ) {//2 means file not exist
                startePos(true);
            }
            else {
                DBG_INFO('Need not startEPOS');
            }
        }
        else {
            DBG_INFO('Now is not shop mode ? Need not start');
            UI.clearEposMenuTimeout();
        }
    }
    catch (ex) {
        DBG_INFO("[configure]Read retailmode file error: " + ex.message, DebugLevel.ERROR);
        //if cannot read file, means ePos have not run once yet.
        startePos(true);
    }
    UI.refreshEposMenuTimeout();
}

var tenMinTmr = 0;
function resetPQAQ(flag) {
    try {
        DBG_INFO("set 10 min timer [" + flag + "]");
        clearInterval(tenMinTmr);
        if (flag) {
            tenMinTmr = setInterval(function () {
                if ("open" == readFileFromNative(RETAILMODE_FLAG_FILE, 0)) {
                    DBG_INFO("action half an hour on.")
                    try{
                        model.system.storeModeSoundReset();
                    }catch(ex){
                        DBG_ERROR(ex.message);
                    }
                    try{
                        model.system.storeModePictureReset();
                    }catch(ex){
                        DBG_ERROR(ex.message);
                    }
                }
                else {
                    DBG_ERROR("epos has been stopped.");
                }
            }, 10 * 60 * 1000);//全球市场统一由30分钟改为10分钟
        }
    }
    catch (ex) {
        DBG_ERROR(ex.message);
    }
}
function changeSourceForEPOS(){
    var inputsData = initAllInputsData();
    var list = getRecentInputs(inputsData);
    try{
        var crtSource = getCurrentSourceId();
    }catch(e){
        DBG_ERROR(e.message);
    }
    for(var i = 0; i < list.length; i++) {
        if(list[i].id == crtSource){
            if(list[i].signal == 0){
                return;
            }
        }
    }
    for(var i = 0; i < list.length; i++) {
        if(list[i].id == crtSource){
            continue;
        }
        if(parseInt(list[i].hotelLock) == 1) {
            continue;
        }
        if(list[i].signal == 0) {
            if (list[i].id == 0) { //tv
                changeSourceToTV();
            }
            else {
//                var id = mapInputsDataById(list[i].id);
//                model.tvservice.playChannel(0, id, 0, null, 0);
                changeSourceTo(list[i].id);
            }
            return;
        }
    }
}

