import { State } from '../../model';
import { VariableState } from '../../communication';
import { ConditionalEnableElement } from './conditional-enable-element';
import { forEach } from 'lodash';
import { VariableUtil } from '../../utils/variable-util';
export class ReadableElement extends ConditionalEnableElement {
    constructor(element, permissionChecker, variableCommunicator, variableStore, localization, signalRAppId) {
        super(element, permissionChecker, variableCommunicator, variableStore, localization, signalRAppId);
    }
    init(variableName) {
        if (variableName) {
            let hasVariable = false;
            forEach(this.elementStates, elementState => {
                if (elementState.variableName === variableName) {
                    hasVariable = true;
                }
            });
            if (!hasVariable) {
                this.elementStates.push(new VariableState(variableName, undefined));
            }
        }
        this.initState();
    }
    get readVariableName() {
        if (!this.model) {
            throw new Error('Model cannot be null');
        }
        if (null == this.model.version || this.model.version < 2) {
            return this.model.variableName;
        }
        else {
            const variable = this.model.readVariable;
            if (!variable) {
                return undefined;
            }
            return VariableUtil.getConvertedVariableName(this.variableStore, variable);
        }
    }
    get minVariableName() {
        if (!this.model.minValueType) {
            return undefined;
        }
        return this.getVariableName(this.model.minVariable);
    }
    get minValueType() {
        return this.model.minValueType;
    }
    get maxVariableName() {
        if (!this.model.maxValueType) {
            return undefined;
        }
        return this.getVariableName(this.model.maxVariable);
    }
    get maxValueType() {
        return this.model.maxValueType;
    }
    getVariableName(variable) {
        if (!this.model) {
            throw new Error('Model cannot be null');
        }
        if (!variable) {
            return undefined;
        }
        return VariableUtil.getConvertedVariableName(this.variableStore, variable);
    }
    reportStateChanged(variableState) {
        forEach(this.elementStates, elementState => {
            if (elementState.variableName === variableState.variableName) {
                elementState.state = variableState.state;
                this.changeStates();
            }
        });
    }
    reportValueChanged(value) {
        var _a, _b, _c, _d;
        forEach(this.elementStates, elementState => {
            if (elementState.variableName === value.variableName) {
                elementState.state = value.state;
                this.changeStates();
            }
        });
        // 处理系统变量"设备状态"
        if (value.systemName === '设备状态') {
            let name = value.customStatus;
            const language = ((_b = (_a = window.abp.localization) === null || _a === void 0 ? void 0 : _a.currentLanguage) === null || _b === void 0 ? void 0 : _b.name) || ((_d = (_c = this.localization.localizationService) === null || _c === void 0 ? void 0 : _c.translate) === null || _d === void 0 ? void 0 : _d.currentLang);
            const isChinese = language === 'zh-Hans' || language === 'zh';
            if (!name) {
                switch (value.state) {
                    case 1:
                        name = isChinese ? '在线' : 'Online';
                        break;
                    case 3:
                        name = isChinese ? '离线' : 'Offline';
                        break;
                    case 4:
                        name = isChinese ? '告警' : 'Alarming';
                        break;
                    case 5:
                        name = isChinese ? '未绑定' : 'Unbound';
                        break;
                    default:
                }
            }
            this.updateVariableValue(name, '设备状态');
            return; // 处理完"设备状态"后直接返回
        }
        // 处理系统变量"当前语种ID"
        if (value.variableName === '当前语种ID') {
            // 直接使用变量值（数字或null）
            this.updateVariableValue(value.value, value.variableName);
            return;
        }
        // 处理普通变量
        if (this.state === State.Normal || this.state === State.Disable
            || value.variableName === this.minVariableName
            || value.variableName === this.maxVariableName) {
            this.updateVariableValue(value.value, value.variableName);
        }
    }
}
