import jQuery = require("jquery");
import * as Communication from "./modules/communication";
import * as I from "./types/types";
import { config } from "./modules/config";
import Utils from "./modules/utils";
import * as Events from "./modules/events";
import * as Cart from "./modules/cart";
import * as BookingBtn from "./modules/bookingButton";
import * as Child from './modules/child';
import * as UI from "./modules/ui";
require("../src/css/seatmap.css");

window.addEventListener('load', () => {
    // Inject JSC (jQuery Seat Charts)
    // Inject CSS for JSC, child and jQuery dialog
    Child.injectResources();

    // Start message handler to be able to receive messages from parent
    Communication.listenToMessages(messageHandler);

    // Div with ID "containerSeatmap" is not yet ready, wait for it, then display seatmap button on trxstate 20
    Utils.waitForSeatmap(BookingBtn.showSeatmapBtnParent);

    // Modal overlay for cart is not yet ready, wait for it, then initialize
    Utils.waitForElement("#modalCart-overlay", Cart.initModalCart);

    // Add event listeners
    Events.addCloseModal();
    Events.addModalCart();
    Events.addDropdownSeatmap();

    // Change version configured in config
    UI.appendVersion();
});

// Hide header when height of window is smaller than ...
// Note: Cannot determine width of inner iframe, therefore need to use window.innerHeight
window.onresize = function () {
    const panzoom = config.state.panzoom;
    const innerHeight = window.innerHeight;

    if (innerHeight < 576) {
        jQuery("#containerEventInfoRow").hide();
        panzoom?.reset();
    }
    else if (innerHeight >= 576) {
        jQuery("#containerEventInfoRow").show();
        panzoom?.reset();
    }
}

function messageHandler(inE: any) {
    if (typeof (inE.data) !== 'string')
        return;

    const data: I.Message = JSON.parse(inE.data);

    Utils.consoleLog(`child: received from ${data.from}`);
    Utils.consoleLog(data);

    switch (data.event) {
        case "parent_init_venue": {
            Child.initVenue();
            break;
        }
        case "parent_init_sendVenueXML": {
            Child.initSendVenueXML(inE);
            break;
        }
        case "parent_init_sendInputsWithValue": {
            Child.initSendInputsWithValue(inE);
            break;
        }
        case "parent_sendSeatmapXML": {
            Child.sendSeatmapXML(inE);
            break;
        }
        case "parent_sendCheckoutResponse": {         
            Child.sendCheckoutResponse(inE);
            break;
        }
        default:
            break;
    }
}