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

namespace OrderEntrySystem
{
    public class ComputerViewModel : EntityViewModel<Computer>, IDataErrorInfo
    {
        private MultiEntityViewModel<Category, CategoryViewModel, CategoryView> filteredCategoryViewModel;

        public ComputerViewModel(Computer computer)
            : base("New computer", computer)
        {
            this.filteredCategoryViewModel = new MultiEntityViewModel<Category, CategoryViewModel, CategoryView>();
            this.filteredCategoryViewModel.AllEntities = this.FilteredCategories;
        }

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

        public MultiEntityViewModel<Category, CategoryViewModel, CategoryView> FilteredCategoryViewModel
        {
            get
            {
                return this.filteredCategoryViewModel;
            }
        }

        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();
                }

                this.FilteredCategoryViewModel.AddPropertyChangedEvent(categories);

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

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

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

        [EntityControl(ControlType.ComboBox, "Condition: ")]
        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: ")]
        public string Description
        {
            get
            {
                return this.Entity.Description;
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        [EntityControlAttribute(ControlType.TextBox, "Quantity: ")]
        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];
            }
        }
    }
}