using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using BooksStoreMVC.Models;

namespace BooksStoreMVC.Controllers
{
    public class BooksController : Controller
    {
        private Model1 db = new Model1();

        // GET: /Books/
        [Authorize(Roles="Utilisateur,Admin")]
        public ActionResult Index(String searchString, string publisherName)
        {
            var books = db.Books.Include(b => b.Publisher);
            var PublisherLst = new List<string>();

            var PublisherQry = from d in db.Publishers
                           orderby d.PublisherName
                            select d.PublisherName;

            PublisherLst.AddRange(PublisherQry.Distinct());
            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 = db.Books.Include(b => b.Publisher);
            var PublisherLst = new List<string>();

            var PublisherQry = from d in db.Publishers
                               orderby d.PublisherName
                               select d.PublisherName;

            PublisherLst.AddRange(PublisherQry.Distinct());
            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());

        }
        // GET: /Books/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        public ActionResult Details_Admin(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        // GET: /Books/Create
         [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            ViewBag.Publisher_PublisherId = new SelectList(db.Publishers, "PublisherId", "PublisherName");
            return View();
        }

        // POST: /Books/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="BookId,BookName,Publisher_PublisherId")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return RedirectToAction("Index_Admin");
            }

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

        // GET: /Books/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            ViewBag.Publisher_PublisherId = new SelectList(db.Publishers, "PublisherId", "PublisherName", book.Publisher_PublisherId);
            return View(book);
        }

        // POST: /Books/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="BookId,BookName,Publisher_PublisherId")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Entry(book).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index_Admin");
            }
            ViewBag.Publisher_PublisherId = new SelectList(db.Publishers, "PublisherId", "PublisherName", book.Publisher_PublisherId);
            return View(book);
        }

        // GET: /Books/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        // POST: /Books/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Book book = db.Books.Find(id);
            db.Books.Remove(book);
            db.SaveChanges();
            return RedirectToAction("Index_Admin");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
