/**
 * This is a generated class and is not intended for modification.  To customize behavior
 * of this value object you may modify the generated sub-class of this class - EmailConfirmation.as.
 */

package com.sharewithagent.services.valueObject
{
import com.adobe.fiber.services.IFiberManagingService;
import com.adobe.fiber.util.FiberUtils;
import com.adobe.fiber.valueobjects.IValueObject;
import com.sharewithagent.services.valueObject.EmailValidationErrorType;
import flash.events.Event;
import flash.events.EventDispatcher;
import mx.binding.utils.ChangeWatcher;
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
import mx.events.PropertyChangeEvent;
import mx.validators.ValidationResult;

import flash.net.registerClassAlias;
import flash.net.getClassByAlias;
import com.adobe.fiber.core.model_internal;
import com.adobe.fiber.valueobjects.IPropertyIterator;
import com.adobe.fiber.valueobjects.AvailablePropertyIterator;

use namespace model_internal;

[ExcludeClass]public class _Super_EmailConfirmation extends flash.events.EventDispatcher implements com.adobe.fiber.valueobjects.IValueObject
{
    model_internal static function initRemoteClassAliasSingle(cz:Class) : void
    {
    }

    model_internal static function initRemoteClassAliasAllRelated() : void
    {
        com.sharewithagent.services.valueObject.EmailValidationErrorType.initRemoteClassAliasSingleChild();
    }

    model_internal var _dminternal_model : _EmailConfirmationEntityMetadata;

    /**
     * properties
     */
    private var _internal_emailUniqueId : String;
    private var _internal_emailValidationError : ArrayCollection;
    model_internal var _internal_emailValidationError_leaf:com.sharewithagent.services.valueObject.EmailValidationErrorType;

    private static var emptyArray:Array = new Array();


    /**
     * derived property cache initialization
     */
    model_internal var _cacheInitialized_isValid:Boolean = false;

    model_internal var _changeWatcherArray:Array = new Array();

    public function _Super_EmailConfirmation()
    {
        _model = new _EmailConfirmationEntityMetadata(this);

        // Bind to own data properties for cache invalidation triggering
        model_internal::_changeWatcherArray.push(mx.binding.utils.ChangeWatcher.watch(this, "emailUniqueId", model_internal::setterListenerEmailUniqueId));
        model_internal::_changeWatcherArray.push(mx.binding.utils.ChangeWatcher.watch(this, "emailValidationError", model_internal::setterListenerEmailValidationError));

    }

    /**
     * data property getters
     */

    [Bindable("propertyChange")]
    public function get emailUniqueId() : String
    {
        return _internal_emailUniqueId;
    }

    [Bindable("propertyChange")]
    public function get emailValidationError() : ArrayCollection
    {
        return _internal_emailValidationError;
    }

    /**
     * data property setters
     */

    public function set emailUniqueId(value:String) : void
    {
        var oldValue:String = _internal_emailUniqueId;
        if (oldValue !== value)
        {
            _internal_emailUniqueId = value;
            this.dispatchEvent(mx.events.PropertyChangeEvent.createUpdateEvent(this, "emailUniqueId", oldValue, _internal_emailUniqueId));
        }
    }

    public function set emailValidationError(value:*) : void
    {
        var oldValue:ArrayCollection = _internal_emailValidationError;
        if (oldValue !== value)
        {
            if (value is ArrayCollection)
            {
                _internal_emailValidationError = value;
            }
            else if (value is Array)
            {
                _internal_emailValidationError = new ArrayCollection(value);
            }
            else
            {
                throw new Error("value of emailValidationError must be a collection");
            }
            this.dispatchEvent(mx.events.PropertyChangeEvent.createUpdateEvent(this, "emailValidationError", oldValue, _internal_emailValidationError));
        }
    }

    /**
     * Data property setter listeners
     *
     * Each data property whose value affects other properties or the validity of the entity
     * needs to invalidate all previously calculated artifacts. These include:
     *  - any derived properties or constraints that reference the given data property.
     *  - any availability guards (variant expressions) that reference the given data property.
     *  - any style validations, message tokens or guards that reference the given data property.
     *  - the validity of the property (and the containing entity) if the given data property has a length restriction.
     *  - the validity of the property (and the containing entity) if the given data property is required.
     */

    model_internal function setterListenerEmailUniqueId(value:flash.events.Event):void
    {
        _model.invalidateDependentOnEmailUniqueId();
    }

    model_internal function setterListenerEmailValidationError(value:flash.events.Event):void
    {
        if (value is mx.events.PropertyChangeEvent)
        {
            if (mx.events.PropertyChangeEvent(value).newValue)
            {
                mx.events.PropertyChangeEvent(value).newValue.addEventListener(mx.events.CollectionEvent.COLLECTION_CHANGE, model_internal::setterListenerEmailValidationError);
            }
        }
        _model.invalidateDependentOnEmailValidationError();
    }


    /**
     * valid related derived properties
     */
    model_internal var _isValid : Boolean;
    model_internal var _invalidConstraints:Array = new Array();
    model_internal var _validationFailureMessages:Array = new Array();

    /**
     * derived property calculators
     */

    /**
     * isValid calculator
     */
    model_internal function calculateIsValid():Boolean
    {
        var violatedConsts:Array = new Array();
        var validationFailureMessages:Array = new Array();

        var propertyValidity:Boolean = true;
        if (!_model.emailUniqueIdIsValid)
        {
            propertyValidity = false;
            com.adobe.fiber.util.FiberUtils.arrayAdd(validationFailureMessages, _model.model_internal::_emailUniqueIdValidationFailureMessages);
        }
        if (!_model.emailValidationErrorIsValid)
        {
            propertyValidity = false;
            com.adobe.fiber.util.FiberUtils.arrayAdd(validationFailureMessages, _model.model_internal::_emailValidationErrorValidationFailureMessages);
        }

        model_internal::_cacheInitialized_isValid = true;
        model_internal::invalidConstraints_der = violatedConsts;
        model_internal::validationFailureMessages_der = validationFailureMessages;
        return violatedConsts.length == 0 && propertyValidity;
    }

    /**
     * derived property setters
     */

    model_internal function set isValid_der(value:Boolean) : void
    {
        var oldValue:Boolean = model_internal::_isValid;
        if (oldValue !== value)
        {
            model_internal::_isValid = value;
            _model.model_internal::fireChangeEvent("isValid", oldValue, model_internal::_isValid);
        }
    }

    /**
     * derived property getters
     */

    [Transient]
Bindable("propertyChange")]
    public function get _model() : _EmailConfirmationEntityMetadata
    {
        return model_internal::_dminternal_model;
    }

    public function set _model(value : _EmailConfirmationEntityMetadata) : void
    {
        var oldValue : _EmailConfirmationEntityMetadata = model_internal::_dminternal_model;
        if (oldValue !== value)
        {
            model_internal::_dminternal_model = value;
            this.dispatchEvent(mx.events.PropertyChangeEvent.createUpdateEvent(this, "_model", oldValue, model_internal::_dminternal_model));
        }
    }

    /**
     * methods
     */


    /**
     *  services
     */
    private var _managingService:com.adobe.fiber.services.IFiberManagingService;

    public function set managingService(managingService:com.adobe.fiber.services.IFiberManagingService):void
    {
        _managingService = managingService;
    }

    model_internal function set invalidConstraints_der(value:Array) : void
    {
        var oldValue:Array = model_internal::_invalidConstraints;
        // avoid firing the event when old and new value are different empty arrays
        if (oldValue !== value && (oldValue.length > 0 || value.length > 0))
        {
            model_internal::_invalidConstraints = value;
            _model.model_internal::fireChangeEvent("invalidConstraints", oldValue, model_internal::_invalidConstraints);
        }
    }

    model_internal function set validationFailureMessages_der(value:Array) : void
    {
        var oldValue:Array = model_internal::_validationFailureMessages;
        // avoid firing the event when old and new value are different empty arrays
        if (oldValue !== value && (oldValue.length > 0 || value.length > 0))
        {
            model_internal::_validationFailureMessages = value;
            _model.model_internal::fireChangeEvent("validationFailureMessages", oldValue, model_internal::_validationFailureMessages);
        }
    }

    model_internal var _doValidationCacheOfEmailUniqueId : Array = null;
    model_internal var _doValidationLastValOfEmailUniqueId : String;

    model_internal function _doValidationForEmailUniqueId(valueIn:Object):Array
    {
        var value : String = valueIn as String;

        if (model_internal::_doValidationCacheOfEmailUniqueId != null && model_internal::_doValidationLastValOfEmailUniqueId == value)
           return model_internal::_doValidationCacheOfEmailUniqueId ;

        _model.model_internal::_emailUniqueIdIsValidCacheInitialized = true;
        var validationFailures:Array = new Array();
        var errorMessage:String;
        var failure:Boolean;

        var valRes:ValidationResult;
        if (_model.isEmailUniqueIdAvailable && _internal_emailUniqueId == null)
        {
            validationFailures.push(new ValidationResult(true, "", "", "emailUniqueId is required"));
        }

        model_internal::_doValidationCacheOfEmailUniqueId = validationFailures;
        model_internal::_doValidationLastValOfEmailUniqueId = value;

        return validationFailures;
    }
    
    model_internal var _doValidationCacheOfEmailValidationError : Array = null;
    model_internal var _doValidationLastValOfEmailValidationError : ArrayCollection;

    model_internal function _doValidationForEmailValidationError(valueIn:Object):Array
    {
        var value : ArrayCollection = valueIn as ArrayCollection;

        if (model_internal::_doValidationCacheOfEmailValidationError != null && model_internal::_doValidationLastValOfEmailValidationError == value)
           return model_internal::_doValidationCacheOfEmailValidationError ;

        _model.model_internal::_emailValidationErrorIsValidCacheInitialized = true;
        var validationFailures:Array = new Array();
        var errorMessage:String;
        var failure:Boolean;

        var valRes:ValidationResult;
        if (_model.isEmailValidationErrorAvailable && _internal_emailValidationError == null)
        {
            validationFailures.push(new ValidationResult(true, "", "", "emailValidationError is required"));
        }

        model_internal::_doValidationCacheOfEmailValidationError = validationFailures;
        model_internal::_doValidationLastValOfEmailValidationError = value;

        return validationFailures;
    }
    

}

}