let buttons_apply = [];
let button_back;

class ApplyZoneForOptimization {

    constructor(buttons_apply,button_back) {
        this.buttons_apply = buttons_apply;
        this.button_back = button_back;
    }
    init(mainViewModel) {
        this.mainViewModel = mainViewModel;
        buttons_apply[0] = document.getElementById("lvdcbd225eb79db4cd687be49e5cc51243d");
        buttons_apply[1] = document.getElementById("lvd4ad59edbdc7545cb83ae6945c30f2f50");
        buttons_apply[2] = document.getElementById("lvdfc0d61975e8b427685fdcfafb5ef2401");
        buttons_apply[3] = document.getElementById("lvd08dd7a6152434c15891076bb21b18dca");
        buttons_apply[4] = document.getElementById("lvd9cb17666e2bf4da7b455438f270eba22");
        buttons_apply[5] = document.getElementById("lvd050aac7d7099428096fa6369db0b6216");
        buttons_apply[6] = document.getElementById("lvd1e53027d29f0417ea4180d1ce5957da9");
        buttons_apply[7] = document.getElementById("lvd0be3b5155c34432c95a299c5bf5f8fd1");
        buttons_apply[8] = document.getElementById("lvd0781b4844b314e55ba6df4b44ed8fc30");
        buttons_apply[9] = document.getElementById("lvd2cb7cb8d8bc14bef80b831e9a4175044");
        buttons_apply[10] = document.getElementById("lvd3e8aaa976de34a0cb4b4a85257407cd6");
        buttons_apply[11] = document.getElementById("lvdbd148542addf4ccfae9b7b7b402cf087");
        buttons_apply[12] = document.getElementById("lvd06bb003048344d37a121fdd21b048c19");
        buttons_apply[13] = document.getElementById("lvd97addbb839e446e289c61372347d247c");
        buttons_apply[14] = document.getElementById("lvd124ebbffbb984016bad0fbc111b6f063");
        buttons_apply[15] = document.getElementById("lvd2a4839d351c34d9b832bb45794d7aa05");
        buttons_apply[16] = document.getElementById("lvd303dfab8c5b447b6a4d5d3e3b553d31c");
        buttons_apply[17] = document.getElementById("lvd6cb05d8781914817b06b0ae228f0e4a9");
        buttons_apply[18] = document.getElementById("lvdf818bdfcdc504150b0565ab668e0d706");
        buttons_apply[19] = document.getElementById("lvda4b89890050f45ae9f9a826f7c1dcee2");
        buttons_apply[20] = document.getElementById("lvdbc844d1e5a90477b9829f383324bc21a");
        buttons_apply[21] = document.getElementById("lvdf904249488204374974528a437608f38");
        buttons_apply[22] = document.getElementById("lvddce327cf988b450db501965f11d9bd80");
        buttons_apply[23] = document.getElementById("lvd67c5d684e6164a188725ab3166147d42");
              button_back = document.getElementById("lvdfac52dd2a12f4855b9df81b10395db0b");
        ApplySettings.initApplySettings(buttons_apply, button_back);
        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) {
        return new ApplyZoneForOptimization(buttons_apply,button_back);
    }

}

class ApplySettings {

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

    constructor(api, buttons_apply, button_back) {

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

        this.buttons_apply = buttons_apply;
        this.button_back = button_back;
        this._registerEvents(buttons_apply, button_back);
    }

    _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, button_back) {

        let key = 0;
        let Button1 = 0;
        let Button2 = 0;


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

            key = 0;

        });



        for (let index = 0; index < buttons_apply.length; index++) {
            buttons_apply[index].addEventListener("click", () => {

                            key++;
                            if (key === 1) {
                                Button1 = index;
                                buttons_apply[index].style.border = "medium solid #0000FF";
                            }
                            if (key === 2) {
                                Button2 = index;
                                buttons_apply[Button1].style.border = "";
                                key = 0;
                                if (Button1 < Button2) {
                                    for (let ind = Button1; ind <= Button2; ind++) {
                                        this.SetTuningPart(ind,1);
                                    }
                                }
                                else if (Button1 > Button2) {
                                    for (let ind = Button2; ind <= Button1; ind++) {
                                        this.SetTuningPart(ind,0);
                                    }
                                }
                                else {
                                    this.ReadAndSetTuningPart(Button1);
                                }
                            }
            });
        } // END FOR

        if ((this.eventMediator !== null) && (this.eventMediator !== undefined) && (this.events !== null) && (this.events !== undefined)) {
            this.eventMediator.subscribe(this.events.getUserDefinedInternalEvent('OpenWindowApplySettings'), () => {

                        for (let index = 0; index < buttons_apply.length; index++) {

                                buttons_apply[index].style.border = "";
                                buttons_apply[index].style.backgroundColor = "#BEBEBE";
                            }
                        });
        }
    }

    SetTuningPart(index,k){
        switch (index) {
            case 0:
                this.datapointAPI.writeDataPoint('10:SetPointManager1.OPTuningParticipation', k);
                break;
            case 1:
                this.datapointAPI.writeDataPoint('10:SetPointManager2.OPTuningParticipation', k);
                break;
            case 2:
                this.datapointAPI.writeDataPoint('10:SetPointManager3.OPTuningParticipation', k);
                break;
            case 3:
                this.datapointAPI.writeDataPoint('10:SetPointManager4.OPTuningParticipation', k);
                break;
            case 4:
                this.datapointAPI.writeDataPoint('10:SetPointManager5.OPTuningParticipation', k);
                break;
            case 5:
                this.datapointAPI.writeDataPoint('10:SetPointManager6.OPTuningParticipation', k);
                break;
            case 6:
                this.datapointAPI.writeDataPoint('10:SetPointManager7.OPTuningParticipation', k);
                break;
            case 7:
                this.datapointAPI.writeDataPoint('10:SetPointManager8.OPTuningParticipation', k);
                break;
            case 8:
                this.datapointAPI.writeDataPoint('10:SetPointManager9.OPTuningParticipation', k);
                break;
            case 9:
                this.datapointAPI.writeDataPoint('10:SetPointManager10.OPTuningParticipation', k);
                break;
            case 10:
                this.datapointAPI.writeDataPoint('10:SetPointManager11.OPTuningParticipation', k);
                break;
            case 11:
                this.datapointAPI.writeDataPoint('10:SetPointManager12.OPTuningParticipation', k);
                break;
            case 12:
                this.datapointAPI.writeDataPoint('10:SetPointManager13.OPTuningParticipation', k);
                break;
            case 13:
                this.datapointAPI.writeDataPoint('10:SetPointManager14.OPTuningParticipation', k);
                break;
            case 14:
                this.datapointAPI.writeDataPoint('10:SetPointManager15.OPTuningParticipation', k);
                break;
            case 15:
                this.datapointAPI.writeDataPoint('10:SetPointManager16.OPTuningParticipation', k);
                break;
            case 16:
                this.datapointAPI.writeDataPoint('10:SetPointManager17.OPTuningParticipation', k);
                break;
            case 17:
                this.datapointAPI.writeDataPoint('10:SetPointManager18.OPTuningParticipation', k);
                break;
            case 18:
                this.datapointAPI.writeDataPoint('10:SetPointManager19.OPTuningParticipation', k);
                break;
            case 19:
                this.datapointAPI.writeDataPoint('10:SetPointManager20.OPTuningParticipation', k);
                break;
            case 20:
                this.datapointAPI.writeDataPoint('10:SetPointManager21.OPTuningParticipation', k);
                break;
            case 21:
                this.datapointAPI.writeDataPoint('10:SetPointManager22.OPTuningParticipation', k);
                break;
            case 22:
                this.datapointAPI.writeDataPoint('10:SetPointManager23.OPTuningParticipation', k);
                break;
            case 23:
                this.datapointAPI.writeDataPoint('10:SetPointManager24.OPTuningParticipation', k);
                break;
        }
    }

    ReadAndSetTuningPart(index){
        switch (index) {
            case 0:
                this.ifFunktion('10:SetPointManager1.OPTuningParticipation', 0);
                break;
            case 1:
                this.ifFunktion('10:SetPointManager2.OPTuningParticipation', 1);
                break;
            case 2:
                this.ifFunktion('10:SetPointManager3.OPTuningParticipation', 2);
                break;
            case 3:
                this.ifFunktion('10:SetPointManager4.OPTuningParticipation', 3);
                break;
            case 4:
                this.ifFunktion('10:SetPointManager5.OPTuningParticipation', 4);
                break;
            case 5:
                this.ifFunktion('10:SetPointManager6.OPTuningParticipation', 5);
                break;
            case 6:
                this.ifFunktion('10:SetPointManager7.OPTuningParticipation', 6);
                break;
            case 7:
                this.ifFunktion('10:SetPointManager8.OPTuningParticipation', 7);
                break;
            case 8:
                this.ifFunktion('10:SetPointManager9.OPTuningParticipation', 8);
                break;
            case 9:
                this.ifFunktion('10:SetPointManager10.OPTuningParticipation', 9);
                break;
            case 10:
                this.ifFunktion('10:SetPointManager11.OPTuningParticipation', 10);
                break;
            case 11:
                this.ifFunktion('10:SetPointManager12.OPTuningParticipation', 11);
                break;
            case 12:
                this.ifFunktion('10:SetPointManager13.OPTuningParticipation', 12);
                break;
            case 13:
                this.ifFunktion('10:SetPointManager14.OPTuningParticipation', 13);
                break;
            case 14:
                this.ifFunktion('10:SetPointManager15.OPTuningParticipation', 14);
                break;
            case 15:
                this.ifFunktion('10:SetPointManager16.OPTuningParticipation', 15);
                break;
            case 16:
                this.ifFunktion('10:SetPointManager17.OPTuningParticipation', 16);
                break;
            case 17:
                this.ifFunktion('10:SetPointManager18.OPTuningParticipation', 17);
                break;
            case 18:
                this.ifFunktion('10:SetPointManager19.OPTuningParticipation', 18);
                break;
            case 19:
                this.ifFunktion('10:SetPointManager20.OPTuningParticipation', 19);
                break;
            case 20:
                this.ifFunktion('10:SetPointManager21.OPTuningParticipation', 20);
                break;
            case 21:
                this.ifFunktion('10:SetPointManager22.OPTuningParticipation', 21);
                break;
            case 22:
                this.ifFunktion('10:SetPointManager23.OPTuningParticipation', 22);
                break;
            case 23:
                this.ifFunktion('10:SetPointManager24.OPTuningParticipation', 23);
                break;
        }
    }

    ifFunktion(DataPoint, index){
        this.datapointAPI.readDataPoint(DataPoint, 0)
            .then((resolve) => {
                if (resolve.value===0) {
                    this.SetTuningPart(index,1);
                }
                else
                {
                    this.SetTuningPart(index,0);
                }
            });
    }



}
        window.registerDashboardModule('ApplyZoneForOptimization', ApplyZoneForOptimization.create);
