using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using OrderEntryDataAccess;
using OrderEntryEngine;

namespace OrderEntrySystem
{
    public class AddCategoryViewModel : EntityViewModel<Computer>
    {
        public AddCategoryViewModel(Computer computer)
            : base("Add category", computer)
        {
        }

        public Category Category { get; set; }

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

        protected override bool Save()
        {
            bool result = true;

            if (this.Entity.IsValid)
            {
                ComputerCategory pc = new ComputerCategory();

                pc.Category = this.Category;
                pc.Computer = this.Entity;

                (RepositoryManager.GetRepository(typeof(ComputerCategory)) as Repository<ComputerCategory>).AddEntity(pc);

                RepositoryManager.GetRepository(typeof(ComputerCategory)).SaveToDatabase();
            }
            else
            {
                result = false;
                MessageBox.Show("One or more fields are invalid. The product category could not be saved.");
            }

            return result;
        }
    }
}