/**
 * 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 emergencyBroadcastModule = (function () {
    'use strict';

    let subEmergencyIsEmergency = null;
    let subEmergencyInProgress = null;
    let subEmergencyMessage = null;

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

     /**
     * Add the red boader to popup
     * @param {boolean} enable 
     */
    function setRedBorder(enableBorder) {
        let screenBody = document.getElementById("emergency-broadcast-page");
        screenBody.classList.remove("selected-element-8");
        if (enableBorder) {
            screenBody.classList.add("selected-element-8");
        }
    }

    /**
     * Invoke subscriptions related to module
     */
    function invokeSubscriptions() {
        subEmergencyIsEmergency = CrComLib.subscribeState('b', 'Room[0].Broadcast.IsEmergency', (response) => {
            const isEmergencyBroadcastResponse = avfUtility.toBool(response);
            avfUtility.log('Room[0].Broadcast.IsEmergency: ', isEmergencyBroadcastResponse);
            const emergencyImage = document.getElementById("emergencyImage");
            const emergencyText = document.getElementById("emergencyText");
            if (isEmergencyBroadcastResponse) {
                emergencyImage.style.display = 'block';
                emergencyText.innerHTML = 'Emergency Alert';
            } else {
                emergencyImage.style.display = 'none';
                emergencyText.innerHTML = 'General Message';
            }
            setRedBorder(isEmergencyBroadcastResponse);
        });

        subEmergencyInProgress = CrComLib.subscribeState('b', 'Room[0].Broadcast.InProgress', (response) => {
            const emergencyBroadcastResponse = avfUtility.toBool(response);
            avfUtility.log('Room[0].Broadcast.InProgress: ', emergencyBroadcastResponse);
            if (emergencyBroadcastResponse) {
                navigationModule.openPopup(navigationModule.popupPages.emergencyBroadcastImportPage);
            } else {
                navigationModule.closePopup(navigationModule.popupPages.emergencyBroadcastImportPage);
            }
        });

        subEmergencyMessage = CrComLib.subscribeState('s', 'Room[0].Broadcast.Message', (response) => {
            avfUtility.log(`Room[0].Broadcast.Message: ${response}`);
            const emergencyStatusText = document.getElementById("emergencyStatusText");
            emergencyStatusText.innerText = response;
        });
    }

    /**
     * Invoke unsubscriptions related to module
     */
    function invokeUnsubscriptions() {
        if (subEmergencyIsEmergency) {
            CrComLib.unsubscribeState('b', 'Room[0].Broadcast.IsEmergency', subEmergencyIsEmergency);
        }
        if (subEmergencyInProgress) {
            CrComLib.unsubscribeState('b', 'Room[0].Broadcast.InProgress', subEmergencyInProgress);
        }
        if (subEmergencyMessage) {
            CrComLib.unsubscribeState('s', 'Room[0].Broadcast.Message', subEmergencyMessage);
        }
    }

    /**
     * Initialize all the variables used in this module
     */
    function initializeVariables() {
        subEmergencyIsEmergency = null;
        subEmergencyInProgress = null;
        subEmergencyMessage = null;
    }

    /**
     * All public method and properties are exported here
     */
    return {
        onInit
    };

}());