let buttons = [];
let GroupID = 0;
let SelectedGroupID = 0;

let counterGr = [0,0,0,0,0,0,0,0,0,0,0];

class CodeModuleClass {

    constructor(buttons) {
        this.buttons = buttons;

    }

    init(mainViewModel) {
        this.mainViewModel = mainViewModel;

        buttons[0] = document.getElementById("lvd6179e037620f44e8a8c1cf67a2f08d26");
        buttons[1] = document.getElementById("lvd064645b85aa5446e84d7c836156e90e3");
        buttons[2] = document.getElementById("lvdb61d3168773b43d8a2561e724c8b6859");
        buttons[3] = document.getElementById("lvd812fc5eba0b849ab920020bb24881f58");
        buttons[4] = document.getElementById("lvd6ba9243fb52c47e28015406771ab969b");
        buttons[5] = document.getElementById("lvd0466795aeb1e499cbcc01ed3168a11ac");
        buttons[6] = document.getElementById("lvda5e983bbbf354dfbb0725fcc5c683107");

        buttons[7] = document.getElementById("lvd0ddf4f38985c47c78d50604ca44f0edb");
        buttons[8] = document.getElementById("lvda13a28d110264fce85e0aa1ead4af165");
        buttons[9] = document.getElementById("lvdf12406b5b4ab44a9b6f23817b5321ba5");
        buttons[10] = document.getElementById("lvd11ddacaedc464f049386ba8f987fc3b9");
        buttons[11] = document.getElementById("lvd448bb7d58d6449eab148139073316e99");
        buttons[12] = document.getElementById("lvd7a3e79dd067745e1a55e4c2157edaef5");

        buttons[13] = document.getElementById("lvd1e17e0b8c597452a94e3b7ecf7707a02");
        buttons[14] = document.getElementById("lvda384d1e6f6614118b6a8aa94c8c7af06");
        buttons[15] = document.getElementById("lvdcab94c3b5b3b4a4fb4f5903f4745b038");
        buttons[16] = document.getElementById("lvda45612a04695486bbb36d90543f53f08");
        buttons[17] = document.getElementById("lvdffedb7c708a24566a453e932d8d81b7c");
        buttons[18] = document.getElementById("lvd1910898639ce4492a06546e9a2175aaf");

        buttons[19] = document.getElementById("lvdff07e54e7e5542e9ac04b9fea2018c31");
        buttons[20] = document.getElementById("lvd5060242fb7434f7b8bec3e24e4820603");
        buttons[21] = document.getElementById("lvdef4c810984eb4309ab28635116a3d8f2");
        buttons[22] = document.getElementById("lvd1476114f57944bc7ac9522f8d9f64404");
        buttons[23] = document.getElementById("lvd6087f43a8cb3458f98ad9f0c6b1d9e0c");

        DatapointManagerGroupID.initDemoDatapointManager(buttons);

        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) {
        return new CodeModuleClass(buttons);
    }

}

class DatapointManagerGroupID {

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

    constructor(api, buttons) {
        this.events = null;
        this.eventMediator = null;
        this.datapointAPI = null;
        this._initAPIs(api);

        this. buttons =  buttons;

        this._registerEvents(buttons);

    }

//##################################################################################################
// Alle benoetigten APIs zuweisen
//##################################################################################################
    _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) {

        let key = 0;
        let Button1 = 0;
        let Button2 = 0;
        let MultipleSelectionOn = 0;
        let RampOn;

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

                this.datapointAPI.readDataPoint('10:Global_Set1.MultipleSelectionOfZones', 0)
                    .then((resolve) => {
                        MultipleSelectionOn = resolve.value;
                        if (MultipleSelectionOn === 2) {


                            this.datapointAPI.readDataPoint('10:SelectedGroup1.GroupID', 0)
                                .then((resolve) => {
                                    SelectedGroupID = resolve.value;

                                    this.switchFunktion(index,RampOn);
                                })
                        }
                    })

            });
        }


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

                    this.datapointAPI.readDataPoint('10:Global_Set1.MultipleSelectionOfZones', 0)
                        .then((resolve) => {
                            MultipleSelectionOn = resolve.value;
                            this.datapointAPI.readDataPoint('10:Global_Set1.GlobalRamp', 0)
                                .then((resolve) => {
                                    RampOn = resolve.value;




                                    if (MultipleSelectionOn === 1) {

                                        key++;

                                        if (key === 1) {
                                            Button1 = index;
                                            buttons[index].style.border = "medium solid #0000FF";
                                        }

                                        if (key === 2) {
                                            Button2 = index;

                                            buttons[Button1].style.border = "";

                                            key = 0;

                                            this.datapointAPI.readDataPoint('10:SelectedGroup1.GroupID', 0)
                                                .then((resolve) => {
                                                    SelectedGroupID = resolve.value;

                                                    if (Button1 <= Button2) {
                                                        for (let ind = Button1; ind <= Button2; ind++) {
                                                            this.switchFunktion(ind,RampOn);
                                                        }
                                                    } else {
                                                        this.switchFunktion(Button2,RampOn);
                                                        this.switchFunktion(Button1,RampOn);
                                                    }

                                                    // this.counter();

                                                })

                                        }
                                    } // END IF MULTIPLE SELECTION
                                    else if (MultipleSelectionOn === 0) {

                                        this.datapointAPI.readDataPoint('10:SelectedGroup1.GroupID', 0)
                                            .then((resolve) => {
                                                SelectedGroupID = resolve.value;
                                                this.switchFunktion(index,RampOn);

                                            })

                                    }  // END ELSE MULTIPLE SELECTION

                                });});



                }
            );

        } // END FOR






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

            this.datapointAPI.readDataPoint('10:SelectedGroup1.GroupID', 0)
                .then((resolve) => {
                    SelectedGroupID = resolve.value;

                    for (let index = 0; index < buttons.length; index++) {
                        this.switchFunktionDelete(index,RampOn);

                    } // END FOR

                    // entsprechende Gruppe auf OFF setzen:

                    switch (SelectedGroupID) {
                        case 1:   this.datapointAPI.writeDataPoint('10:HelperClass1.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup1.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup1.GroupColorScheme', 2661195519);

                            counterGr[0]=0;
                            break;

                        case 2:   this.datapointAPI.writeDataPoint('10:HelperClass2.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup2.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup2.GroupColorScheme', 2661195519);

                            counterGr[1]=0;
                            break;

                        case 3:   this.datapointAPI.writeDataPoint('10:HelperClass3.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup3.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup3.GroupColorScheme', 2661195519);

                            counterGr[2]=0;
                            break;

                        case 4:   this.datapointAPI.writeDataPoint('10:HelperClass4.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup4.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup4.GroupColorScheme', 2661195519);

                            counterGr[3]=0;
                            break;

                        case 5:   this.datapointAPI.writeDataPoint('10:HelperClass5.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup5.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup5.GroupColorScheme', 2661195519);

                            counterGr[4]=0;
                            break;

                        case 6:   this.datapointAPI.writeDataPoint('10:HelperClass6.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup6.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup6.GroupColorScheme', 2661195519);

                            counterGr[5]=0;
                            break;

                        case 7:   this.datapointAPI.writeDataPoint('10:HelperClass7.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup7.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup7.GroupColorScheme', 2661195519);

                            counterGr[6]=0;
                            break;

                        case 8:   this.datapointAPI.writeDataPoint('10:HelperClass8.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup8.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup8.GroupColorScheme', 2661195519);

                            counterGr[7]=0;
                            break;

                        case 9:   this.datapointAPI.writeDataPoint('10:HelperClass9.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup9.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup9.GroupColorScheme', 2661195519);

                            counterGr[8]=0;
                            break;

                        case 10:  this.datapointAPI.writeDataPoint('10:HelperClass10.GroupOn', 0);
                            this.datapointAPI.writeDataPoint('10:PIDGroup10.GroupName', '-');
                            this.datapointAPI.writeDataPoint('10:PIDGroup10.GroupColorScheme', 2661195519);

                            counterGr[9]=0;
                            break;
                    }

                })


        });
    }

    selectZone(ZoneName) {
        this.datapointAPI.readDataPoint(ZoneName, 0)
            .then((resolve) => {
                GroupID = resolve.value;
                if (GroupID !== SelectedGroupID) {

                    this.datapointAPI.writeDataPoint(ZoneName, SelectedGroupID)
                        .then(() => {
                            counterGr[SelectedGroupID-1] = counterGr[SelectedGroupID-1]+1;
                        });

                } else {
                    this.datapointAPI.writeDataPoint(ZoneName, 0)
                        .then(() => {
                            counterGr[SelectedGroupID-1] = counterGr[SelectedGroupID-1]-1;
                        });
                }
            });
    }

    switchFunktion(index,RampOn){

    /*    if (RampOn === 0){

            switch (index) {
                case 0:  this.selectZone('10:Zone1.GroupID'); break;
                case 1:  this.selectZone('10:Zone2.GroupID'); break;
                case 2:  this.selectZone('10:Zone3.GroupID'); break;
                case 3:  this.selectZone('10:Zone4.GroupID'); break;
                case 4:  this.selectZone('10:Zone5.GroupID'); break;
                case 5:  this.selectZone('10:Zone6.GroupID'); break;
                case 6:  this.selectZone('10:Zone7.GroupID'); break;
                case 7:  this.selectZone('10:Zone8.GroupID'); break;
                case 8:  this.selectZone('10:Zone9.GroupID'); break;
                case 9:  this.selectZone('10:Zone10.GroupID'); break;
                case 10: this.selectZone('10:Zone11.GroupID'); break;
                case 11: this.selectZone('10:Zone12.GroupID'); break;
                case 12: this.selectZone('10:Zone13.GroupID'); break;
                case 13: this.selectZone('10:Zone14.GroupID'); break;
                case 14: this.selectZone('10:Zone15.GroupID'); break;
                case 15: this.selectZone('10:Zone16.GroupID'); break;
                case 16: this.selectZone('10:Zone17.GroupID'); break;
                case 17: this.selectZone('10:Zone18.GroupID'); break;
                case 18: this.selectZone('10:Zone19.GroupID'); break;
                case 19: this.selectZone('10:Zone20.GroupID'); break;
                case 20: this.selectZone('10:Zone21.GroupID'); break;
                case 21: this.selectZone('10:Zone22.GroupID'); break;
                case 22: this.selectZone('10:Zone23.GroupID'); break;
                case 23: this.selectZone('10:Zone24.GroupID'); break;
            }

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

    switchFunktionDelete(index,RampOn) {

        /*if (RampOn === 0){
            switch (index) {
                case 0:
                    this.selectZoneDelete('10:Zone1.GroupID');
                    break;
                case 1:
                    this.selectZoneDelete('10:Zone2.GroupID');
                    break;
                case 2:
                    this.selectZoneDelete('10:Zone3.GroupID');
                    break;
                case 3:
                    this.selectZoneDelete('10:Zone4.GroupID');
                    break;
                case 4:
                    this.selectZoneDelete('10:Zone5.GroupID');
                    break;
                case 5:
                    this.selectZoneDelete('10:Zone6.GroupID');
                    break;
                case 6:
                    this.selectZoneDelete('10:Zone7.GroupID');
                    break;
                case 7:
                    this.selectZoneDelete('10:Zone8.GroupID');
                    break;
                case 8:
                    this.selectZoneDelete('10:Zone9.GroupID');
                    break;
                case 9:
                    this.selectZoneDelete('10:Zone10.GroupID');
                    break;
                case 10:
                    this.selectZoneDelete('10:Zone11.GroupID');
                    break;
                case 11:
                    this.selectZoneDelete('10:Zone12.GroupID');
                    break;
                case 12:
                    this.selectZoneDelete('10:Zone13.GroupID');
                    break;
                case 13:
                    this.selectZoneDelete('10:Zone14.GroupID');
                    break;
                case 14:
                    this.selectZoneDelete('10:Zone15.GroupID');
                    break;
                case 15:
                    this.selectZoneDelete('10:Zone16.GroupID');
                    break;
                case 16:
                    this.selectZoneDelete('10:Zone17.GroupID');
                    break;
                case 17:
                    this.selectZoneDelete('10:Zone18.GroupID');
                    break;
                case 18:
                    this.selectZoneDelete('10:Zone19.GroupID');
                    break;
                case 19:
                    this.selectZoneDelete('10:Zone20.GroupID');
                    break;
                case 20:
                    this.selectZoneDelete('10:Zone21.GroupID');
                    break;
                case 21:
                    this.selectZoneDelete('10:Zone22.GroupID');
                    break;
                case 22:
                    this.selectZoneDelete('10:Zone23.GroupID');
                    break;
                case 23:
                    this.selectZoneDelete('10:Zone24.GroupID');
                    break;
            }

        }
        else
        {*/

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

   //     }

    }

    selectZoneDelete(ZoneName) {
        this.datapointAPI.readDataPoint(ZoneName, 0)
            .then((resolve) => {
                GroupID = resolve.value;
                if (GroupID === SelectedGroupID) {
                    this.datapointAPI.writeDataPoint(ZoneName, 0);
                }
            });
    }

    switchGroupID(GrID, counterGr){

        switch (GrID) {
            case 1:
                counterGr[0]=counterGr[0]+1;
                break;
            case 2:
                counterGr[1]=counterGr[1]+1;
                break;
            case 3:
                counterGr[2]=counterGr[2]+1;
                break;
            case 4:
                counterGr[3]=counterGr[3]+1;
                break;
            case 5:
                counterGr[4]=counterGr[4]+1;
                break;
            case 6:
                counterGr[5]=counterGr[5]+1;
                break;
            case 7:
                counterGr[6]=counterGr[6]+1;
                break;
            case 8:
                counterGr[7]=counterGr[7]+1;
                break;
            case 9:
                counterGr[8]=counterGr[8]+1;
                break;
            case 10:
                counterGr[9]=counterGr[9]+1;
                break;
            case 11:
                counterGr[10]=counterGr[10]+1;
                break;
        }
    }



}
window.registerDashboardModule('GroupDetail_Assistent', CodeModuleClass.create);