using System.Linq;
using System.Web.Mvc;
using Wierszowki.Core.Linq;
using Wierszowki.Models;
using Wierszowki.Services;
using xVal.ServerSide;
using System.Collections.Generic;
using Wierszowki.Core.Interfaces;

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)
            {
                var confirmViewData = new ConfirmViewData();
                confirmViewData = InitConfirmViewData(author, OperationType.Create, null);
                return View("Confirm", confirmViewData); 
                //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)
            {
                var confirmViewData = new ConfirmViewData();
                confirmViewData = InitConfirmViewData(author, OperationType.Update, null);
                return View("Confirm", confirmViewData); 
            }
            var authorViewData = new AuthorViewData
            {
                Author = author,
                EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(), "Id", "Name")
            };

            return View(authorViewData);
        }
        private ConfirmViewData InitConfirmViewData(Author author, OperationType c_type, Info inf)
        {
            var viewData = new ConfirmViewData
            {
                Author = author,
                akcja = c_type,
                info = inf
            };
            return viewData;
        }
    }
}