import { Color, Utils, Observable, Enums, Font, EditableTextBase, colorProperty, fontSizeProperty, fontInternalProperty, widthProperty, paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty } from '@nativescript/core';
import * as commonModule from './common';
import { AutoCompleteDisplayMode } from 'nativescript-ui-autocomplete';
export * from './common';
let DataFormCreateGroupClass;
let DataFormExpandedChangedListener;
let DataFormValidationListener;
let EditorFocusChangeListener;
let DataFormEditorCustomizations;
let DataFormEditorGroupCustomizations;
let DataFormEntityPropertyCommitListener;
let DataFormValidationInfoProcedure;
function initializeListeners() {
    if (!DataFormExpandedChangedListener) {
        var DataFormExpandedChangedListenerImpl = /** @class */ (function (_super) {
    __extends(DataFormExpandedChangedListenerImpl, _super);
    function DataFormExpandedChangedListenerImpl(owner, group) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        _this.group = group;
        return global.__native(_this);
    }
    DataFormExpandedChangedListenerImpl.prototype.onChanged = function (isExpanded) {
        var name = isExpanded ? commonModule.RadDataForm.groupExpandedEvent : commonModule.RadDataForm.groupCollapsedEvent;
        var propertyGroup = this.owner.getGroupByName(this.group.name());
        propertyGroup.collapsed = !isExpanded;
        var args = {
            eventName: name,
            object: this.owner,
            editor: undefined,
            entityProperty: undefined,
            propertyName: undefined,
            group: this.group,
            groupName: this.group.name(),
            returnValue: true
        };
        this.owner.notify(args);
    };
    DataFormExpandedChangedListenerImpl = __decorate([
        Interfaces([com.telerik.widget.dataform.visualization.ExpandableEditorGroup.IsExpandedChangedListener])
    ], DataFormExpandedChangedListenerImpl);
    return DataFormExpandedChangedListenerImpl;
}(java.lang.Object));
        DataFormExpandedChangedListener = DataFormExpandedChangedListenerImpl;
    }
    if (!DataFormCreateGroupClass) {
        var DataFormCreateGroupClassImpl = /** @class */ (function (_super) {
    __extends(DataFormCreateGroupClassImpl, _super);
    function DataFormCreateGroupClassImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    DataFormCreateGroupClassImpl.prototype._updateGroupLayout = function (propertyGroup, nativeGroup) {
        var context = nativeGroup.rootLayout().getContext();
        if (propertyGroup.layout instanceof commonModule.DataFormStackLayout) {
            var nativeLinearLayout = new com.telerik.widget.dataform.visualization.DataFormLinearLayoutManager(context);
            if (propertyGroup.layout.orientation === Enums.Orientation.horizontal) {
                nativeLinearLayout.setOrientation(android.widget.LinearLayout.HORIZONTAL);
            }
            else {
                nativeLinearLayout.setOrientation(android.widget.LinearLayout.VERTICAL);
            }
            nativeGroup.setLayoutManager(nativeLinearLayout);
        }
        else if (propertyGroup.layout instanceof commonModule.DataFormGridLayout) {
            var nativeTableLayout = new com.telerik.widget.dataform.visualization.DataFormTableLayoutManager(context);
            nativeGroup.setLayoutManager(nativeTableLayout);
        }
    };
    DataFormCreateGroupClassImpl.prototype.apply = function (context, name) {
        var dataForm = this.owner;
        if (dataForm.groups) {
            for (var i = 0; i < dataForm.groups.length; i++) {
                var propertyGroup = dataForm.groups[i];
                if (propertyGroup.name === name.toString()) {
                    var group_1 = new com.telerik.widget.dataform.visualization.ExpandableEditorGroup(context, name.toString());
                    if (!propertyGroup.collapsible) {
                        group_1.setExpandable(false);
                    }
                    var newListener = new DataFormExpandedChangedListener(this.owner, group_1);
                    this.owner.groupListeners.push(newListener);
                    group_1.addIsExpandedChangedListener(newListener);
                    if (propertyGroup.collapsed) {
                        group_1.setIsExpanded(false);
                    }
                    if (propertyGroup.hidden) {
                        group_1.rootLayout().setVisibility(android.view.View.GONE);
                    }
                    if (propertyGroup.titleHidden) {
                        group_1.getHeaderContainer().setVisibility(android.view.View.GONE);
                    }
                    if (!propertyGroup.titleStyle) {
                        propertyGroup.titleStyle = new commonModule.GroupTitleStyle();
                    }
                    if (!propertyGroup.layout) {
                        propertyGroup.layout = new commonModule.DataFormStackLayout();
                    }
                    this._updateGroupLayout(propertyGroup, group_1);
                    dataForm._attachGroupChangeListener(propertyGroup);
                    return group_1;
                }
            }
        }
        var group = new com.telerik.widget.dataform.visualization.ExpandableEditorGroup(context, name.toString());
        group.setExpandable(false);
        return group;
    };
    DataFormCreateGroupClassImpl = __decorate([
        Interfaces([com.telerik.android.common.Function2])
    ], DataFormCreateGroupClassImpl);
    return DataFormCreateGroupClassImpl;
}(java.lang.Object));
        DataFormCreateGroupClass = DataFormCreateGroupClassImpl;
    }
    if (!EditorFocusChangeListener) {
        var EditorFocusChangeListenerImpl = /** @class */ (function (_super) {
    __extends(EditorFocusChangeListenerImpl, _super);
    function EditorFocusChangeListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    EditorFocusChangeListenerImpl.prototype.onFocusChange = function (hasFocus) {
        if (hasFocus) {
            this.owner.notify({ eventName: EditableTextBase.focusEvent, object: this.owner });
        }
        else {
            this.owner.notify({ eventName: EditableTextBase.blurEvent, object: this.owner });
        }
    };
    EditorFocusChangeListenerImpl = __decorate([
        Interfaces([com.telerik.widget.dataform.engine.EditorFocusChangeListener])
    ], EditorFocusChangeListenerImpl);
    return EditorFocusChangeListenerImpl;
}(java.lang.Object));
        EditorFocusChangeListener = EditorFocusChangeListenerImpl;
    }
    if (!DataFormValidationListener) {
        var DataFormValidationListenerImpl = /** @class */ (function (_super) {
    __extends(DataFormValidationListenerImpl, _super);
    function DataFormValidationListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    DataFormValidationListenerImpl.prototype.onValidate = function (property) {
        var dataForm = this.owner;
        var entityProperty = dataForm.getPropertyByName(property.name());
        var group = dataForm.getGroupByName(property.getGroupName());
        var args = {
            eventName: commonModule.RadDataForm.propertyValidateEvent,
            object: dataForm,
            editor: entityProperty.editor,
            entityProperty: entityProperty,
            propertyName: property.name(),
            group: group,
            groupName: property.getGroupName(),
            returnValue: true
        };
        dataForm.notify(args);
        if (property instanceof com.telerik.widget.dataform.engine.EntityPropertyCore) {
            var result = Promise.resolve(args.returnValue);
            property.onValidationStarted();
            var validationValue_1 = entityProperty.valueCandidate;
            result.then(function (answer) {
                if (answer === false) {
                    property.onValidationResult(validationValue_1, false, entityProperty.errorMessage);
                }
                else {
                    property.onValidationResult(validationValue_1, true, entityProperty.successMessage);
                }
            });
        }
    };
    DataFormValidationListenerImpl.prototype.onDidValidate = function (property) {
        var dataForm = this.owner;
        if (!dataForm) {
            return;
        }
        var entityProperty = dataForm.getPropertyByName(property.name());
        var group = dataForm.getGroupByName(property.getGroupName());
        var args = {
            eventName: commonModule.RadDataForm.propertyValidatedEvent,
            object: dataForm,
            editor: entityProperty.editor,
            entityProperty: entityProperty,
            propertyName: property.name(),
            group: group,
            groupName: property.getGroupName(),
            returnValue: true
        };
        dataForm.notify(args);
    };
    DataFormValidationListenerImpl = __decorate([
        Interfaces([com.telerik.widget.dataform.engine.EntityPropertyValidationListener])
    ], DataFormValidationListenerImpl);
    return DataFormValidationListenerImpl;
}(java.lang.Object));
        DataFormValidationListener = DataFormValidationListenerImpl;
    }
    if (!DataFormEditorCustomizations) {
        var DataFormEditorCustomizationsImpl = /** @class */ (function (_super) {
    __extends(DataFormEditorCustomizationsImpl, _super);
    function DataFormEditorCustomizationsImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    DataFormEditorCustomizationsImpl.prototype.apply = function (editor) {
        var dataForm = this.owner;
        var property = dataForm.getPropertyByName(editor.property().name());
        if (property.editor) {
            PropertyEditorHelper.applyStyle(property.editor);
        }
        var groupName = property.android ? property.android.getGroupName() : undefined;
        var args = {
            eventName: commonModule.RadDataForm.editorUpdateEvent,
            object: dataForm,
            editor: editor,
            entityProperty: property.android,
            propertyName: property.name,
            group: undefined,
            groupName: groupName,
            returnValue: true
        };
        dataForm.notify(args);
    };
    DataFormEditorCustomizationsImpl = __decorate([
        Interfaces([com.telerik.android.common.Procedure])
    ], DataFormEditorCustomizationsImpl);
    return DataFormEditorCustomizationsImpl;
}(java.lang.Object));
        DataFormEditorCustomizations = DataFormEditorCustomizationsImpl;
    }
    if (!DataFormEditorGroupCustomizations) {
        var DataFormEditorGroupCustomizationsImpl = /** @class */ (function (_super) {
    __extends(DataFormEditorGroupCustomizationsImpl, _super);
    function DataFormEditorGroupCustomizationsImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    DataFormEditorGroupCustomizationsImpl.prototype._applyGroupTitleStyle = function (nativeGroup, titleStyle) {
        if (titleStyle.fillColor) {
            nativeGroup.getHeaderContainer().setBackgroundColor(titleStyle.fillColor.android);
        }
        if (titleStyle.strokeColor || titleStyle.strokeWidth) {
            var drawable = new android.graphics.drawable.GradientDrawable();
            var strokeWidthDips = titleStyle.strokeWidth ? titleStyle.strokeWidth : 2;
            var strokeWidth = strokeWidthDips * Utils.layout.getDisplayDensity();
            var strokeColor = titleStyle.strokeColor ? titleStyle.strokeColor.android : android.graphics.Color.BLACK;
            var fillColor = titleStyle.fillColor ? titleStyle.fillColor.android : android.graphics.Color.TRANSPARENT;
            drawable.setStroke(strokeWidth, strokeColor);
            drawable.setColor(fillColor);
            nativeGroup.getHeaderContainer().setBackgroundDrawable(drawable);
        }
        if (titleStyle.labelTextColor) {
            nativeGroup.getHeaderView().setTextColor(titleStyle.labelTextColor.android);
        }
        if (titleStyle.labelFontName || titleStyle.labelFontStyle) {
            var editorTypeface = RadDataForm._makeTypeface(titleStyle.labelFontName, titleStyle.labelFontStyle);
            nativeGroup.getHeaderView().setTypeface(editorTypeface);
        }
        if (titleStyle.labelTextSize) {
            nativeGroup.getHeaderView().setTextSize(titleStyle.labelTextSize);
        }
    };
    DataFormEditorGroupCustomizationsImpl.prototype.apply = function (editorGroup) {
        var dataForm = this.owner;
        var group = dataForm.getGroupByName(editorGroup.name());
        if (group !== null && group.titleStyle !== null) {
            this._applyGroupTitleStyle(editorGroup, group.titleStyle);
        }
        // throw event for additional customizations
        var groupName = editorGroup.name();
        var args = {
            eventName: commonModule.RadDataForm.groupUpdateEvent,
            object: dataForm,
            editor: undefined,
            entityProperty: undefined,
            propertyName: undefined,
            group: editorGroup,
            groupName: groupName,
            returnValue: true
        };
        dataForm.notify(args);
    };
    DataFormEditorGroupCustomizationsImpl = __decorate([
        Interfaces([com.telerik.android.common.Procedure])
    ], DataFormEditorGroupCustomizationsImpl);
    return DataFormEditorGroupCustomizationsImpl;
}(java.lang.Object));
        DataFormEditorGroupCustomizations = DataFormEditorGroupCustomizationsImpl;
    }
    if (!DataFormEntityPropertyCommitListener) {
        var DataFormEntityPropertyCommitListenerImpl = /** @class */ (function (_super) {
    __extends(DataFormEntityPropertyCommitListenerImpl, _super);
    function DataFormEntityPropertyCommitListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    DataFormEntityPropertyCommitListenerImpl.prototype.isUsingBooleanEditor = function (property) {
        return property.getEditorType() === com.telerik.widget.dataform.visualization.editors.DataFormSwitchEditor.class;
    };
    DataFormEntityPropertyCommitListenerImpl.prototype.isUsingNumericEditor = function (property) {
        return property.getEditorType() === com.telerik.widget.dataform.visualization.editors.DataFormDecimalEditor.class || property.getEditorType() === com.telerik.widget.dataform.visualization.editors.DataFormIntegerEditor.class || property.getEditorType() === com.telerik.widget.dataform.visualization.editors.DataFormNumberPickerEditor.class || property.getEditorType() === com.telerik.widget.dataform.visualization.editors.DataFormSeekBarEditor.class;
    };
    DataFormEntityPropertyCommitListenerImpl.prototype.isUsingDateTimeEditor = function (property) {
        return property.getEditorType() === com.telerik.widget.dataform.visualization.editors.DataFormDateEditor.class || property.getEditorType() === com.telerik.widget.dataform.visualization.editors.DataFormTimeEditor.class;
    };
    DataFormEntityPropertyCommitListenerImpl.prototype.convertToTypedValue = function (oldValue, newValue, nativeProperty) {
        // The newValue is of type object, we try to deduct the desired type mostly based
        // on the type of the old value, so we can cast the newValue to the correct type
        if (newValue === null) {
            return null;
        }
        if (typeof oldValue === 'number' || this.isUsingNumericEditor(nativeProperty)) {
            return Number(newValue);
        }
        if (typeof oldValue === 'boolean' || this.isUsingBooleanEditor(nativeProperty)) {
            return String(newValue) === 'true';
        }
        if (this.isUsingDateTimeEditor(nativeProperty)) {
            // The Date/Time Editors can edit properties of types Date and String.
            if (typeof oldValue === 'string') {
                return String(newValue);
            }
            else {
                return new Date(newValue);
            }
        }
        if (newValue instanceof java.util.ArrayList) {
            var jsArray = [];
            for (var i = 0; i < newValue.size(); i++) {
                jsArray.push(newValue.get(i));
            }
            return jsArray;
        }
        return newValue;
    };
    DataFormEntityPropertyCommitListenerImpl.prototype.onBeforeCommit = function (property) {
        var dataform = this.owner;
        var entityProperty = dataform.getPropertyByName(property.name());
        var args = {
            eventName: commonModule.RadDataForm.propertyCommitEvent,
            object: dataform,
            editor: undefined,
            entityProperty: entityProperty,
            propertyName: property.name(),
            group: undefined,
            groupName: property.getGroupName(),
            returnValue: true
        };
        dataform.notify(args);
        return !args.returnValue;
    };
    DataFormEntityPropertyCommitListenerImpl.prototype.onAfterCommit = function (property) {
        var dataform = this.owner;
        var entityProperty = dataform.getPropertyByName(property.name());
        if (dataform.source.hasOwnProperty(property.name())) {
            var oldValue = dataform.source[property.name()];
            var newValue = property.getValue();
            var typedValue = this.convertToTypedValue(oldValue, newValue, property);
            dataform.source[property.name()] = typedValue;
            var args = {
                eventName: commonModule.RadDataForm.propertyCommittedEvent,
                object: dataform,
                editor: undefined,
                entityProperty: entityProperty,
                propertyName: property.name(),
                group: undefined,
                groupName: property.getGroupName(),
                returnValue: true
            };
            dataform.notify(args);
        }
    };
    DataFormEntityPropertyCommitListenerImpl = __decorate([
        Interfaces([com.telerik.widget.dataform.engine.EntityPropertyCommitListener])
    ], DataFormEntityPropertyCommitListenerImpl);
    return DataFormEntityPropertyCommitListenerImpl;
}(java.lang.Object));
        DataFormEntityPropertyCommitListener = DataFormEntityPropertyCommitListenerImpl;
    }
    if (!DataFormValidationInfoProcedure) {
        var DataFormValidationInfoProcedureImpl = /** @class */ (function (_super) {
    __extends(DataFormValidationInfoProcedureImpl, _super);
    function DataFormValidationInfoProcedureImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    DataFormValidationInfoProcedureImpl.prototype.apply = function (info) {
        if (this.validateResolve) {
            this.validateResolve(!info.hasErrors());
        }
    };
    DataFormValidationInfoProcedureImpl = __decorate([
        Interfaces([com.telerik.android.common.Procedure])
    ], DataFormValidationInfoProcedureImpl);
    return DataFormValidationInfoProcedureImpl;
}(java.lang.Object));
        DataFormValidationInfoProcedure = DataFormValidationInfoProcedureImpl;
    }
}
export class RadDataForm extends commonModule.RadDataForm {
    constructor() {
        super(...arguments);
        this._loaded = false;
        this._groupListeners = [];
    }
    get groupListeners() {
        return this._groupListeners;
    }
    createNativeView() {
        initializeListeners();
        if (!this._android) {
            this._android = new com.telerik.widget.dataform.visualization.RadDataForm(this._context);
            this._layoutManager = new com.telerik.widget.dataform.visualization.DataFormGroupLayoutManager(this._context);
            this._android.setLayoutManager(this._layoutManager);
        }
        this._ngKey = Object.keys(this).find(key => key.startsWith('_ngcontent'));
        const _entityPropertyChangedHandler = function (propertyChangeData) {
            let property = propertyChangeData.object;
            if (!property._shouldSkipEditorUpdate || propertyChangeData.propertyName !== 'editor') {
                if (!this || !this._android || !this._loaded) {
                    return;
                }
                switch (propertyChangeData.propertyName) {
                    case 'index':
                    case 'hidden':
                    case 'editor':
                        this.reload();
                        break;
                }
            }
        };
        this.entityPropertyChangedHandler = _entityPropertyChangedHandler.bind(this);
        const _groupTitleStylePropertyChangedHandler = function (propertyChangeData) {
            if (!this || !this._android || !this._loaded) {
                return;
            }
            this._layoutManager.applyEditorGroupCustomizations();
        };
        this.groupTitleStylePropertyChangedHandler = _groupTitleStylePropertyChangedHandler.bind(this);
        const _groupLayoutPropertyChangedHandler = function (propertyChangeData) {
            if (!this || !this._android || !this._loaded) {
                return;
            }
            this.android.arrangeEditors();
        };
        this.groupLayoutPropertyChangedHandler = _groupLayoutPropertyChangedHandler.bind(this);
        const _groupPropertyChangedHandler = function (propertyChangeData) {
            if (!this || !this._android || !this._loaded) {
                return;
            }
            switch (propertyChangeData.propertyName) {
                case 'collapsed':
                    let propertyGroup = propertyChangeData.object;
                    if (!propertyGroup.collapsible) {
                        // If the group is not collapsible, we don't want to collapse it.
                        if (propertyChangeData.value) {
                            console.log('WARNING: collapsible should be true before collapsing a group.');
                        }
                        return;
                    }
                    let nativeGroup = this.getNativeGroup(propertyGroup.name);
                    nativeGroup.setIsExpanded(!propertyChangeData.value);
                    break;
                case 'hidden':
                case 'titleHidden':
                case 'collapsible':
                case 'layout':
                    this._android.arrangeEditors();
                    if (propertyChangeData.propertyName === 'layout') {
                        this._attachGroupLayoutChangeListener(undefined, propertyChangeData.value);
                    }
                    break;
                case 'titleStyle':
                    this._layoutManager.applyEditorGroupCustomizations();
                    this._attachGroupTitleStyleChangeListener(undefined, propertyChangeData.value);
                    break;
                case 'name':
                    let group = propertyChangeData.object;
                    if (group.properties) {
                        for (let i = 0; i < group.properties.length; i++) {
                            let property = group.properties[i];
                            if (property.android) {
                                property.android.setGroupName(group.name);
                            }
                        }
                        this.reload();
                    }
                    break;
            }
        };
        this.groupPropertyChangedHandler = _groupPropertyChangedHandler.bind(this);
        this._setupGroups();
        this._updateEditorStyles();
        this._updateGroupStyles();
        this._updateSource();
        this._updateIsReadOnly();
        this._updateCommitMode();
        this._updateValidationMode();
        this._updateMetadata();
        this._addValidationListener();
        this._addCommitListener();
        this._loaded = true;
        return this._android;
    }
    disposeNativeView() {
        if (this._android._createGroupFunction) {
            this._android._createGroupFunction.owner = null;
        }
        if (this._android._editorCustomizations) {
            this._android._editorCustomizations.owner = null;
        }
        if (this._android._editorGroupCustomizations) {
            this._android._editorGroupCustomizations.owner = null;
        }
        if (this._android._validationListener) {
            this._android._validationListener.owner = null;
        }
        if (this._android._entityPropertyCommitListener) {
            this._android._entityPropertyCommitListener.owner = null;
        }
        if (this._android._validationInfoProcedure) {
            this._android._validationInfoProcedure.owner = null;
        }
        for (let i = this.groupListeners.length; i >= 0; i--) {
            if (this.groupListeners[i]) {
                this.groupListeners[i].owner = null;
                this.groupListeners[i].group = null;
            }
        }
        super.disposeNativeView();
    }
    notifyValidated(propertyName, result) {
        let property = this.getPropertyByName(propertyName);
        if (property.android) {
            let message = result ? property.successMessage : property.errorMessage;
            property.android.onValidationResult(property.valueCandidate, result, message);
        }
    }
    _setupGroups() {
        this._android._createGroupFunction = new DataFormCreateGroupClass(this);
        this._layoutManager.setCreateGroup(this._android._createGroupFunction);
        this._android.arrangeEditors();
    }
    _updateSource() {
        if (!this._android || !this.source) {
            return;
        }
        this._android.setReloadSuspended(true);
        let objJSON = JSON.stringify(this.source);
        let jsonObject = new org.json.JSONObject(objJSON);
        this._android.setEntity(jsonObject);
        this._syncPropertiesWithNativeProperties();
        this._updateNativeGroups();
        this._android.setReloadSuspended(false);
        this.reload();
    }
    _updateMetadata() {
        if (!this._android || !this.metadata) {
            return;
        }
        this._android.setReloadSuspended(true);
        let objJSON = JSON.stringify(this.metadata);
        let jsonObject = new org.json.JSONObject(objJSON);
        let metadata = new com.telerik.widget.dataform.engine.DataFormMetadata(jsonObject);
        this._android.setMetadata(metadata);
        this._android.setReloadSuspended(false);
        this.reload();
    }
    _syncPropertiesWithNativeProperties() {
        let nativeEntity = this._android.getEntity();
        let nativeProperties = nativeEntity.properties();
        // We probably need to use the for(let  ) loop here
        let length = nativeProperties.size();
        for (let i = 0; i < length; i++) {
            let nativeProperty = nativeProperties.get(i);
            let property = this.getPropertyByName(nativeProperty.name());
            if (property == null) {
                property = this._createPropertyFromNative(nativeProperty);
                if (!this.properties) {
                    this.properties = new Array();
                }
                this.properties.push(property);
            }
            else {
                property._linkPropertyWithNative(nativeProperty);
            }
            this._attachEntityPropertyPropertyChangeListener(property);
        }
    }
    updateNativePropertyEditorDisplayMode(editor, value) {
        let nativeValue;
        switch (value) {
            case AutoCompleteDisplayMode.Plain:
                nativeValue = com.telerik.widget.autocomplete.DisplayMode.PLAIN;
                break;
            case AutoCompleteDisplayMode.Tokens:
                nativeValue = com.telerik.widget.autocomplete.DisplayMode.TOKENS;
                break;
        }
        if (value && editor) {
            editor.setDisplayMode(nativeValue);
        }
        else {
            console.log('autoCompleteDisplayMode cannot be set to: ' + value);
        }
    }
    _updateNativeGroups() {
        if (!this.source) {
            return;
        }
        // go through all groups / entity properties
        if (this.groups) {
            for (let i = 0; i < this.groups.length; i++) {
                if (this.groups[i].properties) {
                    for (let j = 0; j < this.groups[i].properties.length; j++) {
                        let entityProperty = this.groups[i].properties[j];
                        if (entityProperty.android) {
                            entityProperty.android.setGroupName(this.groups[i].name);
                        }
                    }
                }
            }
        }
    }
    getNativeGroup(name) {
        let groupCount = this._layoutManager.editorGroups().size();
        for (let i = 0; i < groupCount; i++) {
            let group = this._layoutManager.editorGroups().get(i);
            if (group.name() === name) {
                return group;
            }
        }
        return null;
    }
    _updateEditorStyles() {
        this._android._editorCustomizations = new DataFormEditorCustomizations(this);
        this._android.setEditorCustomizations(this._android._editorCustomizations);
    }
    _updateGroupStyles() {
        this._android._editorGroupCustomizations = new DataFormEditorGroupCustomizations(this);
        this._layoutManager.setEditorGroupCustomizations(this._android._editorGroupCustomizations);
    }
    _createPropertyFromNative(nativeProperty) {
        let entityProperty = new EntityProperty();
        entityProperty.name = nativeProperty.name();
        entityProperty._linkPropertyWithNative(nativeProperty);
        return entityProperty;
    }
    _addValidationListener() {
        this._android._validationListener = new DataFormValidationListener(this);
        this._android.addValidationListener(this._android._validationListener);
    }
    _addCommitListener() {
        this._android._entityPropertyCommitListener = new DataFormEntityPropertyCommitListener(this);
        this._android.addCommitListener(this._android._entityPropertyCommitListener);
    }
    get editedObject() {
        let editedObject = this._android.getEditedObject();
        if (editedObject) {
            return editedObject.toString();
        }
        return null;
    }
    _onIsReadOnlyPropertyChanged(oldValue, newValue) {
        this._updateIsReadOnly();
    }
    _onCommitModePropertyChanged(oldValue, newValue) {
        this._updateCommitMode();
    }
    _onValidationModePropertyChanged(oldValue, newValue) {
        this._updateValidationMode();
    }
    _updateIsReadOnly() {
        if (!this._android) {
            return;
        }
        this._android.setEnabled(!this.isReadOnly);
    }
    _updateCommitMode() {
        if (!this._android) {
            return;
        }
        switch (this.commitMode) {
            case commonModule.DataFormCommitMode.Immediate:
                this._android.setCommitMode(com.telerik.widget.dataform.visualization.core.CommitMode.IMMEDIATE);
                break;
            case commonModule.DataFormCommitMode.Manual:
                this._android.setCommitMode(com.telerik.widget.dataform.visualization.core.CommitMode.MANUAL);
                break;
            case commonModule.DataFormCommitMode.OnLostFocus:
                this._android.setCommitMode(com.telerik.widget.dataform.visualization.core.CommitMode.ON_LOST_FOCUS);
                break;
        }
    }
    _updateValidationMode() {
        if (!this._android) {
            return;
        }
        switch (this.validationMode) {
            case commonModule.DataFormValidationMode.Immediate:
                this._android.setValidationMode(com.telerik.widget.dataform.visualization.core.ValidationMode.IMMEDIATE);
                break;
            case commonModule.DataFormValidationMode.Manual:
                this._android.setValidationMode(com.telerik.widget.dataform.visualization.core.ValidationMode.MANUAL);
                break;
            case commonModule.DataFormValidationMode.OnLostFocus:
                this._android.setValidationMode(com.telerik.widget.dataform.visualization.core.ValidationMode.ON_LOST_FOCUS);
                break;
        }
    }
    reload() {
        if (this._android) {
            this._android.reload();
            if (!this._android.isReloadSuspended()) {
                this._syncEditorsWithNativeEditors();
            }
        }
    }
    validateAll() {
        if (!this._android) {
            return null;
        }
        if (!this._android._validationInfoProcedure || !this._android._validationInfoProcedure.owner) {
            this._android._validationInfoProcedure = new DataFormValidationInfoProcedure(this);
        }
        const promiseResolve = function (resolve) {
            if (this && this._android._validationInfoProcedure) {
                this._android._validationInfoProcedure.validateResolve = resolve;
            }
        };
        const promise = new Promise(promiseResolve.bind(this));
        this._android.validateChanges(this._android._validationInfoProcedure);
        return promise;
    }
    validateAndCommitAll() {
        if (!this._android) {
            return null;
        }
        const promiseResolve = function (resolve) {
            this.validateAll().then(result => {
                if (result) {
                    this.android.commitForced();
                }
                resolve(result);
            });
        };
        return new Promise(promiseResolve.bind(this));
    }
    commitAll() {
        if (!this._android) {
            return;
        }
        this._android.commitForced();
    }
    hasValidationErrors() {
        if (this._android) {
            this._android.validateChanges();
            return this._android.hasValidationErrors();
        }
        return false;
    }
    _onSourcePropertyChanged(oldValue, newValue) {
        this._updateSource();
    }
    _onMetadataPropertyChanged(oldValue, newValue) {
        this._updateMetadata();
    }
    _updateNativeEditor(entityProperty) {
        let nativeEditor = this._android.getExistingEditorForProperty(entityProperty.name);
        if (nativeEditor == null) {
            return;
        }
        if (!entityProperty.editor) {
            entityProperty._createEditorFromNative(nativeEditor);
        }
        else {
            PropertyEditorHelper._linkEditorWithNative(entityProperty.editor, nativeEditor);
            if (entityProperty.autoCompleteDisplayMode) {
                this.updateNativePropertyEditorDisplayMode(nativeEditor, entityProperty.autoCompleteDisplayMode);
            }
        }
    }
    _syncEditor(entityProperty) {
        this._updateNativeEditor(entityProperty);
        if (!entityProperty.editor) {
            // If an EntityProperty is hidden,
            // the native android never creates an editor for it.
            return;
        }
        if (!entityProperty.parent && !entityProperty.editor.parent) {
            this._addView(entityProperty);
            entityProperty._addView(entityProperty.editor);
        }
        let ngKey = this._ngKey;
        if (ngKey) {
            // Add any newly created editors to the same scope as RadDataForm
            // in order to apply component-specific css in angular
            let ngValue = this[ngKey];
            entityProperty[ngKey] = ngValue;
            entityProperty.editor[ngKey] = ngValue;
            entityProperty.editor.label[ngKey] = ngValue;
            entityProperty.editor.editorCore[ngKey] = ngValue;
        }
    }
    _syncEditorsWithNativeEditors() {
        if (!this.source) {
            return;
        }
        if (this.groups) {
            for (let i = 0; i < this.groups.length; i++) {
                if (this.groups[i].properties) {
                    for (let j = 0; j < this.groups[i].properties.length; j++) {
                        let entityProperty = this.groups[i].properties[j];
                        this._syncEditor(entityProperty);
                    }
                }
            }
        }
        if (this.properties) {
            for (let i = 0; i < this.properties.length; i++) {
                let entityProperty = this.properties[i];
                this._syncEditor(entityProperty);
            }
        }
        this._onCssStateChange();
    }
    static _makeTypeface(fontName, style) {
        let fontStyle = android.graphics.Typeface.NORMAL;
        switch (style) {
            case commonModule.DataFormFontStyle.Bold:
                fontStyle = android.graphics.Typeface.BOLD;
                break;
            case commonModule.DataFormFontStyle.Italic:
                fontStyle = android.graphics.Typeface.ITALIC;
                break;
            case commonModule.DataFormFontStyle.BoldItalic:
                fontStyle = android.graphics.Typeface.BOLD_ITALIC;
                break;
        }
        return android.graphics.Typeface.create(fontName, fontStyle);
    }
}
export class EntityProperty extends commonModule.EntityProperty {
    constructor() {
        super();
        this._shouldSkipEditorUpdate = false;
    }
    get android() {
        return this._android;
    }
    get isValid() {
        if (this.android) {
            return this.android.isValid();
        }
        return undefined;
    }
    get value() {
        if (this.android) {
            return this.android.getValue();
        }
        return undefined;
    }
    get valueCandidate() {
        if (this.android) {
            return this.android.getValueCandidate();
        }
        return undefined;
    }
    _linkPropertyWithNative(value) {
        this._android = value;
        this._onNativeSet();
    }
    _createEditorFromNative(nativeEditor) {
        let type = PropertyEditor._getNativeEditorType(nativeEditor);
        this._shouldSkipEditorUpdate = true;
        let propertyEditor = new PropertyEditor();
        propertyEditor.type = type;
        PropertyEditorHelper._linkEditorWithNative(propertyEditor, nativeEditor);
        this.editor = propertyEditor;
        this._shouldSkipEditorUpdate = false;
    }
    onEditorChanged(oldValue, newValue) {
        if (oldValue) {
            oldValue.off(Observable.propertyChangeEvent);
        }
        if (newValue instanceof commonModule.PropertyEditor) {
            const propertyChangedHandler = function (propertyChangeData) {
                if (propertyChangeData.propertyName === 'type') {
                    this._onEditorTypeChanged(propertyChangeData);
                }
            };
            newValue.on(Observable.propertyChangeEvent, propertyChangedHandler, this);
            if (!this._shouldSkipEditorUpdate) {
                this.updateNativeEditor(newValue);
            }
        }
    }
    _onEditorTypeChanged(data) {
        let newEditor = new PropertyEditor();
        newEditor.type = this.editor.type;
        newEditor.propertyEditorStyle = this.editor.propertyEditorStyle;
        newEditor.params = this.editor.params;
        this.editor = newEditor;
    }
    _onNativeSet() {
        if (!this._android) {
            return;
        }
        this.updateNativeEditor(this.editor);
        this.updateNativeValidators(this.validators);
        this.updateNativeConverter(this.converter);
        this.updateNativeValuesProvider(this.valuesProviderArray);
        this.updateNativeDisplayName(this.displayName);
        this.updateNativeIndex(this.index);
        this.updateNativeColumnIndex(this.columnIndex);
        this.updateNativeHidden(this.hidden);
        this.updateNativeReadOnly(this.readOnly);
        this.updateNativeRequired(this.required);
        this.updateNativeHintText(this.hintText);
        this.updateNativeImageResource(this.imageResource);
    }
    updateNativeEditor(value) {
        if (!this._android || !value) {
            return;
        }
        if (value instanceof CustomPropertyEditor) {
            this._android.setEditorType(com.telerik.widget.dataform.visualization.editors.DataFormCustomEditor.class);
            return;
        }
        this._android.setEditorType(value.editorClass);
        this._android.setEditorParams(value.editorParams);
    }
    updateNativeValidators(value) {
        if (!this._android || !value) {
            return;
        }
        let validatorSet = new com.telerik.widget.dataform.engine.PropertyValidatorSet();
        for (let k = 0; k < value.length; k++) {
            let validatorBase = value[k];
            let aValidator = validatorBase.android;
            validatorSet.add(aValidator);
        }
        this._android.setValidator(validatorSet);
    }
    updateNativeValuesProvider(value) {
        if (!this._android || !value) {
            return;
        }
        let nativeSource = new java.util.ArrayList();
        for (let i = 0; i < value.length; i++) {
            let nativeValue = value[i];
            if (typeof nativeValue === 'string') {
                nativeValue = nativeValue.trim();
            }
            if (typeof nativeValue === 'number') {
                nativeValue = new java.lang.Integer(nativeValue);
            }
            nativeSource.add(nativeValue);
        }
        let nativeList = nativeSource.toArray();
        this._android.updateValues(nativeList);
        if (this.editor && this.editor.android) {
            this.editor.android.notifyEntityPropertyChanged();
        }
    }
    updateNativeImageResource(value) {
        if (!this._android || value === undefined) {
            return;
        }
        if (value != null) {
            let nativeValue = value;
            let appResources = Utils.android.getApplicationContext().getResources();
            let packageName = Utils.android.getApplication().getPackageName();
            if (appResources) {
                let identifier = appResources.getIdentifier(nativeValue, 'drawable', packageName);
                nativeValue = identifier;
            }
            this._android.setImageResource(nativeValue);
        }
        else {
            this._android.setImageResource(0);
        }
        if (this.editor && this.editor.android) {
            this.editor.android.notifyEntityPropertyChanged();
        }
    }
    updateNativeDisplayName(value) {
        if (!this._android || value == null) {
            return;
        }
        this._android.setHeader(value);
        if (this.editor && this.editor.android) {
            this.editor.android.notifyEntityPropertyChanged();
        }
    }
    updateNativeIndex(value) {
        if (!this._android || value == null) {
            return;
        }
        this._android.setPosition(value);
    }
    updateNativeConverter(value) {
        if (!this._android || value == null) {
            return;
        }
        this._android.setConverter(new com.telerik.widget.dataform.engine.PropertyConverter({
            convertTo(source) {
                let result = value.convertTo(source);
                return result;
            },
            convertFrom(source) {
                let result = value.convertFrom(source);
                return result;
            }
        }));
        if (this.editor && this.editor.android) {
            this.editor.android.loadPropertyValue();
        }
    }
    updateNativeColumnIndex(value) {
        if (!this._android || value == null) {
            return;
        }
        this._android.setColumnPosition(value);
    }
    updateNativeHidden(value) {
        if (!this._android || value == null) {
            return;
        }
        this._android.setSkip(value);
    }
    updateNativeReadOnly(value) {
        if (!this._android || value == null) {
            return;
        }
        if (this.editor) {
            PropertyEditorHelper.setReadOnly(this.editor, value);
            if (this.editor.android) {
                this.editor.android.setEnabled(!value);
            }
        }
    }
    updateNativeRequired(value) {
        if (!this._android || value == null) {
            return;
        }
        this._android.setRequired(value);
    }
    updateNativeHintText(value) {
        if (!this._android || !value) {
            return;
        }
        this._android.setHintText(value);
        if (this.editor && this.editor.android) {
            this.editor.android.notifyEntityPropertyChanged();
        }
    }
}
export class DataFormEditorLabel extends commonModule.DataFormEditorLabel {
    constructor(editor) {
        super();
        this._editor = editor;
        this._android = editor.android.getHeaderView();
    }
    get android() {
        return this._android;
    }
    createNativeView() {
        return this._android;
    }
    disposeNativeView() {
        this._editor = null;
        this._android = null;
    }
    [colorProperty.setNative](value) {
        const nsColor = value instanceof Color ? value.android : value;
        this._android.setTextColor(nsColor);
    }
    [fontSizeProperty.setNative](value) {
        if (typeof value === 'number') {
            this._android.setTextSize(value);
        }
        else {
            this._android.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
        }
    }
    [fontInternalProperty.setNative](value) {
        this._android.setTypeface(value instanceof Font ? value.getAndroidTypeface() : value);
    }
    [widthProperty.setNative](value) {
        PropertyEditorHelper._updateLabelWidth(this._editor, value);
    }
    [commonModule.PropertyEditor.positionProperty.setNative](value) {
        if (!value) {
            return;
        }
        if (value.toLowerCase() === 'left') {
            PropertyEditorHelper._updateLabelPosition(this._editor, commonModule.DataFormLabelPosition.Left);
        }
        else if (value.toLowerCase() === 'top') {
            PropertyEditorHelper._updateLabelPosition(this._editor, commonModule.DataFormLabelPosition.Top);
        }
    }
}
export class DataFormEditorCore extends commonModule.DataFormEditorCore {
    constructor(editor) {
        super();
        this._editor = editor;
        this._android = this._editor.android.getEditorView();
    }
    get android() {
        return this._android;
    }
    createNativeView() {
        return this._android;
    }
    disposeNativeView() {
        this._editor = null;
        this._android = null;
    }
    [colorProperty.setNative](value) {
        const nsColor = value instanceof Color ? value.android : value;
        this._editor.android.setEditorColor(nsColor);
    }
    [fontSizeProperty.setNative](value) {
        const size = typeof value === 'number' ? value : value.nativeSize;
        this._editor.android.setEditorTextSize(size);
    }
    [fontInternalProperty.setNative](value) {
        const editorTypeface = value instanceof Font ? value.getAndroidTypeface() : value;
        this._editor.android.setEditorTypeface(editorTypeface);
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Editors
export class PropertyEditor extends commonModule.PropertyEditor {
    constructor() {
        super(...arguments);
        this._readOnly = false;
    }
    get android() {
        return this._android;
    }
    set android(value) {
        if (this._android && this._android._focusChangeListener) {
            this._android._focusChangeListener.owner = null;
        }
        this._android = value;
        this.setNativeView(value.rootLayout());
        if (this._label) {
            this._removeView(this._label);
        }
        if (this._editorCore) {
            this._removeView(this._editorCore);
        }
        if (this._android) {
            this._android._focusChangeListener = new EditorFocusChangeListener(this);
            this._android.addFocusChangeListener(this._android._focusChangeListener);
            this._label = new DataFormEditorLabel(this);
            this._editorCore = new DataFormEditorCore(this);
            this._addView(this._label);
            this._addView(this._editorCore);
        }
        else {
            this._label = null;
            this._editorCore = null;
        }
    }
    get label() {
        return this._label;
    }
    get editorCore() {
        return this._editorCore;
    }
    get editorClass() {
        return this._editorClass;
    }
    get editorParams() {
        return this._editorParams;
    }
    [paddingLeftProperty.setNative](value) {
        this.android.getMainLayout().getLayoutParams().leftMargin = value;
    }
    [paddingTopProperty.setNative](value) {
        this.android.getMainLayout().getLayoutParams().topMargin = value;
    }
    [paddingRightProperty.setNative](value) {
        this.android.getMainLayout().getLayoutParams().rightMargin = value;
    }
    [paddingBottomProperty.setNative](value) {
        this.android.getMainLayout().getLayoutParams().bottomMargin = value;
    }
    createNativeView() {
        return this._android ? this._android.rootLayout() : super.createNativeView();
    }
    onPropertyEditorStyleChanged(oldValue, newValue) {
        PropertyEditorHelper.applyStyle(this);
    }
    onStylePropertyChanged(propertyName) {
        PropertyEditorHelper.applyStyleForProperty(this, propertyName);
    }
    onParamsChanged(oldValue, newValue) {
        PropertyEditorHelper.applyParams(this);
    }
    onParamsPropertyChanged(propertyName) {
        PropertyEditorHelper.applyParams(this);
    }
    onTypeChanged(oldValue, newValue) {
        this._updateEditorClass();
    }
    _updateEditorClass() {
        if (this.type == null) {
            return;
        }
        switch (this.type) {
            case commonModule.DataFormEditorType.Text:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormTextEditor.class;
                break;
            case commonModule.DataFormEditorType.MultilineText:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormMultilineTextEditor.class;
                break;
            case commonModule.DataFormEditorType.Email:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormEmailEditor.class;
                break;
            case commonModule.DataFormEditorType.Password:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormPasswordEditor.class;
                break;
            case commonModule.DataFormEditorType.Phone:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormPhoneEditor.class;
                break;
            case commonModule.DataFormEditorType.Decimal:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormDecimalEditor.class;
                break;
            case commonModule.DataFormEditorType.Number:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormIntegerEditor.class;
                break;
            case commonModule.DataFormEditorType.Switch:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormSwitchEditor.class;
                break;
            case commonModule.DataFormEditorType.Stepper:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormNumberPickerEditor.class;
                break;
            case commonModule.DataFormEditorType.Slider:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormSeekBarEditor.class;
                break;
            case commonModule.DataFormEditorType.SegmentedEditor:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormSegmentedEditor.class;
                break;
            case commonModule.DataFormEditorType.DatePicker:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormDateEditor.class;
                break;
            case commonModule.DataFormEditorType.TimePicker:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormTimeEditor.class;
                break;
            case commonModule.DataFormEditorType.Picker:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormSpinnerEditor.class;
                break;
            case commonModule.DataFormEditorType.List:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormListViewEditor.class;
                break;
            case commonModule.DataFormEditorType.AutoCompleteInline:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormRadAutoCompleteEditor.class;
                break;
            case commonModule.DataFormEditorType.Label:
                this._editorClass = com.telerik.widget.dataform.visualization.editors.DataFormLabelEditor.class;
                break;
            default:
                console.log('WARNING: Unsupported editor type: ' + this.type);
        }
    }
    static _getNativeEditorType(nativeEditor) {
        let nativeEditorClass = nativeEditor.getClass();
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormMultilineTextEditor.class) {
            return commonModule.DataFormEditorType.MultilineText;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormEmailEditor.class) {
            return commonModule.DataFormEditorType.Email;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormPasswordEditor.class) {
            return commonModule.DataFormEditorType.Password;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormPhoneEditor.class) {
            return commonModule.DataFormEditorType.Phone;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormDecimalEditor.class) {
            return commonModule.DataFormEditorType.Decimal;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormIntegerEditor.class) {
            return commonModule.DataFormEditorType.Number;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormSwitchEditor.class) {
            return commonModule.DataFormEditorType.Switch;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormNumberPickerEditor.class) {
            return commonModule.DataFormEditorType.Stepper;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormSeekBarEditor.class) {
            return commonModule.DataFormEditorType.Slider;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormSegmentedEditor.class) {
            return commonModule.DataFormEditorType.SegmentedEditor;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormDateEditor.class) {
            return commonModule.DataFormEditorType.DatePicker;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormTimeEditor.class) {
            return commonModule.DataFormEditorType.TimePicker;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormSpinnerEditor.class) {
            return commonModule.DataFormEditorType.Picker;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormListViewEditor.class) {
            return commonModule.DataFormEditorType.List;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormRadAutoCompleteEditor.class) {
            return commonModule.DataFormEditorType.AutoCompleteInline;
        }
        if (nativeEditorClass === com.telerik.widget.dataform.visualization.editors.DataFormLabelEditor.class) {
            return commonModule.DataFormEditorType.Label;
        }
        return commonModule.DataFormEditorType.Text;
    }
}
export class CustomPropertyEditor extends commonModule.CustomPropertyEditor {
    constructor() {
        super(...arguments);
        this._readOnly = false;
    }
    get android() {
        return this._android;
    }
    set android(value) {
        this._android = value;
        this.setNativeView(value.rootLayout());
        if (this._label) {
            this._removeView(this._label);
        }
        if (this._editorCore) {
            this._removeView(this._editorCore);
        }
        if (this._android) {
            this._label = new DataFormEditorLabel(this);
            this._editorCore = new DataFormEditorCore(this);
            this._addView(this._label);
            this._addView(this._editorCore);
        }
        else {
            this._label = null;
            this._editorCore = null;
        }
    }
    get label() {
        return this._label;
    }
    get editorCore() {
        return this._editorCore;
    }
    get editorClass() {
        return this._editorClass;
    }
    get editorParams() {
        return this._editorParams;
    }
    onPropertyEditorStyleChanged(oldValue, newValue) {
        PropertyEditorHelper.applyStyle(this);
    }
    onStylePropertyChanged(propertyName) {
        PropertyEditorHelper.applyStyleForProperty(this, propertyName);
    }
    onParamsChanged(oldValue, newValue) {
        PropertyEditorHelper.applyParams(this);
    }
    onParamsPropertyChanged(propertyName) {
        PropertyEditorHelper.applyParams(this);
    }
    onTypeChanged(oldValue, newValue) {
        console.log("WARNING: You can't change CustomPropertyEditor's type");
    }
    [paddingLeftProperty.setNative](value) {
        this.android.getMainLayout().getLayoutParams().leftMargin = value;
    }
    [paddingTopProperty.setNative](value) {
        this.android.getMainLayout().getLayoutParams().topMargin = value;
    }
    [paddingRightProperty.setNative](value) {
        this.android.getMainLayout().getLayoutParams().rightMargin = value;
    }
    [paddingBottomProperty.setNative](value) {
        this.android.getMainLayout().getLayoutParams().bottomMargin = value;
    }
    createView(context) {
        let args = {
            eventName: commonModule.CustomPropertyEditor.editorNeedsViewEvent,
            object: this,
            view: undefined,
            context: context,
            value: undefined
        };
        this.notify(args);
        return args.view;
    }
    applyValueToEditor(value, view) {
        let args = {
            eventName: commonModule.CustomPropertyEditor.editorHasToApplyValueEvent,
            object: this,
            view: view,
            context: view.getContext(),
            value: value
        };
        this.notify(args);
    }
    value(view) {
        let args = {
            eventName: commonModule.CustomPropertyEditor.editorNeedsValueEvent,
            object: this,
            view: view,
            context: view.getContext(),
            value: undefined
        };
        this.notify(args);
        return args.value;
    }
    notifyValueChanged() {
        if (this.android) {
            this.android.notifyEditorValueChanged();
        }
    }
}
export class PropertyEditorHelper {
    static _linkEditorWithNative(editor, value) {
        if (editor instanceof CustomPropertyEditor) {
            editor.android = value;
        }
        else {
            editor.android = value;
        }
        if (!editor.propertyEditorStyle) {
            editor.propertyEditorStyle = new commonModule.PropertyEditorStyle();
        }
        if (!editor.params) {
            editor.params = new commonModule.PropertyEditorParams();
        }
        PropertyEditorHelper._onNativeSet(editor);
    }
    static _onNativeSet(editor) {
        if (!editor.android) {
            return;
        }
        if (editor instanceof CustomPropertyEditor) {
            editor.android.setProvider(new com.telerik.widget.dataform.visualization.editors.DataFormCustomEditor.DataFormCustomEditorProvider({
                createView(context) {
                    return editor.createView(context);
                },
                applyValueToEditor(value, view) {
                    editor.applyValueToEditor(value, view);
                },
                getValue(view) {
                    return editor.value(view);
                }
            }));
        }
        else {
            editor.type = PropertyEditor._getNativeEditorType(editor.android);
        }
        PropertyEditorHelper.applyStyle(editor);
        PropertyEditorHelper.applyParams(editor);
        let editorEnabled = !PropertyEditorHelper.isReadOnly(editor);
        editor.android.setEnabled(editorEnabled);
        editor.android.notifyEntityPropertyChanged();
    }
    static _updateLabelTextColor(editor, labelTextColor) {
        if (!editor.android || labelTextColor === undefined) {
            return;
        }
        editor.android.getHeaderView().setTextColor(labelTextColor.android);
    }
    static _updateLabelFont(editor, labelFontName, labelFontStyle) {
        if (!editor._android || (labelFontName === undefined && labelFontStyle === undefined)) {
            return;
        }
        let editorTypeface = RadDataForm._makeTypeface(labelFontName, labelFontStyle);
        editor.android.getHeaderView().setTypeface(editorTypeface);
    }
    static _updateLabelTextSize(editor, labelTextSize) {
        if (!editor.android || labelTextSize === undefined) {
            return;
        }
        editor.android.getHeaderView().setTextSize(labelTextSize);
    }
    static _updateLabelHorizontalOffset(editor, labelHorizontalOffset) {
        if (!editor.android || labelHorizontalOffset === undefined) {
            return;
        }
        editor.android.getHeaderView().setTranslationX(labelHorizontalOffset);
    }
    static _updateLabelVerticalOffset(editor, labelVerticalOffset) {
        if (!editor.android || labelVerticalOffset === undefined) {
            return;
        }
        editor.android.getHeaderView().setTranslationY(labelVerticalOffset);
    }
    static _updateEditorHorizontalOffset(editor, editorHorizontalOffset) {
        if (!editor.android || editorHorizontalOffset === undefined) {
            return;
        }
        editor.android.getEditorView().setTranslationX(editorHorizontalOffset);
    }
    static _updateEditorVerticalOffset(editor, editorVerticalOffset) {
        if (!editor.android || editorVerticalOffset === undefined) {
            return;
        }
        editor.android.getEditorView().setTranslationY(editorVerticalOffset);
    }
    static _updateEditorFillColor(editor, editorFillColor) {
        if (!editor.android || editorFillColor === undefined) {
            return;
        }
        editor.android.rootLayout().setBackgroundColor(editorFillColor.android);
    }
    static _updateEditorStroke(editor, editorStrokeColor, editorStrokeWidth, editorFillColor) {
        if (!editor.android || (editorStrokeColor === undefined && editorStrokeWidth === undefined)) {
            return;
        }
        let drawable = new android.graphics.drawable.GradientDrawable();
        let strokeWidthDips = editorStrokeWidth ? editorStrokeWidth : 2;
        let strokeWidth = strokeWidthDips * Utils.layout.getDisplayDensity();
        let strokeColor = editorStrokeColor ? editorStrokeColor.android : android.graphics.Color.BLACK;
        let fillColor = editorFillColor ? editorFillColor.android : android.graphics.Color.TRANSPARENT;
        drawable.setStroke(strokeWidth, strokeColor);
        drawable.setColor(fillColor);
        editor.android.rootLayout().setBackgroundDrawable(drawable);
    }
    static _updateLabelHidden(editor, labelHidden) {
        if (!editor.android || labelHidden === undefined) {
            return;
        }
        let visibility = labelHidden ? android.view.View.GONE : android.view.View.VISIBLE;
        editor.android.getHeaderView().setVisibility(visibility);
    }
    static _updateLabelPosition(editor, labelPosition) {
        if (!editor.android || labelPosition === undefined) {
            return;
        }
        if (labelPosition === commonModule.DataFormLabelPosition.Left) {
            editor.android.setLabelPosition(com.telerik.widget.dataform.engine.LabelPosition.LEFT);
        }
        else if (labelPosition === commonModule.DataFormLabelPosition.Top) {
            editor.android.setLabelPosition(com.telerik.widget.dataform.engine.LabelPosition.TOP);
        }
    }
    static _updateLabelWidth(editor, labelWidth) {
        if (!editor.android || labelWidth === undefined) {
            return;
        }
        let nativeLabelWidth = labelWidth * Utils.layout.getDisplayDensity();
        editor.android.setLabelWidth(nativeLabelWidth);
    }
    static applyParams(editor) {
        let editorParams = editor.params;
        if (!editorParams) {
            return;
        }
        editor._editorParams = new java.util.HashMap();
        if (editorParams.minimum) {
            let min = new java.lang.Float(editorParams.minimum);
            editor._editorParams.put('minimum', min);
        }
        if (editorParams.maximum) {
            let max = new java.lang.Float(editorParams.maximum);
            editor._editorParams.put('maximum', max);
        }
        if (editorParams.step) {
            let step = new java.lang.Float(editorParams.step);
            editor._editorParams.put('step', step);
        }
        if (editor.android) {
            editor.android.applyParams(editor.editorParams);
        }
    }
    static applyStyle(editor) {
        if (!editor.propertyEditorStyle) {
            return;
        }
        PropertyEditorHelper._updateLabelTextColor(editor, editor.propertyEditorStyle.labelTextColor);
        PropertyEditorHelper._updateLabelFont(editor, editor.propertyEditorStyle.labelFontName, editor.propertyEditorStyle.labelFontStyle);
        PropertyEditorHelper._updateLabelTextSize(editor, editor.propertyEditorStyle.labelTextSize);
        PropertyEditorHelper._updateLabelHorizontalOffset(editor, editor.propertyEditorStyle.labelHorizontalOffset);
        PropertyEditorHelper._updateLabelVerticalOffset(editor, editor.propertyEditorStyle.labelVerticalOffset);
        PropertyEditorHelper._updateEditorHorizontalOffset(editor, editor.propertyEditorStyle.editorHorizontalOffset);
        PropertyEditorHelper._updateEditorVerticalOffset(editor, editor.propertyEditorStyle.editorVerticalOffset);
        PropertyEditorHelper._updateEditorFillColor(editor, editor.propertyEditorStyle.fillColor);
        PropertyEditorHelper._updateEditorStroke(editor, editor.propertyEditorStyle.strokeColor, editor.propertyEditorStyle.strokeWidth, editor.propertyEditorStyle.fillColor);
        PropertyEditorHelper._updateLabelHidden(editor, editor.propertyEditorStyle.labelHidden);
        PropertyEditorHelper._updateLabelPosition(editor, editor.propertyEditorStyle.labelPosition);
        PropertyEditorHelper._updateLabelWidth(editor, editor.propertyEditorStyle.labelWidth);
    }
    static applyStyleForProperty(editor, propertyName) {
        if (!editor.propertyEditorStyle) {
            return;
        }
        switch (propertyName) {
            case 'labelTextColor':
                PropertyEditorHelper._updateLabelTextColor(editor, editor.propertyEditorStyle.labelTextColor);
                break;
            case 'labelFontName':
            case 'labelFontStyle':
                PropertyEditorHelper._updateLabelFont(editor, editor.propertyEditorStyle.labelFontName, editor.propertyEditorStyle.labelFontStyle);
                break;
            case 'labelTextSize':
                PropertyEditorHelper._updateLabelTextSize(editor, editor.propertyEditorStyle.labelTextSize);
                break;
            case 'labelHorizontalOffset':
                PropertyEditorHelper._updateLabelHorizontalOffset(editor, editor.propertyEditorStyle.labelHorizontalOffset);
                break;
            case 'labelVerticalOffset':
                PropertyEditorHelper._updateLabelVerticalOffset(editor, editor.propertyEditorStyle.labelVerticalOffset);
                break;
            case 'editorHorizontalOffset':
                PropertyEditorHelper._updateEditorHorizontalOffset(editor, editor.propertyEditorStyle.editorHorizontalOffset);
                break;
            case 'editorVerticalOffset':
                PropertyEditorHelper._updateEditorVerticalOffset(editor, editor.propertyEditorStyle.editorVerticalOffset);
                break;
            case 'fillColor':
                PropertyEditorHelper._updateEditorFillColor(editor, editor.propertyEditorStyle.fillColor);
                PropertyEditorHelper._updateEditorStroke(editor, editor.propertyEditorStyle.strokeColor, editor.propertyEditorStyle.strokeWidth, editor.propertyEditorStyle.fillColor);
                break;
            case 'strokeColor':
            case 'strokeWidth':
                PropertyEditorHelper._updateEditorStroke(editor, editor.propertyEditorStyle.strokeColor, editor.propertyEditorStyle.strokeWidth, editor.propertyEditorStyle.fillColor);
                break;
            case 'labelHidden':
                PropertyEditorHelper._updateLabelHidden(editor, editor.propertyEditorStyle.labelHidden);
                break;
            case 'labelPosition':
                PropertyEditorHelper._updateLabelPosition(editor, editor.propertyEditorStyle.labelPosition);
                break;
            case 'labelWidth':
                PropertyEditorHelper._updateLabelWidth(editor, editor.propertyEditorStyle.labelWidth);
                break;
        }
    }
    static isReadOnly(editor) {
        if (editor instanceof CustomPropertyEditor) {
            return editor._readOnly;
        }
        return editor._readOnly;
    }
    static setReadOnly(editor, value) {
        if (editor instanceof CustomPropertyEditor) {
            editor._readOnly = value;
        }
        editor._readOnly = value;
    }
}
let ManualValidationProvider;
function initializeValidationProviders() {
    if (!ManualValidationProvider) {
        var ManualValidationProviderImpl = /** @class */ (function (_super) {
    __extends(ManualValidationProviderImpl, _super);
    function ManualValidationProviderImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    ManualValidationProviderImpl.prototype.validate = function (value, propertyName) {
        return this.owner.validate(value, propertyName);
    };
    ManualValidationProviderImpl = __decorate([
        Interfaces([com.telerik.widget.dataform.engine.PropertyValidatorManual.ValidationProvider])
    ], ManualValidationProviderImpl);
    return ManualValidationProviderImpl;
}(java.lang.Object));
        ManualValidationProvider = ManualValidationProviderImpl;
    }
}
export class PropertyValidator extends commonModule.PropertyValidator {
    constructor() {
        super();
        initializeValidationProviders();
        this._android = new com.telerik.widget.dataform.engine.PropertyValidatorManual();
        this._android._manualValidationProvider = new ManualValidationProvider(this);
        this._android.setValidationProvider(this._android._manualValidationProvider);
        if (this.errorMessage === undefined) {
            this.errorMessage = 'This is not valid.';
        }
    }
    get android() {
        return this._android;
    }
    disposeNativeView() {
        if (this._android._manualValidationProvider) {
            this._android._manualValidationProvider.owner = null;
        }
        super.disposeNativeView();
    }
    validate(value, propertyName) {
        return true;
    }
}
export class MinimumLengthValidator extends commonModule.MinimumLengthValidator {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.MinimumLengthValidator();
    }
    get android() {
        return this._android;
    }
    onLengthChanged(oldValue, newValue) {
        if (!isNaN(+newValue)) {
            this.android.setMinimumLength(newValue);
        }
    }
}
export class MaximumLengthValidator extends commonModule.MaximumLengthValidator {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.MaximumLengthValidator();
    }
    get android() {
        return this._android;
    }
    onLengthChanged(oldValue, newValue) {
        if (!isNaN(+newValue)) {
            this.android.setMaximumLength(newValue);
        }
    }
}
export class EmailValidator extends commonModule.EmailValidator {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.MailValidator();
    }
    get android() {
        return this._android;
    }
}
export class NonEmptyValidator extends commonModule.NonEmptyValidator {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.NonEmptyValidator();
    }
    get android() {
        return this._android;
    }
}
export class RangeValidator extends commonModule.RangeValidator {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.RangeValidator();
    }
    get android() {
        return this._android;
    }
    onMinimumChanged(oldValue, newValue) {
        if (!isNaN(+newValue)) {
            this._android.setMin(newValue);
        }
    }
    onMaximumChanged(oldValue, newValue) {
        if (!isNaN(+newValue)) {
            this._android.setMax(newValue);
        }
    }
}
export class PhoneValidator extends commonModule.PhoneValidator {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.PhoneValidator();
    }
    get android() {
        return this._android;
    }
}
export class RegExValidator extends commonModule.RegExValidator {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.RegExValidator();
    }
    get android() {
        return this._android;
    }
    onRegExChanged(oldValue, newValue) {
        this._android.setRegEx(newValue);
    }
}
export class IsTrueValidator extends commonModule.IsTrueValidator {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.IsTrueValidator();
    }
    get android() {
        return this._android;
    }
}
export class StringToDateConverter extends commonModule.StringToDateConverter {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.StringToDateConverter();
    }
    get android() {
        return this._android;
    }
}
export class StringToTimeConverter extends commonModule.StringToTimeConverter {
    constructor() {
        super();
        this._android = new com.telerik.widget.dataform.engine.StringToTimeConverter();
    }
    get android() {
        return this._android;
    }
}
//# sourceMappingURL=index.android.js.map