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

    let reserveMeetingRoom = document.getElementById('reserveMeetingRoom');
    let cancelBooking = document.getElementById("cancelBooking");
    let bookMeetingEndTime = document.getElementById('bookMeetingEndTimeOptions');
    // let loaderPage = document.getElementById('loader');
    let size = 3;
    let isBusy = false;
    let spinnerSelectedIndexValue = 0;
    let subScheduleMeetingIsBusy = null;

    function onInit() {
        initializeVariables();
        invokeSubscriptions();
        // invokeUnsubscriptions();
    }

    /**
     * Invoke subscriptions related to module
     */
    function invokeSubscriptions() {
        // TODO: Need to remove the size and visibleItemScroll from the HTML CH-Spinner and set from here
        /*CrComLib.subscribeState('n', 'Room[0].RoomScheduler.ReserveNow.OptionsCount', (response) => {
            avfUtility.log(`ReserveNow.OptionsCount: ${response}`);
            size = response;
            bookMeetingEndTime.size = size; // reserveNowScheduler.size;
            bookMeetingEndTime.visibleItemScroll = size; // reserveNowScheduler.size;
        });*/

        CrComLib.subscribeState('b', 'Room[0].RoomScheduler.IsBusy', (response) => {
            isBusy = avfUtility.toBool(response);
            avfUtility.log(`RoomScheduler.IsBusy: ${isBusy}`);
            if (isBusy) {
                navigationModule.closePopup(navigationModule.popupPages.scheduleMeetingImportPage, 0);
                navigationModule.openPopup(navigationModule.popupPages.loaderImportPage);
            } else {
                navigationModule.closePopup(navigationModule.popupPages.loaderImportPage, 0);
            }
        });
    }

    /**
     * Show the room booking view and update the start and end time
     */
    function reserveNowMeeting() {
        CrComLib.publishEvent('b', `Room[0].RoomScheduler.ReserveNow.ReserveNowOption[${parseInt(spinnerSelectedIndexValue)}].Command.Execute`, false);
        CrComLib.publishEvent('b', `Room[0].RoomScheduler.ReserveNow.ReserveNowOption[${parseInt(spinnerSelectedIndexValue)}].Command.Execute`, true);
    }

    /**
     * Update the selected value for endtime
     */
    function getSelectedEndTimeOption(e) {
        const eleTg = bookMeetingEndTime.querySelector(".ch5-spinner--active");
        spinnerSelectedIndexValue = eleTg.getAttribute("data-initial-index");
        bookMeetingEndTime.setAttribute("selectedvalue", spinnerSelectedIndexValue);
    }

    /**
     * Hide the Room booking view
     */
    function cancelRoomBooking() {
        navigationModule.closePopup(navigationModule.popupPages.scheduleMeetingImportPage);
    }

    /**
     * Invoke unsubscriptions related to module
     */
    function invokeUnsubscriptions() {
        CrComLib.unsubscribeState('b', 'Room[0].RoomScheduler.IsBusy', subScheduleMeetingIsBusy);
    }

    /**
     * Initialize all the variables used in this module
     */
    function initializeVariables() {
        subScheduleMeetingIsBusy = false;
    }

    cancelBooking.addEventListener('click', cancelRoomBooking, false);
    reserveMeetingRoom.addEventListener('click', reserveNowMeeting, false);
    bookMeetingEndTime.addEventListener('change', getSelectedEndTimeOption);

    return {
        onInit: onInit
    };

}());