/**
 * Copyright (C) 2020 to the present, Crestron Electronics, Inc.
 * All rights reserved.
 * No part of this software may be reproduced in any form, machine
 * or natural, without the express written consent of Crestron Electronics.
 * Use of this source code is subject to the terms of the Crestron Software License Agreement 
 * under which you licensed this source code.  
 *
 * This code was automatically generated by Crestron's code generation tool.
 */
/* jslint es6 */

const shutdownModule = (function () {
    'use strict';

    const timerInitialValue = 30;
    const shutDownEle = document.getElementById("shutDownBtn");
    const continueEle = document.getElementById("cancelShutDown");
    let isPowerOn = false;
    let subShutdownIsShuttingDown = null;
    let subShutdownShuttingDownCount = null;
    let subShutdownHardButton1 = null;
    let isShuttingDownScreenActivated = false;
    let isCancelShuttingDownClicked = false;

    /**
     * Reset Screensaver
     */
    function resetScreenSaver() {
        resetTimer();
        isCancelShuttingDownClicked = true;
        screenSaverModule.resetTimer();
    }

    /**
     * Initialize Method
     */
    function onInit() {
        initializeVariables();
        invokeUnsubscriptions();
        invokeSubscriptions();
    }

    /**
     * Invoke subscriptions related to module
     */
    function invokeSubscriptions() {
        subShutdownIsShuttingDown = CrComLib.subscribeState('b', 'Room[0].Power.IsShuttingDown', (response) => {
            const shuttingDownBoolean = avfUtility.toBool(response);
            avfUtility.log('Room[0].Power.IsShuttingDown:', shuttingDownBoolean);
            if (shuttingDownBoolean === true) {
                isShuttingDownScreenActivated = true;
                navigationModule.openPopup(navigationModule.popupPages.shutdownImportPage);
            } else {
                if (isShuttingDownScreenActivated) {
                    isShuttingDownScreenActivated = false;
                    // if (isCancelShuttingDownClicked) {
                    resetTimer();
                    navigationModule.closePopup(navigationModule.popupPages.shutdownImportPage);
                    // }
                }
            }
        });

        // Show device shutdown counter on shutting down screen. 
        // If its active then its turn off the screen saver.
        subShutdownShuttingDownCount = CrComLib.subscribeState('n', 'Room[0].Power.ShuttingDownCount', (response) => {
            const timerCountDiv = document.getElementById("timer-count-text");
            const counter = parseInt(response);
            avfUtility.log('Room[0].Power.ShuttingDownCount:', counter);
            screenSaverModule.turnOffScreenSaver();
            timerCountDiv.innerText = `${counter}`;
            if (counter == 0) {
                goToHomePage();
            }
        });

        CrComLib.subscribeState('b', 'Room[0].Power.IsPowerOn', (response) => {
            isPowerOn = avfUtility.toBool(response);
            avfUtility.log('Room[0].Power.IsPowerOn:', isPowerOn);
            if (isPowerOn === false) {
                avfUtility.log('Going to Home page because of power off');
                navigationModule.goToPage();
            }
        });

        subShutdownHardButton1 = CrComLib.subscribeState('b', 'Csig.Hard_Button_1.Press', (response) => {
            avfUtility.log('Csig.Hard_Button_1.Press: ' + response);
            CrComLib.publishEvent('b', 'Room[0].Power.PowerOff.Execute', response);
        });
    }

    /**
     * Invoke unsubscriptions related to module
     */
    function invokeUnsubscriptions() {
        if (subShutdownIsShuttingDown) {
            CrComLib.unsubscribeState('b', 'Room[0].Power.IsShuttingDown', subShutdownIsShuttingDown);
        }
        if (subShutdownShuttingDownCount) {
            CrComLib.unsubscribeState('n', 'Room[0].Power.ShuttingDownCount', subShutdownShuttingDownCount);
        }
        if (subShutdownHardButton1) {
            CrComLib.unsubscribeState('b', 'Csig.Hard_Button_1.Press', subShutdownHardButton1);
        }
    }

    /**
     * 
     */
    function goToHomePage() {
        navigationModule.closePopup(navigationModule.popupPages.shutdownImportPage);
        navigationModule.goToPage();
    }

    /**
     * Initialize all the variables used in this module
     */
    function initializeVariables() {
        isPowerOn = false;
        isShuttingDownScreenActivated = false;
        subShutdownIsShuttingDown = null;
        subShutdownShuttingDownCount = null;
        subShutdownHardButton1 = null;
        resetTimer();
    }

    /**
     * Reset Timer
     */
    function resetTimer() {
        setTimeout(() => {
            const timerCountDiv = document.getElementById("timer-count-text");
            timerCountDiv.innerText = String(timerInitialValue);
        }, 1000);
    }

    shutDownEle.addEventListener("click", goToHomePage, false);
    continueEle.addEventListener("click", resetScreenSaver, false);

    return {
        onInit
    };

}());