/**
 * dashboard module template
 */
let background;

class DashboardModule {
    /**
     * @returns {DashboardModule}
     */
    static create(background) {
        return new DashboardModule(background);
    }

    constructor(background)
    {
        this.background = background;

    }

    /**
     * @param {ViewModel} viewModel 
     */
    init(viewModel) {
        this._viewModel = viewModel;

        // Add your own initialization

        this._initialized = true;
        this._active = false;

        background = document.getElementById("lvdf535ed837027492eb0f65b7d64dba994");

        Apply.initApply(background);
    }

    /**
     * @param {ViewModel} viewModel 
     */
    cleanUp(viewModel) {
        // Timeouts, intervals, promises and eventListeners registered using
        // sigApi.sigUtils funcitons are cleaned up by runtime
        // example of sigApi.sigUtls.setTimeout
        //   sigApi.sigUtils.setTimeout(this, () => {}, 1000);

        // Add your own cleanUp code

        this._initialized = false;
    }

    /**
     * @param {boolean} active 
     */
    onActiveChanged(active) {
        // Add your own active state handling code

        this._active = active;
    }

    _registerEvents(background) {

        background.addEventListener("click", () => {
/*
            sigApi.datapointManager.readDataPoint('10:Global_Set1.TTY_Schalter', 0)
                .then((resolve) => {

                    if (resolve.value === 1)
                    {
                        window.sigApi.appRouter.navigate('TTY');
                    }
                    else
                    {
                        window.sigApi.appRouter.navigate('Startseite');
                    }
                }); */
        });
    }


}

class Apply {

    static initApply(background) {

        if (window.sigApi !== undefined) {
            const fm = new Apply(window.sigApi,background);
        } else {
            // ist sigApi noch nicht geladen, als EventListener fuer 'SigApiReady' registrieren
            window.addEventListener('SigApiReady', () => {
                const fm = new Apply(window.sigApi,background);
            });
        }
    }

    constructor(api, background) {

        this.events = null;
        this.eventMediator = null;
        this.datapointAPI = null;
        this._initAPIs(api);

        this.background =  background;

        this._registerEvents(background);
    }

    _initAPIs(api) {
        if ((api !== null) && (api !== undefined)) {
            if (api.datapointManager !== undefined) {
                this.datapointAPI = api.datapointManager;
            }
            if (api.eventMediator !== undefined) {
                this.eventMediator = api.eventMediator;
            }
            if (api.events !== undefined) {
                this.events = api.events;
            }
        }
    }

    _registerEvents(background) {

        background.addEventListener("click", () => {

            sigApi.datapointManager.readDataPoint('10:Global_Set1.TTY_Schalter', 0)
                .then((resolve) => {

                    if (resolve.value === 1)
                    {
                        window.sigApi.appRouter.navigate('TTY');
                    }
                    else
                    {
                        window.sigApi.appRouter.navigate('Startseite');
                    }
                });
        });
    }

}


// Register the DashboardModule for a dashboard
// window.registerDashboardModule('New_StartDashdoard', DashboardModule.create);
