import { WordSwitchOperation } from '../../model';
import { OperationHelper } from '../../shared';
import { VariableStateEnum } from '../../communication';
import { VariableUtil } from '../../utils/variable-util';
import { VariableDefinition } from '../../communication/variable/variable-definition';
export class WordSwitchOperator {
    constructor(settings, variableCommunicator, variableStore, signalRAppId) {
        this.settings = settings;
        this.variableCommunicator = variableCommunicator;
        this.variableStore = variableStore;
        this.signalRAppId = signalRAppId;
        if (!this.settings) {
            throw new Error('settings cannot be null');
        }
        if (this.settings.isWriteOnly) {
            this.canExecuteInternal = true;
        }
        else {
            variableCommunicator.openVariable(this.variableName, this.signalRAppId).subscribe(value => {
                this.currentValue = value;
                this.canExecuteInternal = this.currentValue && this.currentValue.state === VariableStateEnum.DataNormal;
            });
        }
    }
    get variableName() {
        const variable = new VariableDefinition(this.settings.variableName, this.settings.variableGroupName, this.settings.dataSourceCode, this.settings.variableVersion);
        return VariableUtil.getConvertedVariableName(this.variableStore, variable);
    }
    canExecute() {
        return this.canExecuteInternal;
    }
    execute() {
        if (!this.canExecute()) {
            throw new Error('Can not execute.');
        }
        const settings = this.settings;
        let newValue;
        switch (+settings.operation) {
            case WordSwitchOperation.Add:
                if (!this.settings.isWriteOnly) {
                    newValue = OperationHelper.add(+this.currentValue.value, settings.operand, settings.dataType, settings.maxValue);
                    this.variableCommunicator.write(this.variableName, newValue).subscribe();
                }
                break;
            case WordSwitchOperation.Subtract:
                if (!this.settings.isWriteOnly) {
                    newValue = OperationHelper.subtract(+this.currentValue.value, settings.operand, settings.dataType, settings.minValue);
                    this.variableCommunicator.write(this.variableName, newValue).subscribe();
                }
                break;
            case WordSwitchOperation.SetConstant:
                // TODO考虑操作数超过数据类型上下限的问题。
                newValue = settings.operand;
                this.variableCommunicator.write(this.variableName, newValue).subscribe();
                break;
            default:
                throw new Error(`Unknown WordSwitchOperator:${settings.operation}`);
        }
        let originValue;
        if (this.currentValue && this.currentValue.value !== undefined) {
            originValue = this.currentValue.value;
        }
        return Promise.resolve({
            variableName: this.variableName,
            originValue: originValue,
            newValue: newValue
        });
    }
    // 此方法为 位设定添加的，字设定目前不会用到，为了解决接口error，添加一个空实现
    writeValue(value) {
        return Promise.resolve({
            newValue: value
        });
    }
}
