using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BookStore_DataAccess.repository;
using BookStore_DataAccess.model;
using WebApiEntityFramework.Models;
using System.Web.Http.Description;

namespace BookStore_WebApi.Controllers
{
    public class BooksController : ApiController
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        // GET: api/Books
        public IQueryable<BookDTO> GetBooks()
        {
            //Eager loading
            //return db.Books.Include(b => b.Author);
            var Books = from b in unitOfWork.BookRepository.GetAll()
                        select new BookDTO()
                        {
                            Id = b.BookId,
                            Title = b.BookTitle,
                            PublisherName = b.Publisher.PublisherName
                        };
            return Books.AsQueryable();
        }

        // GET: api/Books/5
        [ResponseType(typeof(BookDetailDTO))]
        public IHttpActionResult GetBook(int id)
        {
            var Book = from b in unitOfWork.BookRepository.GetAll()
                       select new BookDetailDTO()
                       {
                           Id = b.BookId,
                           Title = b.BookTitle,
                           Year = b.BookYear,
                           Price = b.BookPrice,
                           PublisherName = b.Publisher.PublisherName
                       };
            if (Book == null)
            {
                return NotFound();
            }

            return Ok(Book);
        }

        // PUT: api/Books/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutBook(int id, Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != book.BookId)
            {
                return BadRequest();
            }

            unitOfWork.BookRepository.Update(book);
            unitOfWork.Save();
            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Books
        [ResponseType(typeof(Book))]
        public IHttpActionResult PostBook(Book book)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            unitOfWork.BookRepository.Insert(book);
            unitOfWork.Save();

            // Load author name
            unitOfWork.BookRepository.Insert(book);

            var newBook = new BookDTO()
            {
                Id = book.BookId,
                Title = book.BookTitle,
                PublisherName = book.Publisher.PublisherName
            };
            return CreatedAtRoute("DefaultApi", new { id = book.BookId }, newBook);
        }

        // DELETE: api/Books/5
        [ResponseType(typeof(Book))]
        public IHttpActionResult DeleteBook(int id)
        {
            Book book = unitOfWork.BookRepository.GetById(id);
            if (book == null)
            {
                return NotFound();
            }

            unitOfWork.BookRepository.Delete(book);
            unitOfWork.Save();
            return Ok(book);
        }

        private bool BookExists(int id)
        {
            return unitOfWork.BookRepository.GetAll().LongCount() > 0;
        }
    }
}
