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

namespace OrderEntrySystem
{
    public class ComputerViewModel : EntityViewModel<Computer>, IDataErrorInfo
    {
        public ComputerViewModel(Computer computer)
            : base("New computer", computer)
        {
        }

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

        public ObservableCollection<CategoryViewModel> FilteredCategories
        {
            get
            {
                List<CategoryViewModel> categories = null;

                if (this.Entity.ProductCategories != null)
                {
                    categories =
                        (from c in this.Entity.ProductCategories
                        select new CategoryViewModel(c.Category)).ToList();
                }

                return new ObservableCollection<CategoryViewModel>(categories);
            }
        }

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

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

        [EntityControl(ControlType.ComboBox, "Condition: ", 3), EntityColumn("Condition", 2)]
        public OrderEntryEngine.Condition Condition
        {
            get
            {
                return this.Entity.Condition;
            }

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

        public ICollection<OrderEntryEngine.Condition> Conditions
        {
            get
            {
                return Enum.GetValues(typeof(OrderEntryEngine.Condition)) as ICollection<OrderEntryEngine.Condition>;
            }
        }

        [EntityControlAttribute(ControlType.TextBox, "Description: ", 2), EntityColumn("Description", 3, 70)]
        public string Description
        {
            get
            {
                return this.Entity.Description;
            }

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

        [EntityControlAttribute(ControlType.CheckBox, "Refurbished: ", 4), EntityColumn("IsRefurbished", 4)]
        public bool IsRefurbished
        {
            get
            {
                return this.Entity.IsRefurbished;
            }

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

        [EntityControlAttribute(ControlType.Label, "Extended Price: ", 6), EntityColumn("ExtendedPrice", 5)]
        public decimal ExtendedPrice
        {
            get
            {
                return this.Entity.ExtendedPrice;
            }
        }

        [EntityControlAttribute(ControlType.TextBox, "Price: ", 7), EntityColumn("Price", 6)]
        public decimal Price
        {
            get
            {
                return this.Entity.Price;
            }

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

        [EntityControlAttribute(ControlType.TextBox, "RAM: ", 8), EntityColumn("Ram", 7)]
        public double Ram
        {
            get
            {
                return this.Entity.Ram;
            }

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

        [EntityControlAttribute(ControlType.TextBox, "CPU Speed: ", 9), EntityColumn("CpuSpeed", 8)]
        public double CpuSpeed
        {
            get
            {
                return this.Entity.CpuSpeed;
            }

            set
            {
                this.Entity.CpuSpeed = value;
                this.OnPropertyChanged("CPU Speed");
            }
        }

        [EntityControlAttribute(ControlType.TextBox, "Memory: ", 10), EntityColumn("Memory", 9)]
        public double Memory
        {
            get
            {
                return this.Entity.Memory;
            }

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

        [EntityControlAttribute(ControlType.ComboBox, "Graphics Card: ", 11), EntityColumn("GraphicsCard", 10)]
        public GraphicsCard GraphicsCard
        {
            get
            {
                return this.Entity.GraphicsCard;
            }

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

        [EntityControlAttribute(ControlType.ComboBox, "Location: ", 12), EntityColumn("Location", 11)]
        public Location Location
        {
            get
            {
                return this.Entity.Location;
            }

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

        [EntityControlAttribute(ControlType.TextBox, "Quantity: ", 5), EntityColumn("Quantity", 12)]
        public int Quantity
        {
            get
            {
                return this.Entity.Quantity;
            }

            set
            {
                this.Entity.Quantity = value;
            }
        }

        public ICollection<Location> Locations
        {
            get
            {
                return (RepositoryManager.GetRepository(typeof(Location)) as Repository<Location>).GetEntities();
            }
        }

        public ICollection<GraphicsCard> GraphicsCards
        {
            get
            {
                return (RepositoryManager.GetRepository(typeof(GraphicsCard)) as Repository<GraphicsCard>).GetEntities();
            }
        }

        public IEnumerable<Category> Categories
        {
            get
            {
                return (RepositoryManager.GetRepository(typeof(Category)) as Repository<Category>).GetEntities();
            }
        }

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