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

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

class ShowToolClass {

    constructor(buttons) {
        this.buttons = buttons;

    }

    init(mainViewModel) {
        this.mainViewModel = mainViewModel;

        buttons[0] = document.getElementById("lvd145ee7daf0a945619a7f879af2639325");
        buttons[1] = document.getElementById("lvde20597ba0fe54813898cbbc78272fbab");
        buttons[2] = document.getElementById("lvd01c970484ae2445ca9b4a880ff50a46d");
        buttons[3] = document.getElementById("lvd5bb10ae1a1094a2e843933914c673979");
        buttons[4] = document.getElementById("lvd569e7cfcec504a8aba5b82f774cbf8d1");
        buttons[5] = document.getElementById("lvd1a2ec6eeaad446bfb53f25ff3f68ed95");
        buttons[6] = document.getElementById("lvdc1c1c64671ac402d865519c8ad8d2d1e");

        buttons[7] = document.getElementById("lvdd78a4b2cc55f42d8bdf8ce016aed451d");
        buttons[8] = document.getElementById("lvdb407004f62654636b869079f3f6fce49");
        buttons[9] = document.getElementById("lvdacd20b5ee49543d0ba1317acdee93fd7");
        buttons[10] = document.getElementById("lvdcb4b121bcb0a4dc089e3dc0aa728b688");
        buttons[11] = document.getElementById("lvd021f0fb091a4477987e8fe512a6e4ef6");
        buttons[12] = document.getElementById("lvd1ef48c169a354104aee5a0bc2e608c04");

        buttons[13] = document.getElementById("lvd946e6e0d33be48968eed96909eaa2d26");
        buttons[14] = document.getElementById("lvd93ed277233e84191bb1b33591120f076");
        buttons[15] = document.getElementById("lvdf55c4be4f9d14bd1bf79f6332aaa12a6");
        buttons[16] = document.getElementById("lvd4cbaa9f0b77f4423a6319cd822105f93");
        buttons[17] = document.getElementById("lvdf4441af6a0b2418a909e58156d62c575");
        buttons[18] = document.getElementById("lvd8c3ac38e7eb847d180b54abd2603593b");

        buttons[19] = document.getElementById("lvd32e6ed1fc7034954847e655895ae0f80");
        buttons[20] = document.getElementById("lvd59041b4194564b86ac2e40555e69c28e");
        buttons[21] = document.getElementById("lvd8293014254504a90930968f61d9b6475");
        buttons[22] = document.getElementById("lvd717944ff11f74ccea9bb12c98edb744c");
        buttons[23] = document.getElementById("lvd98552506983b48bcb20e508e85a5e90a");

        ShowToolClassManager.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 ShowToolClass(buttons);
    }

}

class ShowToolClassManager {

    static initDemoDatapointManager(buttons) {
        if (window.sigApi !== undefined) {
            const fm = new ShowToolClassManager(window.sigApi, buttons);
        } else {
            // ist sigApi noch nicht geladen, als EventListener fuer 'SigApiReady' registrieren
            window.addEventListener('SigApiReady', () => {
                const fm = new ShowToolClassManager(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";
                    }

                    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);
                                    }
                                } 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:GroupIDVisu1.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:GroupIDVisu2.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:GroupIDVisu3.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:GroupIDVisu4.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:GroupIDVisu5.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:GroupIDVisu6.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:GroupIDVisu7.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:GroupIDVisu8.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:GroupIDVisu9.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:GroupIDVisu10.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:GroupIDVisu11.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:GroupIDVisu12.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:GroupIDVisu13.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:GroupIDVisu14.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:GroupIDVisu15.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:GroupIDVisu16.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:GroupIDVisu17.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:GroupIDVisu18.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:GroupIDVisu19.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:GroupIDVisu20.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:GroupIDVisu21.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:GroupIDVisu22.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:GroupIDVisu23.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:GroupIDVisu24.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('Show_Tool', ShowToolClass.create);