import * as Communication from "./communication";
import * as UI from "./ui";
import { config } from "./config";
import Utils from "./utils";
import * as Parser from './parser';
import * as I from "../types/types";
import * as JSC from "./jsc";
import * as Legend from "./legend";
import * as Events from "./events";
import * as Trims from "./trims";
import * as XMLHelper from "./xmlhelper";
import * as Panzoom from "./panzoom";
import * as jBoxHelper from "./jBoxHelper";
import * as Cart from "./cart";
import * as CartButtons from "./cartButtons";

export function initVenue() {
    UI.controlLoftloader("show");
    Communication.sendEventToParent("child_init_needInputsWithValue");
}

export function initSendVenueXML(inE: any) {
    const data: I.Message = JSON.parse(inE.data);
    config.state.inVenueXML = data.message.map_response;
    const venueXML = config.state.inVenueXML!;

    // Generate pricescale css classes
    const css = UI.generatePricescaleCSS();
    Utils.inject(css, "cssCustom", "body");

    // Fill event info
    const eventInfo = Parser.getEventInfo();
    UI.setEventInfo(eventInfo);

    // Fill select dropdown
    const seatmapListing: I.Seatmap[] = venueXML.seatmap_config[0].seatmap;
    UI.setOptionSelect(seatmapListing, "dropdownSeatmap");

    // Display first seatmapXML
    // If smap[1] is given by user, display specific seatmap on load
    const id: string = XMLHelper.getFirstSeatmapIDToLoad();
    
    jQuery("#dropdownSeatmap").val(id);
    Communication.needSeatmapXML(id);

    Utils.consoleLog(css);
    Utils.consoleLog(seatmapListing);
}

export function initSendInputsWithValue(inE: any) {
    const data: I.Message = JSON.parse(inE.data);
    config.state.inputsWithValue = data.message;
    UI.changeVenueImage();
    UI.changeSiteTitle();
    Communication.sendEventToParent("child_init_needVenueXML");
}

export function sendSeatmapXML(inE: any) {
    const data: I.Message = JSON.parse(inE.data);
    config.state.seatmapXML = data.message.map_response;
    const map: string[] = JSC.generateMap();
    const rowsNaming: string[] = JSC.getRowsNaming();
    const seats: I.JSCSeats = JSC.getSeats();
    const legend: I.JSCLegend = Legend.generateLegend("#JSCLegendInner");

    JSC.addSeatmap("#containerSeatmapInner", map, rowsNaming, seats, legend);
    JSC.setBookedSeatsUnavailable();
    JSC.selectSeatsInCart();
    Legend.convertLegendToDropdown("dropdownLegend");
    Events.dropdownLegendOnChange("#dropdownLegend");
    Trims.addTrims();
    XMLHelper.showHideVenueCapacity();
    config.state.panzoom = Panzoom.addPanzoom("#containerSeatmapInner", ".panzoomZoomIn", ".panzoomZoomOut", "#panzoomResetZoom");
    Cart.setImportantNote();
    UI.controlLoftloader("hide");
    jBoxHelper.createSeatTooltips();
}

export function sendCheckoutResponse(inE: any) {
    const data: I.Message = JSON.parse(inE.data);
    config.state.isValidSeatSelection = data.message.isValidSeatSelection;

    Utils.consoleLog(data.message);

    if (!config.state.isValidSeatSelection) {
        jBoxHelper.showJBoxNotice(`Auswahl nicht möglich: Einzelplatz freigelassen oder max. Ticketsanzahl überschritten.`);
        CartButtons.showHideBtnCartLoading("hide");
    }
    else {
        Cart.removeCartItems();
        Cart.generateCartItems();
        const url = Cart.generateCheckoutUrl();
        Events.addRedirectCheckout(url);
        UI.fadeInCartModal();
        CartButtons.showHideBtnCartLoading("hide");
    }

    config.state.cartChanged = false;
}

export function injectResources(): void {
    const branch: I.TypeConfigBranch = config.branch;
    const resources = config.resources[branch];

    Utils.inject(resources.JSC, "js", "head");
    Utils.inject(resources.CSSJSC, "css", "body");    
    Utils.inject(resources.CSSjQueryUI, "css", "body");    
    Utils.inject(resources.CSSChild, "css", "body");
    // Utils.inject(resources.CSSjBox, "css", "body");
}