import { EventEmitter } from '@angular/core';
import { last, sortBy } from 'lodash';
import { ConditionHelper } from '../../model';
import { VariableStateEnum, VariableState } from '../../communication';
import { CurrentStateIdValue } from './current-stateId-value';
import { VariableUtil } from '../../utils/variable-util';
import { VariableDefinition } from '../../communication/variable/variable-definition';
export class WordIndicatorLightOperator {
    constructor(settings, states, variableCommunicator, variableStore) {
        this.settings = settings;
        this.variableCommunicator = variableCommunicator;
        this.variableStore = variableStore;
        this.currentStateIdChanged = new EventEmitter();
        if (!this.settings) {
            throw new Error('settings cannot be null');
        }
        this.sortedStates = sortBy(states, ['id']);
    }
    get variableName() {
        const variable = new VariableDefinition(this.settings.variableName, this.settings.variableGroupName, this.settings.dataSourceCode, this.settings.variableVersion);
        return VariableUtil.getConvertedVariableName(this.variableStore, variable);
    }
    requestData(appId) {
        if (this.variableName) {
            this.variableCommunicator.subscribeVariableState(this.variableName).subscribe(variableValue => {
                if (variableValue.state === VariableStateEnum.Normal) {
                    this.requestCurrentSateIdValue(this.variableCommunicator, appId);
                }
                else {
                    this.currentStateIdChanged.emit(new CurrentStateIdValue(new VariableState(this.variableName, variableValue.state)));
                }
            });
        }
        else {
            this.requestCurrentSateIdValue(this.variableCommunicator, appId);
        }
    }
    requestCurrentSateIdValue(variableCommunicator, appId = '') {
        // TODO 检查状态id是从0-n连续编号的。
        variableCommunicator.openVariable(this.variableName, appId).subscribe(value => {
            if (value.state === VariableStateEnum.DataNormal) {
                let stateId = 0;
                for (let index = 0; index < this.sortedStates.length - 1; index++) {
                    const element = this.sortedStates[index];
                    const condition = element.condition;
                    if (!condition) {
                        throw new Error(`condition is not WordCondition.stateId:${element.id}`);
                    }
                    stateId = element.id;
                    if (ConditionHelper.isSatisfied(condition.operator, condition.operand, +value.value)) {
                        this.currentStateId = stateId;
                        this.currentStateIdChanged.emit(new CurrentStateIdValue(new VariableState(this.variableName, value.state), this.currentStateId));
                        return;
                    }
                }
                this.currentStateId = last(this.sortedStates).id;
                this.currentStateIdChanged.emit(new CurrentStateIdValue(new VariableState(this.variableName, value.state), this.currentStateId));
            }
            else {
                delete this.currentStateId;
                this.currentStateIdChanged.emit(new CurrentStateIdValue(new VariableState(this.variableName, value.state)));
            }
        });
    }
}
