
let buttons_apply = [];
let background_;

class SetColor {

    constructor(buttons_apply,background_) {
        this.buttons_apply = buttons_apply;
        this.background_ = background_;
    }

    init(mainViewModel) {
        this.mainViewModel = mainViewModel;
        background_ = document.getElementById("lvd22e26d60e867411e9d26c45bd2ae04e6");
        buttons_apply[0] = document.getElementById("lvd404c90bea4bb4aafa95b3dbe54417781");
        buttons_apply[1] = document.getElementById("lvd7d109cd5ee764cfb9c3861444b48b270");
        buttons_apply[2] = document.getElementById("lvd2e9e18e318fd4b918d9b8e42d4019e5a");
        buttons_apply[3] = document.getElementById("lvd356a7721bb3d4f3e84bb9f1d060f2c37");
        buttons_apply[4] = document.getElementById("lvdc188ed973cf64004925869cfaca4aaa3");
        buttons_apply[5] = document.getElementById("lvdd49dbef620cd49deb56336cfa96e86cc");
        buttons_apply[6] = document.getElementById("lvdbe60e1d89cf3478c9c433ad424a89d13");
        buttons_apply[7] = document.getElementById("lvd3f20365f64b24237ac17c3ee721475e2");
        buttons_apply[8] = document.getElementById("lvdabe3748fe3aa4f379344db086a71bbb9");
        buttons_apply[9] = document.getElementById("lvd34b3873c3b384c4ab98d346849cecaee");
        buttons_apply[10] = document.getElementById("lvdff21cfea51d24a8395609baa38b5c556");
        buttons_apply[11] = document.getElementById("lvd52815c49f90f4a70a5e795794ba200ea");
        buttons_apply[12] = document.getElementById("lvd9c562f3519914efc8f76ddef48f66007");
        buttons_apply[13] = document.getElementById("lvdb5374d7f4ed64f03ba9d28ed888802f7");
        buttons_apply[14] = document.getElementById("lvda82fd988f1674e79baa48804ad804517");
        buttons_apply[15] = document.getElementById("lvd0b81e1eb2d3b4f5ba232310cdf5f8417");
        buttons_apply[16] = document.getElementById("lvd1b1699e41c054141a10b1002e274dc3e");
        buttons_apply[17] = document.getElementById("lvd0afa102c618644b48ca16b1e88867275");
        buttons_apply[18] = document.getElementById("lvdc095a027dddf4260904abed2b905d090");
        buttons_apply[19] = document.getElementById("lvd76b31e771bf341a0bc739e7137c828c3");
        buttons_apply[20] = document.getElementById("lvdcb8bf0b3bc8e41ca8cbb495b4e89af42");
        buttons_apply[21] = document.getElementById("lvd996913cd455d43fc8e8e0666ab731360");
        buttons_apply[22] = document.getElementById("lvd6fb0798cb22349b9b421d2fde919c22e");
        buttons_apply[23] = document.getElementById("lvdd13ba177d188459ea884d737eb516067");
        ApplySetColor.initApplySetColor(buttons_apply,background_);
        this.initialized = true;
    }

    cleanUp(mainViewModel) {
// Add your own cleanUp code

        this.initialized = false;
    }

    onActiveChanged(active) {

// Add your own active state handling code

        this.active = active;


    }

    static create(buttons_apply,background_) {
        return new SetColor(buttons_apply,background_);
    }

}

class ApplySetColor {

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

    constructor(api, buttons_apply,background_) {

        this.events = null;
        this.eventMediator = null;
        this.datapointAPI = null;
        this._initAPIs(api);
        this.background_ = background_;
        this.buttons_apply =  buttons_apply;
        this._registerEvents(buttons_apply,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(buttons_apply,background_) {

        if ((this.eventMediator !== null) && (this.eventMediator !== undefined) && (this.events !== null) && (this.events !== undefined)) {

         /*   background_.addEventListener("click", () => {
                sigApi.datapointManager.readDataPoint('10:SelectedZone1.ZoneID', 0)
                    .then((resolve) => {
                        for (let index = 0; index < buttons_apply.length; index++) {
                            if (index === (resolve.value - 1)){
                                buttons_apply[index].style.border = "medium solid #00ff00";
                                buttons_apply[index].style.backgroundColor = "#298A08"


                                //    buttons_apply[index].style.backgroundColor = "#ff0000";
                            }
                            else {
                                buttons_apply[index].style.border = "";
                                buttons_apply[index].style.backgroundColor= "#BEBEBE";
                            }
                        }
                    });
            }); */

            this.eventMediator.subscribe(this.events.getUserDefinedInternalEvent('SetColorForSelectedZone'), () => {

                sigApi.datapointManager.readDataPoint('10:SelectedZone1.ZoneID', 0)
                    .then((resolve) => {
                        for (let index = 0; index < buttons_apply.length; index++) {
                            if (index === (resolve.value - 1)){
                                buttons_apply[index].style.border = "medium solid #00ff00";
                               // buttons_apply[index].style.backgroundColor = "#ff0000";
                                buttons_apply[index].style.backgroundColor = "#298A08"
                            }
                            else {
                                buttons_apply[index].style.border = "";
                                buttons_apply[index].style.backgroundColor= "#BEBEBE";
                            }
                        }
                    });
            });
        }
    }
}

window.registerDashboardModule('ApplySettingsForZone', SetColor.create);
