//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization;

namespace WWWings_GO
{
    [DataContract(IsReference = true)]
    [KnownType(typeof(Passagier))]
    [KnownType(typeof(Pilot))]
    public partial class Flug: IObjectWithChangeTracker, INotifyPropertyChanged
    {
        #region Primitive Properties
    
        [DataMember]
        public int ID
        {
            get { return _iD; }
            set
            {
                if (_iD != value)
                {
                    if (ChangeTracker.ChangeTrackingEnabled && ChangeTracker.State != ObjectState.Added)
                    {
                        throw new InvalidOperationException("The property 'ID' is part of the object's key and cannot be changed. Changes to key properties can only be made when the object is not being tracked or is in the Added state.");
                    }
                    _iD = value;
                    OnPropertyChanged("ID");
                }
            }
        }
        private int _iD;
    
        [DataMember]
        public string Abflugort
        {
            get { return _abflugort; }
            set
            {
                if (_abflugort != value)
                {
                    _abflugort = value;
                    OnPropertyChanged("Abflugort");
                }
            }
        }
        private string _abflugort;
    
        [DataMember]
        public string Zielort
        {
            get { return _zielort; }
            set
            {
                if (_zielort != value)
                {
                    _zielort = value;
                    OnPropertyChanged("Zielort");
                }
            }
        }
        private string _zielort;
    
        [DataMember]
        public System.DateTime Datum
        {
            get { return _datum; }
            set
            {
                if (_datum != value)
                {
                    _datum = value;
                    OnPropertyChanged("Datum");
                }
            }
        }
        private System.DateTime _datum;
    
        [DataMember]
        public short Plaetze
        {
            get { return _plaetze; }
            set
            {
                if (_plaetze != value)
                {
                    _plaetze = value;
                    OnPropertyChanged("Plaetze");
                }
            }
        }
        private short _plaetze;
    
        [DataMember]
        public short FreiePlaetze
        {
            get { return _freiePlaetze; }
            set
            {
                if (_freiePlaetze != value)
                {
                    _freiePlaetze = value;
                    OnPropertyChanged("FreiePlaetze");
                }
            }
        }
        private short _freiePlaetze;
    
        [DataMember]
        public int PilotId
        {
            get { return _pilotId; }
            set
            {
                if (_pilotId != value)
                {
                    ChangeTracker.RecordOriginalValue("PilotId", _pilotId);
                    if (!IsDeserializing)
                    {
                        if (Pilot != null && Pilot.ID != value)
                        {
                            Pilot = null;
                        }
                    }
                    _pilotId = value;
                    OnPropertyChanged("PilotId");
                }
            }
        }
        private int _pilotId;

        #endregion
        #region Navigation Properties
    
        [DataMember]
        public TrackableCollection<Passagier> Passagier
        {
            get
            {
                if (_passagier == null)
                {
                    _passagier = new TrackableCollection<Passagier>();
                    _passagier.CollectionChanged += FixupPassagier;
                }
                return _passagier;
            }
            set
            {
                if (!ReferenceEquals(_passagier, value))
                {
                    if (ChangeTracker.ChangeTrackingEnabled)
                    {
                        throw new InvalidOperationException("Cannot set the FixupChangeTrackingCollection when ChangeTracking is enabled");
                    }
                    if (_passagier != null)
                    {
                        _passagier.CollectionChanged -= FixupPassagier;
                    }
                    _passagier = value;
                    if (_passagier != null)
                    {
                        _passagier.CollectionChanged += FixupPassagier;
                    }
                    OnNavigationPropertyChanged("Passagier");
                }
            }
        }
        private TrackableCollection<Passagier> _passagier;
    
        [DataMember]
        public Pilot Pilot
        {
            get { return _pilot; }
            set
            {
                if (!ReferenceEquals(_pilot, value))
                {
                    var previousValue = _pilot;
                    _pilot = value;
                    FixupPilot(previousValue);
                    OnNavigationPropertyChanged("Pilot");
                }
            }
        }
        private Pilot _pilot;

        #endregion
        #region ChangeTracking
    
        protected virtual void OnPropertyChanged(String propertyName)
        {
            if (ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
            {
                ChangeTracker.State = ObjectState.Modified;
            }
            if (_propertyChanged != null)
            {
                _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        protected virtual void OnNavigationPropertyChanged(String propertyName)
        {
            if (_propertyChanged != null)
            {
                _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged{ add { _propertyChanged += value; } remove { _propertyChanged -= value; } }
        private event PropertyChangedEventHandler _propertyChanged;
        private ObjectChangeTracker _changeTracker;
    
        [DataMember]
        public ObjectChangeTracker ChangeTracker
        {
            get
            {
                if (_changeTracker == null)
                {
                    _changeTracker = new ObjectChangeTracker();
                    _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
                }
                return _changeTracker;
            }
            set
            {
                if(_changeTracker != null)
                {
                    _changeTracker.ObjectStateChanging -= HandleObjectStateChanging;
                }
                _changeTracker = value;
                if(_changeTracker != null)
                {
                    _changeTracker.ObjectStateChanging += HandleObjectStateChanging;
                }
            }
        }
    
        private void HandleObjectStateChanging(object sender, ObjectStateChangingEventArgs e)
        {
            if (e.NewState == ObjectState.Deleted)
            {
                ClearNavigationProperties();
            }
        }
    
        protected bool IsDeserializing { get; private set; }
    
        [OnDeserializing]
        public void OnDeserializingMethod(StreamingContext context)
        {
            IsDeserializing = true;
        }
    
        [OnDeserialized]
        public void OnDeserializedMethod(StreamingContext context)
        {
            IsDeserializing = false;
            ChangeTracker.ChangeTrackingEnabled = true;
        }
    
        protected virtual void ClearNavigationProperties()
        {
            Passagier.Clear();
            Pilot = null;
        }

        #endregion
        #region Association Fixup
    
        private void FixupPilot(Pilot previousValue)
        {
            if (IsDeserializing)
            {
                return;
            }
    
            if (previousValue != null && previousValue.Flug.Contains(this))
            {
                previousValue.Flug.Remove(this);
            }
    
            if (Pilot != null)
            {
                if (!Pilot.Flug.Contains(this))
                {
                    Pilot.Flug.Add(this);
                }
    
                PilotId = Pilot.ID;
            }
            if (ChangeTracker.ChangeTrackingEnabled)
            {
                if (ChangeTracker.OriginalValues.ContainsKey("Pilot")
                    && (ChangeTracker.OriginalValues["Pilot"] == Pilot))
                {
                    ChangeTracker.OriginalValues.Remove("Pilot");
                }
                else
                {
                    ChangeTracker.RecordOriginalValue("Pilot", previousValue);
                }
                if (Pilot != null && !Pilot.ChangeTracker.ChangeTrackingEnabled)
                {
                    Pilot.StartTracking();
                }
            }
        }
    
        private void FixupPassagier(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (IsDeserializing)
            {
                return;
            }
    
            if (e.NewItems != null)
            {
                foreach (Passagier item in e.NewItems)
                {
                    if (!item.Flug.Contains(this))
                    {
                        item.Flug.Add(this);
                    }
                    if (ChangeTracker.ChangeTrackingEnabled)
                    {
                        if (!item.ChangeTracker.ChangeTrackingEnabled)
                        {
                            item.StartTracking();
                        }
                        ChangeTracker.RecordAdditionToCollectionProperties("Passagier", item);
                    }
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Passagier item in e.OldItems)
                {
                    if (item.Flug.Contains(this))
                    {
                        item.Flug.Remove(this);
                    }
                    if (ChangeTracker.ChangeTrackingEnabled)
                    {
                        ChangeTracker.RecordRemovalFromCollectionProperties("Passagier", item);
                    }
                }
            }
        }

        #endregion
    }
}
