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("lvde26925464e9a41ee80cfd328cf19df03");
        buttons[1] = document.getElementById("lvd4160e0e0e672445183f8049ac456138f");
        buttons[2] = document.getElementById("lvd622d29feae4940c19aa92041abf55d05");
        buttons[3] = document.getElementById("lvd858564bbac7b4a7580724e766139a727");
        buttons[4] = document.getElementById("lvdb90ec183fdb64f1c9e115e5ed2aaee53");
        buttons[5] = document.getElementById("lvd66398b562c894b0280fcfb3e8bd215f3");
        buttons[6] = document.getElementById("lvdf19e1e4537564a9583a5872f5b82df61");

        buttons[7] = document.getElementById("lvd06f298b1cf4f461788c1d139b1e462e4");
        buttons[8] = document.getElementById("lvd9292ae5867bc45049e4bb83fed1c1f97");
        buttons[9] = document.getElementById("lvd241aade5c8d548c19496de50bca0273a");
        buttons[10] = document.getElementById("lvd3ca8f34ba5a1494fa52c52c363cda7df");
        buttons[11] = document.getElementById("lvd5cb99bfc6f8b4cb09b33250992b3eacd");
        buttons[12] = document.getElementById("lvd07020e86309c4d51b6c2112f1626a464");

        buttons[13] = document.getElementById("lvde4fd94968862477285df2d41b2f2d03c");
        buttons[14] = document.getElementById("lvd7c2a0936a76144de83dfc05c63b34489");
        buttons[15] = document.getElementById("lvdd574b2dca77047e9a952e9a934098bde");
        buttons[16] = document.getElementById("lvd9a669a169d8645cfbbc6d24ae9c91e31");
        buttons[17] = document.getElementById("lvd53c4378608084b16822b052c5512c7d3");
        buttons[18] = document.getElementById("lvd1c0efb1fc9934ab1bc7c62070c645541");

        buttons[19] = document.getElementById("lvde8ec3cb342f34d6199ba1fbfefb0e7e0");
        buttons[20] = document.getElementById("lvd21ea8716cf1d47158f8e38fece06c2b3");
        buttons[21] = document.getElementById("lvd91eb21a2e4954fd5a5c78e74c1ae8381");
        buttons[22] = document.getElementById("lvd69b56bc770d94cec9439a334a6cf1c16");
        buttons[23] = document.getElementById("lvd4b3e45410de24317a50b57e88c2dfe93");

        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;

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

                                        key++;

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

                                            this.datapointAPI.readDataPoint('10:SelectedGroup1.GroupID', 0)
                                                .then((resolve) => {
                                                    SelectedGroupID = resolve.value;
                                                    this.switchFunktion_one_button(Button1);
                                                })
                                        }
                                        if (key === 2) {
                                            Button2 = index;
                                            buttons[Button1].style.border = "";
                                            key = 0;
                                            if (Button1 < Button2) {
                                                        for (let ind = Button1+1; ind <= Button2; ind++) {
                                                            this.switchFunktion(ind);
                                                        }
                                            } else if (Button1 > Button2)
                                            {
                                                        for (let ind = Button2; ind < Button1; ind++) {
                                                            this.switchFunktion_cancel(ind);
                                                        }
                                            } else { //der Fall Button1 = Button2
                                                       // this.switchFunktion_one_button(Button1);
                                            }
                                        }
            }
            );

        } // END FOR

    }

    switchFunktion(index){

            switch (index) {
                case 0:  this.selectZone('10:Zone1.ZoneNotExist'); this.selectZone_1('10:Zone1.PowerLimitationVal');this.selectZone_1('10:Zone1.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager1.GroupID_LVD');break;
                case 1:  this.selectZone('10:Zone2.ZoneNotExist'); this.selectZone_1('10:Zone2.PowerLimitationVal');this.selectZone_1('10:Zone2.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager2.GroupID_LVD');break;
                case 2:  this.selectZone('10:Zone3.ZoneNotExist'); this.selectZone_1('10:Zone3.PowerLimitationVal');this.selectZone_1('10:Zone3.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager3.GroupID_LVD');break;
                case 3:  this.selectZone('10:Zone4.ZoneNotExist'); this.selectZone_1('10:Zone4.PowerLimitationVal');this.selectZone_1('10:Zone4.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager4.GroupID_LVD');break;
                case 4:  this.selectZone('10:Zone5.ZoneNotExist'); this.selectZone_1('10:Zone5.PowerLimitationVal');this.selectZone_1('10:Zone5.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager5.GroupID_LVD');break;
                case 5:  this.selectZone('10:Zone6.ZoneNotExist'); this.selectZone_1('10:Zone6.PowerLimitationVal');this.selectZone_1('10:Zone6.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager6.GroupID_LVD');break;
                case 6:  this.selectZone('10:Zone7.ZoneNotExist'); this.selectZone_1('10:Zone7.PowerLimitationVal');this.selectZone_1('10:Zone7.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager7.GroupID_LVD');break;
                case 7:  this.selectZone('10:Zone8.ZoneNotExist'); this.selectZone_1('10:Zone8.PowerLimitationVal');this.selectZone_1('10:Zone8.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager8.GroupID_LVD');break;
                case 8:  this.selectZone('10:Zone9.ZoneNotExist'); this.selectZone_1('10:Zone9.PowerLimitationVal');this.selectZone_1('10:Zone9.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager9.GroupID_LVD');break;
                case 9:  this.selectZone('10:Zone10.ZoneNotExist'); this.selectZone_1('10:Zone10.PowerLimitationVal');this.selectZone_1('10:Zone10.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager10.GroupID_LVD');break;
                case 10: this.selectZone('10:Zone11.ZoneNotExist'); this.selectZone_1('10:Zone11.PowerLimitationVal');this.selectZone_1('10:Zone11.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager11.GroupID_LVD');break;
                case 11: this.selectZone('10:Zone12.ZoneNotExist'); this.selectZone_1('10:Zone12.PowerLimitationVal');this.selectZone_1('10:Zone12.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager12.GroupID_LVD');break;
                case 12: this.selectZone('10:Zone13.ZoneNotExist'); this.selectZone_1('10:Zone13.PowerLimitationVal');this.selectZone_1('10:Zone13.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager13.GroupID_LVD');break;
                case 13: this.selectZone('10:Zone14.ZoneNotExist'); this.selectZone_1('10:Zone14.PowerLimitationVal');this.selectZone_1('10:Zone14.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager14.GroupID_LVD');break;
                case 14: this.selectZone('10:Zone15.ZoneNotExist'); this.selectZone_1('10:Zone15.PowerLimitationVal');this.selectZone_1('10:Zone15.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager15.GroupID_LVD');break;
                case 15: this.selectZone('10:Zone16.ZoneNotExist'); this.selectZone_1('10:Zone16.PowerLimitationVal');this.selectZone_1('10:Zone16.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager16.GroupID_LVD');break;
                case 16: this.selectZone('10:Zone17.ZoneNotExist'); this.selectZone_1('10:Zone17.PowerLimitationVal');this.selectZone_1('10:Zone17.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager17.GroupID_LVD');break;
                case 17: this.selectZone('10:Zone18.ZoneNotExist'); this.selectZone_1('10:Zone18.PowerLimitationVal');this.selectZone_1('10:Zone18.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager18.GroupID_LVD');break;
                case 18: this.selectZone('10:Zone19.ZoneNotExist'); this.selectZone_1('10:Zone19.PowerLimitationVal');this.selectZone_1('10:Zone19.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager19.GroupID_LVD');break;
                case 19: this.selectZone('10:Zone20.ZoneNotExist'); this.selectZone_1('10:Zone20.PowerLimitationVal');this.selectZone_1('10:Zone20.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager20.GroupID_LVD');break;
                case 20: this.selectZone('10:Zone21.ZoneNotExist'); this.selectZone_1('10:Zone21.PowerLimitationVal');this.selectZone_1('10:Zone21.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager21.GroupID_LVD');break;
                case 21: this.selectZone('10:Zone22.ZoneNotExist'); this.selectZone_1('10:Zone22.PowerLimitationVal');this.selectZone_1('10:Zone22.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager22.GroupID_LVD');break;
                case 22: this.selectZone('10:Zone23.ZoneNotExist'); this.selectZone_1('10:Zone23.PowerLimitationVal');this.selectZone_1('10:Zone23.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager23.GroupID_LVD');break;
                case 23: this.selectZone('10:Zone24.ZoneNotExist'); this.selectZone_1('10:Zone24.PowerLimitationVal');this.selectZone_1('10:Zone24.ZoneTestColorScheme');this.selectZone_1('10:SetPointManager24.GroupID_LVD');break;
            }
    }

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

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

    selectZone(ZoneName) {this.datapointAPI.writeDataPoint(ZoneName, 1)}
    selectZone_1(ZoneName) {this.datapointAPI.writeDataPoint(ZoneName, 0)}

    selectZone_one_button(ZoneName) {
        this.datapointAPI.readDataPoint(ZoneName, 0)
            .then((resolve) => {
                if (resolve.value === 0){
                    this.datapointAPI.writeDataPoint(ZoneName, 1)
                }else {
                    this.datapointAPI.writeDataPoint(ZoneName, 0)
                }
            });
    }

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