using BookStore_DataAccess.model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace BookStoreMVC.Controllers
{
    public class BooksController : Controller
    {
        public BookStore_DataAccess.repository.UnitOfWork unitOfWork = new BookStore_DataAccess.repository.UnitOfWork();
        // GET: /Books/
        [Authorize(Roles = "User,Admin")]
        public ActionResult Index_User(String searchString)
        { 
            var books = unitOfWork.BookRepository.GetAll();
            var PublisherLst = unitOfWork.PublisherRepository.GetAll();

            ViewBag.PublisherName = new SelectList(PublisherLst);

            if (!String.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.BookTitle.Contains(searchString)).ToList();
            }
            return View(books);

        }

        [Authorize(Roles = "Admin")]
        public ActionResult Index_Admin(String searchString)
        {
            var books = unitOfWork.BookRepository.GetAll();
            var PublisherList = unitOfWork.PublisherRepository.GetAll();

        
            if (!String.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.BookTitle.Contains(searchString)).ToList();
            }

            return View(books);

        }
        
        public ActionResult Detail_Admin(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = unitOfWork.BookRepository.GetById(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        public ActionResult Detail_User(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = unitOfWork.BookRepository.GetById(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult CreateBook()
        {


            ViewBag.PublisherId = new SelectList(unitOfWork.PublisherRepository.GetAll(), "PublisherId", "PublisherName");
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateBook([Bind(Include = "BookId,BookTitle,PublisherId,BookPrice")] Book book)
        {
            if (ModelState.IsValid)
            {
                var publisher = unitOfWork.PublisherRepository.GetById(book.PublisherId);
                book.Publisher = publisher;
                unitOfWork.BookRepository.Insert(book);
                unitOfWork.Save();
                return RedirectToAction("Index_Admin");
            }

            ViewBag.PublisherId = new SelectList(unitOfWork.PublisherRepository.GetAll(), "PublisherId", "PublisherName", book.PublisherId);
            return View(book);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult EditBook(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = unitOfWork.BookRepository.GetById(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            ViewBag.Publisher_PublisherId = new SelectList(unitOfWork.PublisherRepository.GetAll(), "PublisherId", "PublisherName", book.PublisherId);
            return View(book);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditBook([Bind(Include = "BookId,BookTitle,PublisherId,BookPrice")] Book book)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.BookRepository.Update(book);
                unitOfWork.Save();
                return RedirectToAction("Index_Admin");
            }
            ViewBag.Publisher_PublisherId = new SelectList(unitOfWork.PublisherRepository.GetAll(), "PublisherId", "PublisherName", book.Publisher.PublisherId);
            return View(book);

        }

        [Authorize(Roles = "Admin")]
        public ActionResult DeleteBook(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = unitOfWork.BookRepository.GetById(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        // POST: Books/Delete/5
        [HttpPost, ActionName("DeleteBook")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            unitOfWork.BookRepository.Delete(id);
            unitOfWork.Save();
            return RedirectToAction("Index_Admin");
        }

    }
}