import * as I from "../types/types";

export default class Utils {

    static waitForElement(inSelector: string | CallableFunction, inCallback: Function, inSwitch: I.TypeWaitForElementSwitch = "selector", inCheckIntervalInMs: number = 1000, inTimeoutInMs: number = 10000): void {
        const startTimeInMs: number = Date.now();

        (function loopSearch(): void {
            const element: boolean | HTMLElement = inSwitch === "selector" ? jQuery(<string>inSelector).get(0) : Utils.getSeatmapElement();
            
            if (element) {
                console.log("ready");
                inCallback();
            }                
            else {
                console.log(`Repeating for ${inSwitch}`);
                setTimeout(() => {
                    if (inTimeoutInMs && Date.now() - startTimeInMs > inTimeoutInMs)
                        return;

                    loopSearch();
                }, inCheckIntervalInMs);
            }
        })();
    }

    static getSeatmapElement(): boolean {
        const seatCharts: CallableFunction = (<any>window).jQuery("#containerSeatmap").seatCharts;
        return Utils.isFunction(seatCharts);
    }

    static waitForSeatmap(inCallback: Function): void {
        const seatCharts: CallableFunction = (<any>window).jQuery("#containerSeatmap").seatCharts;
        this.waitForElement(seatCharts, inCallback, "seatmap");
    }

    static querySelector(selector: string): boolean {
        if (document.querySelector(selector) != null)
            return true;
        else
            return false;
    }

    static isFunction(inSelector: CallableFunction): boolean {
        return typeof inSelector === "function" ? true : false;
    }

    static inject(inContent: string, inType: string = "js", inLoc: string = "head"): void {
        var script: I.TypeInjectScript | undefined;

        switch (inType) {
            case "js":
                script = document.createElement('script');
                script.type = 'text/javascript';
                script.src = inContent;
                break;

            case "css":
                script = document.createElement('link');
                script.type = 'text/css';
                (<HTMLLinkElement>script).rel = "stylesheet";
                (<HTMLLinkElement>script).href = inContent;
                break;

            case "cssCustom":
                script = document.createElement('style');
                script.type = 'text/css';
                script.innerHTML = inContent;
                break;
            
            case "script":
                script = document.createElement('script');
                script.type = 'text/javascript';
                script.innerHTML = inContent;
                break;

            default:
                script = undefined;
                break;                
        }

        if (script)
            Utils.appendScript(script, inLoc);        
    }

    static appendScript(inScript: I.TypeInjectScript, inLoc: string): void {
        if (inLoc === "body")
            document.body.appendChild(inScript);
        else if (inLoc === "head")
            document.head.appendChild(inScript);
    }

    static getDayName(inDate: Date, inLocale: string): string {
        return inDate.toLocaleDateString(inLocale, { weekday: 'long' });
    }    
}