using System;
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 MagazineItemController : Controller
    {
        private readonly WierszowkiDataContext _context;
        private readonly MagazineItemService _service;

        public MagazineItemController()
        {
            _context = new WierszowkiDataContext();
            _service = new MagazineItemService();
        }

        public ActionResult Index()
        {
            return RedirectToAction("Index", "Report");
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Get)]
        [OutputCache(Duration = 10, VaryByParam = "none")]
        public ActionResult Create()
        {
            var viewData = InitMagazineItemViewData();
            return View(viewData);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(MagazineItem magazineItem)
        {
            try
            {
                _service.Create(magazineItem, ControllerContext.HttpContext.User.Identity.Name);
                ModelState.Clear();
            }
            catch (RulesException ex)
            {
                ex.AddModelStateErrors(ModelState, "magazineItem");
            }

            if (ModelState.IsValid)
            {
                var author = _context.Authors.Single(a => a.Id == magazineItem.AuthorId);
                return RedirectToAction("Confirm", author);
            }

            var viewData = InitMagazineItemViewData();
            viewData.MagazineItem = magazineItem;
            return View(viewData);
        }

        [Authorize]
        public ActionResult Confirm(Author author)
        {
            return View(author);
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            var viewData = InitMagazineItemViewData();
            var magazineItem = _service.Find(id);
            viewData.MagazineItem = magazineItem;
            viewData.Magazine = magazineItem.Issue.Magazine;
            viewData.Issue = magazineItem.Issue;
            viewData.IssueList = new SelectList(_context.FindIssuesByMagazineItemId(magazineItem.Issue.Id), "Id", "Number");
            return View(viewData);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update(MagazineItem magazineItem)
        {
            try
            {
                //var service = new MagazineItemService();
                _service.Update(magazineItem, ControllerContext.HttpContext.User.Identity.Name);
                ModelState.Clear();
            }
            catch (RulesException ex)
            {
                ex.AddModelStateErrors(ModelState, "magazineItem");
            }

            if (ModelState.IsValid)
            {
                var author = _context.Authors.Single(a => a.Id == magazineItem.AuthorId);
                return RedirectToAction("Confirm", author);
            }

            var viewData = InitMagazineItemViewData();
            viewData.MagazineItem = magazineItem;
            return View(viewData);
        }

        private MagazineItemViewData InitMagazineItemViewData()
        {
            var viewData = new MagazineItemViewData
                               {
                                   MagazineItem = new MagazineItem(),
                                   AuthorList = new SelectList(_context.Authors.ToList(), "Id", "FullName"),
                                   MagazineList = new SelectList(_context.Magazines.ToList(), "Id", "NickName"),
                                   IssueList = new SelectList(_context.FindIssuesByMagazineId(0), "Id", "Name"),
                                   ItemTypeList = new SelectList(_context.ItemTypes.ToList(), "Id", "Name")
                               };
            return viewData;
        }
    }
}