{{-- Express Calendar --}}
<script>
    $(function() {
      //* Build Calendar Data
        // Build calendar
        const $express_calendar = $('#express-calendar');
        window.express_calendar = new FullCalendar.Calendar($express_calendar[0], {
          // Plugins - Localization - Lang
            plugins: ['interaction', 'resourceTimeline'],
            locale: window.page_lang,
            lang: window.page_lang, // Idioma
            navLinks: false,
            timeZone: "local",

          // Default view and design
            defaultView: 'resourceTimelineDay',
            aspectRatio: 1.5,
            resourceAreaWidth: '30%',
            height: 600,

          // Time Control
            nowIndicator: true,

            slotMinTime: "00:00:00",
            slotMaxTime: "24:00:00",
            slotDuration: '00:30:00',
            slotLabelInterval: '00:30',
            scrollTime: "00:00",
            snapDuration: '00:10:00', // Drag & Drop de cuanto en cuanto

          // Header and Buttons
            header: {
                left: 'today prev,next',
                center: 'title',
                right: 'resourceTimelineDay resourceTimelineWeek resourceTimelineFifteenDay',
            },

          // Custom button's texts
            buttonText: {
                today: "{{ __('neutral_words')['today'] }}",
                resourceTimelineDay: "{{ __('neutral_words')['day'] }}",
                resourceTimelineWeek: "{{ __('neutral_words')['week'] }}",
            },

          // Custom views and design
            views: {
                resourceTimelineFifteenDay: {
                    type: 'resourceTimeline',
                    duration: { weeks: 2 },
                    buttonText: "{{ __('neutral_words')['fortnight'] }}"
                },
            },

          // Data
            resourceColumns: @json($render_data->resource_columns),
            resourceLabelText:  "{{ $render_data->resource_main_label }}",
            resourceOrder: '{{ $render_data->resource_order }}',
            resources(fetchInfo, successData, failureMessage) {
                $.get("{{ route('api.public-calendar.rooms') }}", {is_express: true})
                    .done(successData).fail(failureMessage);
            },

            events: function(fetchInfo, successCallback, failureCallback) {
              // Conditionals
                if(!window.is_express_calendar) {
                    successCallback([]);
                    return;
                }

              // Get Stored Data
                let eventsEncrypted = localStorage.getItem("ExpressEventData") || null;
                let eventsStored = decryptData(eventsEncrypted);

              // Request for Changes
                // Request Data
                const start = moment(fetchInfo.startStr).format('YYYY-MM-DD');
                const end = moment(fetchInfo.endStr).format('YYYY-MM-DD');

                $.get("{{ route('api.public-calendar.events') }}", {start, end, is_express: true},
                    function(response) {
                        let data = JSON.parse(response);

                        if (JSON.stringify(eventsStored) !== JSON.stringify(data)) {
                            successCallback(data);

                            const encrypted = encryptData(data);
                            localStorage.setItem("ExpressEventData", encrypted);

                            launchSwalToast(
                                `{{ __('neutral_words')['information'] }} <b style="color:#12a15e">{{ __('common_actions')['updated'] }}</b>`,
                                'info',
                            );
                        }
                    }
                );
            },
        });

        // Draw Calendar
        window.express_calendar.render();
        window.express_calendar.scrollToTime(moment().format('HH:mm:ss'));

      //* Calendar data cycle
        window.updateCalendarExpressEvents = () => window.express_calendar.refetchEvents();
        window.updateCalendarExpressEventsDebounced = (window.updateCalendarExpressEventsDebounced || debounce(updateCalendarExpressEvents, 800));

        setInterval(() => {
            if(window.is_express_calendar)
                updateCalendarExpressEventsDebounced();
        }, 6000);
    });
</script>
