import { BitConditonLogic, ConditionItemsResultObserver } from '../../model';
import { forEach } from 'lodash';
import { ConditionType } from '../../model/shared/condition/condition-type';
import { RelationType } from '../../model/shared/condition/relation-type';
import { VariableState } from '../../communication';
import { StateControlElement } from './state-control-element';
import { VariableUtil } from '../../utils/variable-util';
import { VariableDefinition } from '../../communication/variable/variable-definition';
export class ConditionalControlElement extends StateControlElement {
    constructor(element, permissionChecker, variableCommunicator, variableStore, localization, signalRAppId) {
        super(element, permissionChecker, variableCommunicator, variableStore, localization, signalRAppId);
        this.variableCommunicator = variableCommunicator;
        this.variableStore = variableStore;
        this.recoverData();
        setTimeout(() => {
            this.initConditionalControl();
        });
    }
    initConditionalControl() {
        if (this.model.isControl === false) {
            return;
        }
        if (null == this.model.controlItems || !this.model.controlItems.length) {
            return;
        }
        const controlConditionResult = new ConditionItemsResultObserver(this.model.controlItems, this.variableCommunicator, this.variableStore);
        controlConditionResult.conditionResultChanged.subscribe(result => {
            this.updateElement(result);
        });
        controlConditionResult.requestData(this.signalRAppId);
    }
    recoverData() {
        if (!this.model) {
            throw new Error('Model cannot be null');
        }
        if (null == this.model.version || this.model.version < 1) {
            if (this.model.variableName) {
                this.model.controlItems = new Array();
                const bitCondition = {
                    variableName: this.model.variableName,
                    bitConditionLogic: BitConditonLogic.On
                };
                const condition = {
                    index: 0,
                    conditionType: ConditionType.Bit,
                    condition: bitCondition,
                    relationType: RelationType.None
                };
                this.model.controlItems.push(condition);
                const variable = new VariableDefinition(condition.condition.variableName, condition.condition.variableGroupName, condition.condition.dataSourceCode, condition.condition.variableVersion);
                this.addElementState(new VariableState(VariableUtil.getConvertedVariableName(this.variableStore, variable), undefined));
                this.initState();
            }
        }
        else if (this.model.version < 2) {
            if (this.model.readVariable
                && this.model.readVariable.name
                && this.model.readVariable.groupName) {
                this.model.controlItems = new Array();
                const bitCondition = {
                    variableName: this.model.readVariable.name,
                    variableGroupName: this.model.readVariable.groupName,
                    bitConditionLogic: BitConditonLogic.On
                };
                const condition = {
                    index: 0,
                    conditionType: ConditionType.Bit,
                    condition: bitCondition,
                    relationType: RelationType.None
                };
                this.model.controlItems.push(condition);
                const variable = new VariableDefinition(condition.condition.variableName, condition.condition.variableGroupName, condition.condition.dataSourceCode, condition.condition.variableVersion);
                this.addElementState(new VariableState(VariableUtil.getConvertedVariableName(this.variableStore, variable), undefined));
                this.initState();
            }
        }
        else {
            forEach(this.model.controlItems, controlItem => {
                if (controlItem.condition.variableName) {
                    const variable = new VariableDefinition(controlItem.condition.variableName, controlItem.condition.variableGroupName, controlItem.condition.dataSourceCode, controlItem.condition.variableVersion);
                    this.addElementState(new VariableState(VariableUtil.getConvertedVariableName(this.variableStore, variable), undefined));
                    this.initState();
                }
            });
        }
    }
}
