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

namespace Wierszowki.Controllers
{
    [HandleError]
    [Authorize]
    public class UserController : Controller
    {
        private readonly IUserService _service;

        public UserController()
        {
            _service = new UserService(new LinqRepository<User>());
        }

        public UserController(IUserService service)
        {
            _service = service;
        }

        public ActionResult Index()
        {
            var users = _service.FindAll();
            return View(users);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            return View(new User());
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(User user)
        {
            try
            {
                _service.Create(user);
            }
            catch (RulesException ex)
            {
                ex.AddModelStateErrors(ModelState, "user");
            }

            if (ModelState.IsValid)
            {
                var userConfirmViewData = new UserConfirmViewData();
                userConfirmViewData = InitUserConfirmViewData("Potwierdzenie dodania użytkownika", "", OperationType.Create, user);   
                return View("Confirm", userConfirmViewData);
            }
            return View(user);
        }

        [Authorize]
        public ActionResult Confirm(User user)
        {
            return View(user);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
            User user = _service.Find(id);
            string errorInfo = "";
            if (user != null)
            {
                string name = ControllerContext.HttpContext.User.Identity.Name;
                var LogInUser = _service.FindOne(u => u.Login == name);
                if (LogInUser.Id == user.Id)
                {
                    return View(_service.Find(id));
                }
                else
                {
                    var userConfirmViewData = new UserConfirmViewData();
                    errorInfo = "Nie można edytować użytkownika, gdyż login różni się od zalogowanego!!!";
                    userConfirmViewData = InitUserConfirmViewData("Edycja użytkownika", errorInfo, OperationType.ErrorLogin, null);
                    return View("Confirm", userConfirmViewData);                    
                }
            }
            var userConfirmViewData1 = new UserConfirmViewData();
            errorInfo = "Nie można edytować użytkownika, gdyż użytkownik o takim id nie istnieje !!!";
            userConfirmViewData1 = InitUserConfirmViewData("Edycja użytkownika", errorInfo, OperationType.ErrorUser, null);
            return View("Confirm", userConfirmViewData1);                 
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(User user)
        {
            try
            {
                _service.Update(user);
               
            }
            catch (RulesException ex)
            {
                ex.AddModelStateErrors(ModelState, "user");
            }

            if (ModelState.IsValid)
            {
                var userConfirmViewData = new UserConfirmViewData();
                userConfirmViewData = InitUserConfirmViewData("Potwierdzenie edycji użytkownika", "", OperationType.Update, user);
                return View("Confirm", userConfirmViewData); 
            }
            return View(user);
        }
        private UserConfirmViewData InitUserConfirmViewData(string naglowek, string error, OperationType u_type, User user)
        {
            var viewData = new  UserConfirmViewData
            {
                Naglowek = naglowek,
                ErrorInfo= error,
                Akcja  = u_type,
                User = user
            };
            return viewData;        
        }
    }
}