/**
 * 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 - AgentLocatorResponse.as.
 */

package valueObjects
{
import com.adobe.fiber.services.IFiberManagingService;
import com.adobe.fiber.util.FiberUtils;
import com.adobe.fiber.valueobjects.IValueObject;
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 valueObjects.AgentContact;
import valueObjects.AgtLocFault;

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_AgentLocatorResponse extends flash.events.EventDispatcher implements com.adobe.fiber.valueobjects.IValueObject
{
    model_internal static function initRemoteClassAliasSingle(cz:Class) : void
    {
    }

    model_internal static function initRemoteClassAliasAllRelated() : void
    {
        valueObjects.AgentContact.initRemoteClassAliasSingleChild();
        valueObjects.AgtLocFault.initRemoteClassAliasSingleChild();
    }

    model_internal var _dminternal_model : _AgentLocatorResponseEntityMetadata;

    /**
     * properties
     */
    private var _internal_agents : ArrayCollection;
    model_internal var _internal_agents_leaf:valueObjects.AgentContact;
    private var _internal_agtLocFaultType : valueObjects.AgtLocFault;
    private var _internal_schemaVersion : Number;

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

    // Change this value according to your application's floating-point precision
    private static var epsilon:Number = 0.0001;

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

    model_internal var _changeWatcherArray:Array = new Array();

    public function _Super_AgentLocatorResponse()
    {
        _model = new _AgentLocatorResponseEntityMetadata(this);

        // Bind to own data properties for cache invalidation triggering
        model_internal::_changeWatcherArray.push(mx.binding.utils.ChangeWatcher.watch(this, "agents", model_internal::setterListenerAgents));
        model_internal::_changeWatcherArray.push(mx.binding.utils.ChangeWatcher.watch(this, "agtLocFaultType", model_internal::setterListenerAgtLocFaultType));

    }

    /**
     * data property getters
     */

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

    [Bindable("propertyChange")]
    public function get agtLocFaultType() : valueObjects.AgtLocFault
    {
        return _internal_agtLocFaultType;
    }

    [Bindable("propertyChange")]
    public function get schemaVersion() : Number
    {
        return _internal_schemaVersion;
    }

    /**
     * data property setters
     */

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

    public function set agtLocFaultType(value:valueObjects.AgtLocFault) : void
    {
        var oldValue:valueObjects.AgtLocFault = _internal_agtLocFaultType;
        if (oldValue !== value)
        {
            _internal_agtLocFaultType = value;
            this.dispatchEvent(mx.events.PropertyChangeEvent.createUpdateEvent(this, "agtLocFaultType", oldValue, _internal_agtLocFaultType));
        }
    }

    public function set schemaVersion(value:Number) : void
    {
        var oldValue:Number = _internal_schemaVersion;
        if (isNaN(_internal_schemaVersion) == true || Math.abs(oldValue - value) > epsilon)
        {
            _internal_schemaVersion = value;
            this.dispatchEvent(mx.events.PropertyChangeEvent.createUpdateEvent(this, "schemaVersion", oldValue, _internal_schemaVersion));
        }
    }

    /**
     * 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 setterListenerAgents(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::setterListenerAgents);
            }
        }
        _model.invalidateDependentOnAgents();
    }

    model_internal function setterListenerAgtLocFaultType(value:flash.events.Event):void
    {
        _model.invalidateDependentOnAgtLocFaultType();
    }


    /**
     * 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.agentsIsValid)
        {
            propertyValidity = false;
            com.adobe.fiber.util.FiberUtils.arrayAdd(validationFailureMessages, _model.model_internal::_agentsValidationFailureMessages);
        }
        if (!_model.agtLocFaultTypeIsValid)
        {
            propertyValidity = false;
            com.adobe.fiber.util.FiberUtils.arrayAdd(validationFailureMessages, _model.model_internal::_agtLocFaultTypeValidationFailureMessages);
        }

        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() : _AgentLocatorResponseEntityMetadata
    {
        return model_internal::_dminternal_model;
    }

    public function set _model(value : _AgentLocatorResponseEntityMetadata) : void
    {
        var oldValue : _AgentLocatorResponseEntityMetadata = 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 _doValidationCacheOfAgents : Array = null;
    model_internal var _doValidationLastValOfAgents : ArrayCollection;

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

        if (model_internal::_doValidationCacheOfAgents != null && model_internal::_doValidationLastValOfAgents == value)
           return model_internal::_doValidationCacheOfAgents ;

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

        var valRes:ValidationResult;
        if (_model.isAgentsAvailable && _internal_agents == null)
        {
            validationFailures.push(new ValidationResult(true, "", "", "agents is required"));
        }

        model_internal::_doValidationCacheOfAgents = validationFailures;
        model_internal::_doValidationLastValOfAgents = value;

        return validationFailures;
    }
    
    model_internal var _doValidationCacheOfAgtLocFaultType : Array = null;
    model_internal var _doValidationLastValOfAgtLocFaultType : valueObjects.AgtLocFault;

    model_internal function _doValidationForAgtLocFaultType(valueIn:Object):Array
    {
        var value : valueObjects.AgtLocFault = valueIn as valueObjects.AgtLocFault;

        if (model_internal::_doValidationCacheOfAgtLocFaultType != null && model_internal::_doValidationLastValOfAgtLocFaultType == value)
           return model_internal::_doValidationCacheOfAgtLocFaultType ;

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

        var valRes:ValidationResult;
        if (_model.isAgtLocFaultTypeAvailable && _internal_agtLocFaultType == null)
        {
            validationFailures.push(new ValidationResult(true, "", "", "agtLocFaultType is required"));
        }

        model_internal::_doValidationCacheOfAgtLocFaultType = validationFailures;
        model_internal::_doValidationLastValOfAgtLocFaultType = value;

        return validationFailures;
    }
    

}

}