using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;


namespace WPDatabaseDeleteCascade
{
    [Table(Name="Persons")]
    public class Person : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int id;
        private string name;
        private string firstName;
        private int age;
        private string city;
        private EntityRef<Order> order;

        [Column]
        private int _orderID;

        [Association(Storage = "order", ThisKey = "_orderID", OtherKey = "ID", DeleteRule = "Cascade")]
        public Order Order
        {
            get
            {
                return order.Entity;
            }
            set
            {
                if (order.Entity != value && value != null)
                {
                    NotifyPropertyChanging("Order");
                    order.Entity = value;
                    order.Entity.Person = this;
                    NotifyPropertyChanged("Order");

                    if (value != null)
                    {
                        _orderID = value.ID;
                    }
                }
            }
        }

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ID
        {
            get
            {
                return id;
            }
            private set
            {
                if (id != value)
                {
                    NotifyPropertyChanging("ID");
                    id = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

        [Column]
        public string LastName
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    NotifyPropertyChanging("LastName");
                    name = value;
                    NotifyPropertyChanged("LastName");
                }
            }
        }

        [Column]
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                if (firstName != value)
                {
                    NotifyPropertyChanging("FirstName");
                    firstName = value;
                    NotifyPropertyChanged("FirstName");
                }
            }
        }

        [Column]
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (age != value)
                {
                    NotifyPropertyChanging("Age");
                    age = value;
                    NotifyPropertyChanged("Age");
                }
            }
        }

        [Column]
        public string City
        {
            get
            {
                return city;
            }
            set
            {
                if (city != value)
                {
                    NotifyPropertyChanging("City");
                    city = value;
                    NotifyPropertyChanged("City");
                }
            }
        }


        // Benötigt für höhere Geschwindigkeit
        [Column(IsVersion = true)]
        private Binary version;


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
    }

    [Table(Name="Order")]
    public class Order : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int id;
        private string name;
        private EntityRef<Person> _person;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ID
        {
            get
            {
                return id;
            }
            private set
            {
                if (id != value)
                {
                    NotifyPropertyChanging("ID");
                    id = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

        [Column]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    NotifyPropertyChanging("LastName");
                    name = value;
                    NotifyPropertyChanged("LastName");
                }
            }
        }

        [Column]
        private int _personID;
        

        [Association(Storage="_person", IsForeignKey=true, ThisKey="_personID", OtherKey="ID")]
        public Person Person
        {
            get
            {
                return _person.Entity;
            }
            set
            {
                if (value != _person.Entity && value != null)
                {
                    NotifyPropertyChanging("Person");
                    _person.Entity = value;
                    _person.Entity.Order = this;
                    _personID = value.ID;
                    NotifyPropertyChanged("Person");                                        
                }
            }
        }

         //Benötigt für höhere Geschwindigkeit

        [Column(IsVersion = true)]
        private Binary version;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
    }
}
