
class SetGlobalForGroup {

    static initSetGlobalForGroup() {
        if (window.sigApi !== undefined) {
            const fm = new SetGlobalForGroup(window.sigApi);
        } else {
            // ist sigApi noch nicht geladen, als EventListener fuer 'SigApiReady' registrieren
            window.addEventListener('SigApiReady', () => {
                const fm = new SetGlobalForGroup(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 GlobalLowerValue;
        let GlobalBoostValue;
        let GlobalBoostTime;

        if ((this.eventMediator !== null) && (this.eventMediator !== undefined) && (this.events !== null) && (this.events !== undefined)) {
            this.eventMediator.subscribe(this.events.getUserDefinedInternalEvent('SetGlobalForGroup'), () => {
                this.datapointAPI.readDataPoint('10:Global_Set1.LowerValue', 0)
                    .then((resolve)=>
                    {
                        GlobalLowerValue = resolve.value;
                        this.datapointAPI.readDataPoint('10:Global_Set1.BoostValue', 0)
                            .then((resolve)=>
                            {
                                GlobalBoostValue = resolve.value;
                                this.datapointAPI.readDataPoint('10:Global_Set1.BoostTime', 0)
                                    .then((resolve)=>
                                    {
                                        GlobalBoostTime = resolve.value;

                                        // For Gruppe 1:

                                        this.datapointAPI.writeDataPoint('10:GroupControl1.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl1.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl1.BoostTime', GlobalBoostTime);

                                        // For Gruppe 2:

                                        this.datapointAPI.writeDataPoint('10:GroupControl2.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl2.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl2.BoostTime', GlobalBoostTime);

                                        // For Gruppe 3:

                                        this.datapointAPI.writeDataPoint('10:GroupControl3.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl3.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl3.BoostTime', GlobalBoostTime);

                                        // For Gruppe 4:

                                        this.datapointAPI.writeDataPoint('10:GroupControl4.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl4.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl4.BoostTime', GlobalBoostTime);

                                        // For Gruppe 5:

                                        this.datapointAPI.writeDataPoint('10:GroupControl5.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl5.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl5.BoostTime', GlobalBoostTime);

                                        // For Gruppe 6:

                                        this.datapointAPI.writeDataPoint('10:GroupControl6.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl6.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl6.BoostTime', GlobalBoostTime);

                                        // For Gruppe 7:

                                        this.datapointAPI.writeDataPoint('10:GroupControl7.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl7.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl7.BoostTime', GlobalBoostTime);

                                        // For Gruppe 8:

                                        this.datapointAPI.writeDataPoint('10:GroupControl8.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl8.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl8.BoostTime', GlobalBoostTime);

                                        // For Gruppe 9:

                                        this.datapointAPI.writeDataPoint('10:GroupControl9.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl9.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl9.BoostTime', GlobalBoostTime);

                                        // For Gruppe 10:

                                        this.datapointAPI.writeDataPoint('10:GroupControl10.LoweringTemp', GlobalLowerValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl10.BoostTemp', GlobalBoostValue);
                                        this.datapointAPI.writeDataPoint('10:GroupControl10.BoostTime', GlobalBoostTime);

                                    });

                            });
                    });
            });


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


                                        // For Zone 1:
                                        this.datapointAPI.writeDataPoint('10:Zone1.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone1.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone1.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork1.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork1.LowParticipation', 1);


                                        // For Zone 2:
                                        this.datapointAPI.writeDataPoint('10:Zone2.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone2.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone2.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork2.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork2.LowParticipation', 1);

                                        // For Zone 3:
                                        this.datapointAPI.writeDataPoint('10:Zone3.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone3.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone3.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork3.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork3.LowParticipation', 1);

                                        // For Zone 4:
                                        this.datapointAPI.writeDataPoint('10:Zone4.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone4.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone4.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork4.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork4.LowParticipation', 1);

                                        // For Zone 5:
                                        this.datapointAPI.writeDataPoint('10:Zone5.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone5.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone5.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork5.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork5.LowParticipation', 1);

                                        // For Zone 6:
                                        this.datapointAPI.writeDataPoint('10:Zone6.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone6.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone6.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork6.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork6.LowParticipation', 1);

                                        // For Zone 7:
                                        this.datapointAPI.writeDataPoint('10:Zone7.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone7.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone7.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork7.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork7.LowParticipation', 1);

                                        // For Zone 8:
                                        this.datapointAPI.writeDataPoint('10:Zone8.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone8.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone8.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork8.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork8.LowParticipation', 1);

                                        // For Zone 9:
                                        this.datapointAPI.writeDataPoint('10:Zone9.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone9.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone9.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork9.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork9.LowParticipation', 1);

                                        // For Zone 10:
                                        this.datapointAPI.writeDataPoint('10:Zone10.ZoneOn', 1);
                                        this.datapointAPI.writeDataPoint('10:Zone10.SetPoint', 2500);
                                        this.datapointAPI.writeDataPoint('10:Zone10.OperationMode', 4);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork10.BooParticipation', 1);
                                        this.datapointAPI.writeDataPoint('10:ModeOfWork10.LowParticipation', 1);


                                        // For Gruppe 1:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup1.SetPoint', 2500);

                                        // For Gruppe 2:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup2.SetPoint', 2500);

                                        // For Gruppe 3:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup3.SetPoint', 2500);

                                        // For Gruppe 4:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup4.SetPoint', 2500);

                                        // For Gruppe 5:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup5.SetPoint', 2500);

                                        // For Gruppe 6:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup6.SetPoint', 2500);

                                        // For Gruppe 7:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup7.SetPoint', 2500);

                                        // For Gruppe 8:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup8.SetPoint', 2500);

                                        // For Gruppe 9:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup9.SetPoint', 2500);

                                        // For Gruppe 10:
                                        this.datapointAPI.writeDataPoint('10:PIDGroup10.SetPoint', 2500);

                                    });

        }
    }


    //##################################################################################################
    // Liest einen Datenpunkt
    //##################################################################################################
    onReadDataPoint() {
        console.log('onReadDataPoint()');
        const dbid = '1:MyClass1.ClassSvr';
        const quality = 0;
        if (this.datapointAPI !== null) {
            const ret = this.datapointAPI.readDataPoint(dbid, quality);
            ret.then(function (resolve) {
                console.log('read() ID=' + resolve.id + ' value=' + resolve.value);
            },
                function (reject) {
                    console.log('read() ID=' + reject.id + ' status=' + reject.status + ' Error=' + reject.errortxt);
                });
        }
    }


    //##################################################################################################
    // Schreibt einen Datenpunkt
    //##################################################################################################
    onWriteDataPoint() {
        console.log('onWriteDataPoint()');
        const dbid = '1:MyClass1.ClassSvr';
        const value = 123;
        if (this.datapointAPI !== null) {
            const ret = this.datapointAPI.writeDataPoint(dbid, value);
            ret.then(function (resolve) {
                console.log('write() ID=' + resolve.id + ' value=' + resolve.value);
            },
                function (reject) {
                    console.log('write() ID=' + reject.id + ' status=' + reject.status + ' Error=' + reject.errortxt);
                });
        }
    }

}

SetGlobalForGroup.initSetGlobalForGroup();