import * as Communication from "./communication";
import * as UI from "./ui";
import { config } from "./config";
import * as I from "../types/types";
import * as Cart from "./cart";
import * as Legend from "./legend";
import * as jBoxHelper from "./jBoxHelper";

export function addCloseModal(): void {
    const btnCloseModal: HTMLElement | undefined = jQuery("#btnCloseModal").get(0);
    if (btnCloseModal)
        btnCloseModal.addEventListener("click", () => Communication.sendEventToParent("child_closeDialog"));
}

export function addDropdownSeatmap(): void {
    const dropdownSeatmap: HTMLElement | null = document.getElementById("dropdownSeatmap");

    if (dropdownSeatmap) {
        dropdownSeatmap.addEventListener("change", function (this: HTMLSelectElement) {
            UI.controlLoftloader("show"); // todo: rewrite?
            UI.destroyCurrentSeatmap("#containerSeatmapInner");
            Communication.needSeatmapXML(this.value);
        });
    }
}

export function addModalCart(): void {
    const btnCart: HTMLElement | undefined = jQuery("#modalCart .uabb-button").get(0);

    if (btnCart) {
        btnCart.addEventListener("click", () => {
            if (!config.state.selectedSeatsArr.length)
                jBoxHelper.showJBoxNotice(`Sie haben bislang keinen Platz ausgewählt.`);
            else if (config.state.cartChanged)
                Communication.isValidSeatSelection();
            else if (!config.state.cartChanged && config.state.isValidSeatSelection)
                UI.fadeInCartModal();
            else if (!config.state.cartChanged && !config.state.isValidSeatSelection)
                jBoxHelper.showJBoxNotice(`Auswahl nicht möglich: Einzelplatz freigelassen oder max. Ticketsanzahl überschritten.`); // todo: outsource error messages
        });
    }
}

export function addRedirectCheckout(inUrl: string | undefined): void {
    if (!inUrl)
        return;
    
    const btnCheckout = jQuery("#modalCart-overlay #checkout .fl-button").get(0);
    if (btnCheckout) {
        btnCheckout.addEventListener("click", () => {
            const message: I.Message = {
                message: {
                    url: inUrl,
                },
                from: "child",
                event: "child_click_checkout",
                date: Date.now()
            };
            Communication.sendMessage(message, "parent");
        });
    }
}

export function addCartClose(): void {
    const btnClose: HTMLElement = jQuery("#modalCart-overlay .uabb-close-icon").get(0);
    if (btnClose)
        btnClose.addEventListener("click", () => Communication.sendEventToParent("child_show_dialog_titlebar"));
}

export function addCartBack(): void {
    const btnBack: HTMLElement = jQuery("#modalCart-overlay #goBack .fl-button").get(0);
    if (btnBack)
        btnBack.addEventListener("click", () => jQuery("#modalCart-overlay .uabb-close-icon").trigger("click"));
}

export function dropdownLegendOnChange(inSelector: string): void {
    const dropdownLegend: HTMLElement = jQuery(inSelector).get(0);

    dropdownLegend.addEventListener("change", function (this: HTMLSelectElement) {
        Legend.dropdownLegendOnChange(this, inSelector);
    });
}

export function addCartDropdownBuyerTypes(inSelector: string, inSeatObj: I.JSCSelectedSeat): void {
    // todo: which jquery variant is not deprecated?
    jQuery(inSelector).on('change', function (this: HTMLSelectElement) {
        Cart.changedDropdownBuyerType(this, inSeatObj);
    });
}

export function addOpenSeatmap() {
    jQuery("#openSeatmap").on("click", () => {
        if (!config.childHasVenueXML)
            Communication.sendEventToChild("parent_init_venue");

        jQuery('html, body').css('overflow', 'hidden');
        jQuery("#dialogSeatmap").dialog('open');
    });
}