using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using OrderEntryDataAccess;
using OrderEntryEngine;
using OrderEntrySystem.Reflection;

namespace OrderEntrySystem
{
    public class CustomerViewModel : EntityViewModel<Customer>, IDataErrorInfo
    {
        /// <summary>
        /// Initializes a new instance of the CustomerViewModel class.
        /// </summary>
        /// <param name="customer">The customer to be shown.</param>
        public CustomerViewModel(Customer customer)
            : base("New customer", customer)
        {
        }

        public string Error
        {
            get
            {
                return this.Entity.Error;
            }
        }

        [EntityControl(ControlType.TextBox, "First Name", 1), EntityColumn("First Name", 1)]
        public string FirstName
        {
            get
            {
                return this.Entity.FirstName;
            }

            set
            {
                this.Entity.FirstName = value;
                this.OnPropertyChanged("FirstName");
            }
        }

        [EntityControl(ControlType.TextBox, "Last Name", 2), EntityColumn("Last Name", 2)]
        public string LastName
        {
            get
            {
                return this.Entity.LastName;
            }

            set
            {
                this.Entity.LastName = value;
                this.OnPropertyChanged("LastName");
            }
        }

        [EntityControl(ControlType.TextBox, "Phone", 3), EntityColumn("Phone", 3)]
        public string Phone
        {
            get
            {
                return this.Entity.Phone;
            }

            set
            {
                this.Entity.Phone = value;
                this.OnPropertyChanged("Phone");
            }
        }

        [EntityControl(ControlType.TextBox, "Email", 4), EntityColumn("Email", 4)]
        public string Email
        {
            get
            {
                return this.Entity.Email;
            }

            set
            {
                this.Entity.Email = value;
                this.OnPropertyChanged("Email");
            }
        }

        public ObservableCollection<OrderViewModel> FilteredOrders
        {
            get
            {
                var orders =
                    (from o in this.Entity.Orders
                    select new OrderViewModel(o)).ToList();

                return new ObservableCollection<OrderViewModel>(orders);
            }
        }

        [EntityControl(ControlType.TextBox, "Address", 5), EntityColumn("Address", 5)]
        public string Address
        {
            get
            {
                return this.Entity.Address;
            }

            set
            {
                this.Entity.Address = value;
                this.OnPropertyChanged("Address");
            }
        }

        [EntityControl(ControlType.TextBox, "City", 6), EntityColumn("City", 6)]
        public string City
        {
            get
            {
                return this.Entity.City;
            }

            set
            {
                this.Entity.City = value;
                this.OnPropertyChanged("City");
            }
        }

        [EntityControl(ControlType.TextBox, "State", 7), EntityColumn("State", 7)]
        public string State
        {
            get
            {
                return this.Entity.State;
            }

            set
            {
                this.Entity.State = value;
                this.OnPropertyChanged("State");
            }
        }

        public string this[string propertyName]
        {
            get
            {
                return this.Entity[propertyName];
            }
        }
    }
}