using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore;
using System.Net;

namespace MonAppBookStore.Controllers
{
    public class BooksController : Controller
    {
        BookStore.Facade.IRepositoryBookStore bs = new BookStore.Implementation.BookStoreRepository();
        //
        // GET: /Books/
       [Authorize(Roles = "Utilisateur,Admin")]
        public ActionResult Index_User(String searchString, string publisherName)
        {
            

           
            var books = bs.findAll();
            var PublisherLst = bs.findAllPublishersByName();
          
           
            ViewBag.PublisherName = new SelectList(PublisherLst);

            if (!String.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.BookName.Contains(searchString));
            }
            if (!string.IsNullOrEmpty(publisherName))
            {
                books = books.Where(x => x.Publisher.PublisherName == publisherName);
            }
            return View(books.ToList());

        }

         [Authorize(Roles = "Admin")]
        public ActionResult Index_Admin(String searchString, string publisherName)
        {


           
            var books = bs.findAll();
            var PublisherLst = bs.findAllPublishersByName();


            ViewBag.PublisherName = new SelectList(PublisherLst);

            if (!String.IsNullOrEmpty(searchString))
            {
                books = books.Where(s => s.BookName.Contains(searchString));
            }
            if (!string.IsNullOrEmpty(publisherName))
            {
                books = books.Where(x => x.Publisher.PublisherName == publisherName);
            }
            return View(books.ToList());

        }

        public ActionResult Details_Admin(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BookStore.Model.Book book = bs.FindBook(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        public ActionResult Details_User(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BookStore.Model.Book book = bs.FindBook(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

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

            ViewBag.Publisher_PublisherId = new SelectList(bs.FindAllPublishers(), "PublisherId", "PublisherName");
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "BookId,BookName,Publisher_PublisherId,BookPrice")] BookStore.Model.Book book)
        {
            if (ModelState.IsValid)
            {
                var publisher = bs.FindPublisher(book.Publisher_PublisherId);
                book.Publisher = publisher;
                bs.AddBook(book);
                return RedirectToAction("Index_Admin");
            }

            ViewBag.Publisher_PublisherId = new SelectList(bs.FindAllPublishers(), "PublisherId", "PublisherName", book.Publisher_PublisherId);
            return View(book);
        }

         [Authorize(Roles = "Admin")]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BookStore.Model.Book book = bs.FindBook(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            ViewBag.Publisher_PublisherId = new SelectList(bs.FindAllPublishers(), "PublisherId", "PublisherName", book.Publisher_PublisherId);
            return View(book);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "BookId,BookName,Publisher_PublisherId,BookPrice")] BookStore.Model.Book book)
        {
             if (ModelState.IsValid)
            {
                bs.changeBook(book);
                return RedirectToAction("Index_Admin");
            }
          ViewBag.Publisher_PublisherId = new SelectList(bs.FindAllPublishers(), "PublisherId", "PublisherName", book.Publisher.PublisherId);
            return View(book);
        
        }

         [Authorize(Roles = "Admin")]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BookStore.Model.Book book = bs.FindBook(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

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