class PutCrosses {

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

    constructor(api) {
        this.events = null;
        this.eventMediator = null;
        this.datapointAPI = null;
        this._initAPIs(api);
        this._registerEvents();
    }


    _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() {


        let i;
        let PowerLimitation = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

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

                //pruefen, ob Sensortest bereits sbgeschlossen ist:

                this.datapointAPI.readDataPoint('10:DaisyChain1.VorOrAfterTest', 0)
                    .then((resolve) => {
                        if (resolve.value === 1){
                            this.datapointAPI.readDataPoint('10:Zone1.PowerLimitationVal', 0)
                                .then((resolve) => {
                                    PowerLimitation[0] = resolve.value;
                                    this.datapointAPI.readDataPoint('10:Zone2.PowerLimitationVal', 0)
                                        .then((resolve) => {
                                            PowerLimitation[1] = resolve.value;
                                            this.datapointAPI.readDataPoint('10:Zone3.PowerLimitationVal', 0)
                                                .then((resolve) => {
                                                    PowerLimitation[2] = resolve.value;
                                                    this.datapointAPI.readDataPoint('10:Zone4.PowerLimitationVal', 0)
                                                        .then((resolve) => {
                                                            PowerLimitation[3] = resolve.value;
                                                            this.datapointAPI.readDataPoint('10:Zone5.PowerLimitationVal', 0)
                                                                .then((resolve) => {
                                                                    PowerLimitation[4] = resolve.value;
                                                                    this.datapointAPI.readDataPoint('10:Zone6.PowerLimitationVal', 0)
                                                                        .then((resolve) => {
                                                                            PowerLimitation[5] = resolve.value;
                                                                            this.datapointAPI.readDataPoint('10:Zone7.PowerLimitationVal', 0)
                                                                                .then((resolve) => {
                                                                                    PowerLimitation[6] = resolve.value;
                                                                                    this.datapointAPI.readDataPoint('10:Zone8.PowerLimitationVal', 0)
                                                                                        .then((resolve) => {
                                                                                            PowerLimitation[7] = resolve.value;
                                                                                            this.datapointAPI.readDataPoint('10:Zone9.PowerLimitationVal', 0)
                                                                                                .then((resolve) => {
                                                                                                    PowerLimitation[8] = resolve.value;
                                                                                                    this.datapointAPI.readDataPoint('10:Zone10.PowerLimitationVal', 0)
                                                                                                        .then((resolve) => {
                                                                                                            PowerLimitation[9] = resolve.value;
                                                                                                            this.datapointAPI.readDataPoint('10:Zone11.PowerLimitationVal', 0)
                                                                                                                .then((resolve) => {
                                                                                                                    PowerLimitation[10] = resolve.value;
                                                                                                                    this.datapointAPI.readDataPoint('10:Zone12.PowerLimitationVal', 0)
                                                                                                                        .then((resolve) => {
                                                                                                                            PowerLimitation[11] = resolve.value;
                                                                                                                            this.datapointAPI.readDataPoint('10:Zone13.PowerLimitationVal', 0)
                                                                                                                                .then((resolve) => {
                                                                                                                                    PowerLimitation[12] = resolve.value;
                                                                                                                                    this.datapointAPI.readDataPoint('10:Zone14.PowerLimitationVal', 0)
                                                                                                                                        .then((resolve) => {
                                                                                                                                            PowerLimitation[13] = resolve.value;
                                                                                                                                            this.datapointAPI.readDataPoint('10:Zone15.PowerLimitationVal', 0)
                                                                                                                                                .then((resolve) => {
                                                                                                                                                    PowerLimitation[14] = resolve.value;
                                                                                                                                                    this.datapointAPI.readDataPoint('10:Zone16.PowerLimitationVal', 0)
                                                                                                                                                        .then((resolve) => {
                                                                                                                                                            PowerLimitation[15] = resolve.value;
                                                                                                                                                            this.datapointAPI.readDataPoint('10:Zone17.PowerLimitationVal', 0)
                                                                                                                                                                .then((resolve) => {
                                                                                                                                                                    PowerLimitation[16] = resolve.value;
                                                                                                                                                                    this.datapointAPI.readDataPoint('10:Zone18.PowerLimitationVal', 0)
                                                                                                                                                                        .then((resolve) => {
                                                                                                                                                                            PowerLimitation[17] = resolve.value;
                                                                                                                                                                            this.datapointAPI.readDataPoint('10:Zone19.PowerLimitationVal', 0)
                                                                                                                                                                                .then((resolve) => {
                                                                                                                                                                                    PowerLimitation[18] = resolve.value;
                                                                                                                                                                                    this.datapointAPI.readDataPoint('10:Zone20.PowerLimitationVal', 0)
                                                                                                                                                                                        .then((resolve) => {
                                                                                                                                                                                            PowerLimitation[19] = resolve.value;
                                                                                                                                                                                            this.datapointAPI.readDataPoint('10:Zone21.PowerLimitationVal', 0)
                                                                                                                                                                                                .then((resolve) => {
                                                                                                                                                                                                    PowerLimitation[20] = resolve.value;
                                                                                                                                                                                                    this.datapointAPI.readDataPoint('10:Zone22.PowerLimitationVal', 0)
                                                                                                                                                                                                        .then((resolve) => {
                                                                                                                                                                                                            PowerLimitation[21] = resolve.value;
                                                                                                                                                                                                            this.datapointAPI.readDataPoint('10:Zone23.PowerLimitationVal', 0)
                                                                                                                                                                                                                .then((resolve) => {
                                                                                                                                                                                                                    PowerLimitation[22] = resolve.value;
                                                                                                                                                                                                                    this.datapointAPI.readDataPoint('10:Zone24.PowerLimitationVal', 0)
                                                                                                                                                                                                                        .then((resolve) => {
                                                                                                                                                                                                                            PowerLimitation[23] = resolve.value;

                                                                                                                                                                                                                            for (i = 0; i < 24; i++) {


                                                                                                                                                                                                                                if (PowerLimitation[i] === 0)  {
                                                                                                                                                                                                                                    this.setCross(i);
                                                                                                                                                                                                                                }

                                                                                                                                                                                                                            }



                                                                                                                                                                                                                        });});});});});});});});});});});});});});});});});});});});});});});});





                        }
                    });
            });
        }
    }

    setCross(i){

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

PutCrosses.initPutCrosses();