using System.Linq;
using System.Web.Mvc;
using Wierszowki.Core.Linq;
using Wierszowki.Models;
using Wierszowki.Services;
using xVal.ServerSide;

namespace Wierszowki.Controllers
{
    public class AuthorController : Controller
    {
        private readonly WierszowkiDataContext _context;

        public AuthorController()
        {
            _context = new WierszowkiDataContext();
        }

        public ActionResult Index()
        {
            var authors = _context.GetAuthors();
            return View(authors);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            var authorViewData = new AuthorViewData
                                     {
                                         Author = new Author(),
                                         EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
                                     };

            return View(authorViewData);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Author author)
        {
            var authorService = new AuthorService();
            try
            {
                authorService.Create(author);
            }
            catch (RulesException ex)
            {
                ex.AddModelStateErrors(ModelState, "author");
            }

            if (ModelState.IsValid)
                return RedirectToAction("Confirm", author);


            var authorViewData = new AuthorViewData
            {
                Author = author,
                EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
            };
            return View(authorViewData);
        }


        [Authorize]
        public ActionResult Confirm(Author author)
        {
            return View(author);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
            var authorViewData = new AuthorViewData
                     {
                         Author = _context.GetAuthorById(id),
                         EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
                     };

            return View(authorViewData);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Author author)
        {
            var authorService = new AuthorService();
            try
            {
                authorService.Update(author);
            }
            catch (RulesException ex)
            {
                ex.AddModelStateErrors(ModelState, "author");
            }

            if (ModelState.IsValid)
                return View("Confirm", author);

            var authorViewData = new AuthorViewData
            {
                Author = author,
                EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
            };

            return View(authorViewData);
        }
    }
}