import { VariableValueType } from './variable-value-type';
import { ConditionItemObserver } from './condition-item-observer';
import { VariableValue, VariableStateEnum, VariableState } from '../../../communication';
import { RelationType } from './relation-type';
import { RelationalOperator } from './relational-operator';
import { RelationConditionResultState } from './relation-condition-result-state';
import { VariableUtil } from '../../../utils/variable-util';
import { VariableDefinition } from '../../../communication/variable/variable-definition';
export class WordConditionItemObserver extends ConditionItemObserver {
    constructor(wordCondition, variableCommunicator, variableStore) {
        super(wordCondition, variableStore);
        this.wordCondition = wordCondition;
        this.variableCommunicator = variableCommunicator;
    }
    get baseVariable() {
        const variableValue = this.wordCondition.baseVariableValue;
        if (!variableValue || !variableValue.variableName) {
            return;
        }
        const variable = new VariableDefinition(variableValue.variableName, variableValue.variableGroupName, variableValue.dataSourceCode, variableValue.variableVersion);
        return VariableUtil.getConvertedVariableName(this.variableStore, variable);
    }
    get realtionVariable() {
        const variableValue = this.wordCondition.realtionVariableValue;
        if (!variableValue || !variableValue.variableName) {
            return;
        }
        const variable = new VariableDefinition(variableValue.variableName, variableValue.variableGroupName, variableValue.dataSourceCode, variableValue.variableVersion);
        return VariableUtil.getConvertedVariableName(this.variableStore, variable);
    }
    requestData(appId) {
        if (!this.wordCondition) {
            throw new Error('WordConditionModel cannot be null.');
        }
        if (!this.wordCondition.baseVariableValue) {
            throw new Error('WordConditionModel.baseVariableValue cannot be null.');
        }
        // eslint-disable-next-line eqeqeq
        if (this.wordCondition.baseVariableValue.valueType == VariableValueType.Constant) {
            this.baseVariableValue = new VariableValue(undefined, VariableStateEnum.DataNormal, this.wordCondition.baseVariableValue.constValue);
        }
        else {
            if (this.variableName) {
                this.variableCommunicator.subscribeVariableState(this.variableName).subscribe(variableValue => {
                    if (variableValue.state === VariableStateEnum.Normal) {
                        this.requestconditionEffectiveBaseValue(this.variableCommunicator, appId);
                    }
                    else {
                        this.conditionEffectiveChanged.emit(new RelationConditionResultState(new VariableState(this.variableName, variableValue.state), false));
                    }
                });
            }
        }
        if (+this.wordCondition.relationType !== 0) {
            if (!this.wordCondition.baseVariableValue) {
                throw new Error('WordConditionModel.realtionVariableValue cannot be null.');
            }
            // eslint-disable-next-line eqeqeq
            if (this.wordCondition.realtionVariableValue.valueType == VariableValueType.Constant) {
                this.realtionVariableValue = new VariableValue(undefined, VariableStateEnum.DataNormal, this.wordCondition.realtionVariableValue.constValue);
            }
            else {
                if (this.variableName) {
                    this.variableCommunicator.subscribeVariableState(this.variableName).subscribe(variableState => {
                        if (variableState.state === VariableStateEnum.Normal) {
                            this.requestconditionEffectiveRealtionValue(this.variableCommunicator, appId);
                        }
                        else {
                            this.conditionEffectiveChanged.emit(new RelationConditionResultState(new VariableState(this.variableName, variableState.state), false));
                        }
                    });
                }
                else {
                    this.requestconditionEffectiveRealtionValue(this.variableCommunicator, appId);
                }
            }
        }
        if (this.variableName) {
            this.variableCommunicator.subscribeVariableState(this.variableName).subscribe(variableValue => {
                if (variableValue.state === VariableStateEnum.Normal) {
                    this.requestconditionEffectiveValue(this.variableCommunicator, appId);
                }
                else {
                    this.conditionEffectiveChanged.emit(new RelationConditionResultState(new VariableState(this.variableName, variableValue.state), false));
                }
            });
        }
    }
    requestconditionEffectiveValue(variableCommunicator, appId = '') {
        variableCommunicator.openVariable(this.variableName, appId).subscribe(variableValue => {
            this.currentValue = variableValue;
            this.isConditionEffective = this.getIsConditionEffective();
            this.conditionEffectiveChanged.emit(new RelationConditionResultState(new VariableState(this.variableName, variableValue.state), this.isConditionEffective));
        });
    }
    requestconditionEffectiveBaseValue(variableCommunicator, appId = '') {
        variableCommunicator.openVariable(this.baseVariable, appId).subscribe(variableValue => {
            this.baseVariableValue = variableValue;
            this.isConditionEffective = this.getIsConditionEffective();
            this.conditionEffectiveChanged.emit(new RelationConditionResultState(new VariableState(this.variableName, variableValue.state), this.isConditionEffective));
        });
    }
    requestconditionEffectiveRealtionValue(variableCommunicator, appId = '') {
        variableCommunicator.openVariable(this.realtionVariable, appId).subscribe(variableValue => {
            this.realtionVariableValue = variableValue;
            this.isConditionEffective = this.getIsConditionEffective();
            this.conditionEffectiveChanged.emit(new RelationConditionResultState(new VariableState(this.variableName, variableValue.state), this.isConditionEffective));
        });
    }
    getIsConditionEffective() {
        // eslint-disable-next-line eqeqeq
        if (this.wordCondition.relationType == RelationType.None) {
            return this.comparator(this.baseVariableValue, this.wordCondition.baseVariableValue.operator);
            // eslint-disable-next-line eqeqeq
        }
        else if (this.wordCondition.relationType == RelationType.And) {
            return this.comparator(this.baseVariableValue, this.wordCondition.baseVariableValue.operator)
                && this.comparator(this.realtionVariableValue, this.wordCondition.realtionVariableValue.operator);
            // eslint-disable-next-line eqeqeq
        }
        else if (this.wordCondition.relationType == RelationType.Or) {
            return this.comparator(this.baseVariableValue, this.wordCondition.baseVariableValue.operator)
                || this.comparator(this.realtionVariableValue, this.wordCondition.realtionVariableValue.operator);
        }
    }
    comparator(comparatorValue, relationalOperator) {
        if (comparatorValue == null) {
            return false;
        }
        if (this.currentValue == null) {
            return false;
        }
        if (comparatorValue.state !== VariableStateEnum.DataNormal || (this.currentValue.state !== VariableStateEnum.DataNormal)) {
            return false;
        }
        // eslint-disable-next-line eqeqeq
        if (relationalOperator == RelationalOperator.Equal) {
            // eslint-disable-next-line eqeqeq
            return (+this.currentValue.value) == (+comparatorValue.value);
            // eslint-disable-next-line eqeqeq
        }
        else if (relationalOperator == RelationalOperator.GreaterOrEqual) {
            // eslint-disable-next-line eqeqeq
            return (+this.currentValue.value) > (+comparatorValue.value) || (+this.currentValue.value) == (+comparatorValue.value);
            // eslint-disable-next-line eqeqeq
        }
        else if (relationalOperator == RelationalOperator.GreaterThan) {
            return (+this.currentValue.value) > (+comparatorValue.value);
            // eslint-disable-next-line eqeqeq
        }
        else if (relationalOperator == RelationalOperator.LessOrEqual) {
            // eslint-disable-next-line eqeqeq
            return (+this.currentValue.value) < (+comparatorValue.value) || (+this.currentValue.value) == (+comparatorValue.value);
            // eslint-disable-next-line eqeqeq
        }
        else if (relationalOperator == RelationalOperator.LessThan) {
            return (+this.currentValue.value) < (+comparatorValue.value);
            // eslint-disable-next-line eqeqeq
        }
        else if (relationalOperator == RelationalOperator.NotEqual) {
            // eslint-disable-next-line eqeqeq
            return (+this.currentValue.value) != (+comparatorValue.value);
        }
    }
}
