/**
 * 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.
 */

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

    let subAnnouncementInProgress = null;
    let subAnnouncementText = null;

    /**
     * On Init
     */
    function onInit() {
        invokeUnsubscriptions();
        initializeVariables();
        invokeSubscriptions();
    }

    /**
     * Initialize all the variables used in this module
     */
    function initializeVariables() {
        subAnnouncementInProgress = null;
        subAnnouncementText = null;
    }

    /**
     * Invoke unsubscriptions related to module
     */
    function invokeUnsubscriptions() {
        if (subAnnouncementInProgress) {
            CrComLib.unsubscribeState('b', 'Room[0].LockTouchPanel.InProgress', subAnnouncementInProgress);
        }
        if (subAnnouncementText) {
            CrComLib.unsubscribeState('s', 'Room[0].LockTouchPanel.Message', subAnnouncementText);
        }
    }

    /**
     * Invoke subscriptions related to module
     */
    function invokeSubscriptions() {
        subAnnouncementInProgress = CrComLib.subscribeState('b', 'Room[0].LockTouchPanel.InProgress', (response) => {
            const announcementResponse = avfUtility.toBool(response);
            avfUtility.log('Room[0].LockTouchPanel.InProgress: ', announcementResponse);
            if (announcementResponse) {
                navigationModule.openPopup(navigationModule.popupPages.announcementImportPage);
            } else {
                navigationModule.closePopup(navigationModule.popupPages.announcementImportPage, 0, false);
            }
        });

        subAnnouncementText = CrComLib.subscribeState('s', 'Room[0].LockTouchPanel.Message', (response) => {
            const announcementResponse = response;
            avfUtility.log('Room[0].LockTouchPanel.Message', announcementResponse);
            if (avfUtility.isValidInput(announcementResponse)) {
               document.getElementById("announcementMessageText").innerHTML = announcementResponse;
            } else {
                setTimeout(()=> {
                    document.getElementById("announcementMessageText").innerHTML = "";
                }, 1000);
            }
        });
    }

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

}());