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

    let subDisconnectedOnlineFB = null;
    let subDisconnectedOnline = null;
    let subDisconnectedOfflineFB = null;
    let subDisconnectedOffline = null;

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

    /**
     * Invoke subscriptions related to module
     */
    function invokeSubscriptions() {
        subDisconnectedOnlineFB = CrComLib.subscribeState('b', 'Csig.All_Control_Systems_Online_fb', (response) => {
            const disconnectedFBResponse = avfUtility.toBool(response);
            avfUtility.log("Csig.All_Control_Systems_Online_fb", disconnectedFBResponse);
            if (disconnectedFBResponse === true) {
                navigationModule.closePopup(navigationModule.popupPages.disconnectedImportPage);
            }
        });

        subDisconnectedOnline = CrComLib.subscribeState('b', 'Csig.All_Control_Systems_Online',  (response) => {
            const disconnectedFBResponse = avfUtility.toBool(response);
            avfUtility.log("Csig.All_Control_Systems_Online", disconnectedFBResponse);
            if (disconnectedFBResponse === true) {
                navigationModule.closePopup(navigationModule.popupPages.disconnectedImportPage);
            }
        });

        subDisconnectedOfflineFB = CrComLib.subscribeState('b', 'Csig.Control_Systems_Offline_fb', (response) => {
            let disconnectedFBResponse = avfUtility.toBool(response);
            avfUtility.log('Disconnected 70 Series - Csig.Control_Systems_Offline_fb: ', disconnectedFBResponse);
            if (disconnectedFBResponse) {
                navigationModule.openPopup(navigationModule.popupPages.disconnectedImportPage);
                setTimeout(() => {
                    location.reload();
                }, 2000);
            }
            else {
                // if (appModule.getSystemReady() === false) {
                //     // navigationModule.closePopup(navigationModule.popupPages.disconnectedImportPage);
                // }
                navigationModule.closePopup(navigationModule.popupPages.disconnectedImportPage);
                // // navigationModule.showHomeScreen(true);
            }
        });

        subDisconnectedOffline = CrComLib.subscribeState('b', 'Csig.Control_Systems_Offline', (response) => {
            const disconnectedResponse = avfUtility.toBool(response);
            avfUtility.log('Disconnected 60 Series - Csig.Control_Systems_Offline: ', disconnectedResponse);
            if (disconnectedResponse) {
                navigationModule.openPopup(navigationModule.popupPages.disconnectedImportPage);
                setTimeout(() => {
                    location.reload();
                }, 2000);
            } else {
                // if (appModule.getSystemReady() === false) {
                    navigationModule.closePopup(navigationModule.popupPages.disconnectedImportPage);
                // }
                // // navigationModule.showHomeScreen(true);
            }
        });
    }

    /**
     * Invoke unsubscriptions related to module
     */
    function invokeUnsubscriptions() {
        if (subDisconnectedOnlineFB) {
            CrComLib.unsubscribeState('b', 'Csig.All_Control_Systems_Online_fb', subDisconnectedOnlineFB);
        }
        if (subDisconnectedOnline) {
            CrComLib.unsubscribeState('b', 'Csig.All_Control_Systems_Online', subDisconnectedOnline);
        }
        if (subDisconnectedOfflineFB) {
            CrComLib.unsubscribeState('b', 'Csig.Control_Systems_Offline_fb', subDisconnectedOfflineFB);
        }
        if (subDisconnectedOffline) {
            CrComLib.unsubscribeState('b', 'Csig.Control_Systems_Offline', subDisconnectedOffline);
        }
    }

    /**
     * Initialize all the variables used in this module
     */
    function initializeVariables() {
        subDisconnectedOnlineFB = null;
        subDisconnectedOnline = null;
        subDisconnectedOfflineFB = null;
        subDisconnectedOffline = null;
    }

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

}());