using System;
using System.Linq;
using System.Web.Mvc;
using DataClasses;
using DataRepositories;
using WebApplication.Models;

namespace WebApplication.Controllers
{
    public class AccountController : Controller
    {
        private readonly DataContext context = new DataContext();


        //
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login(string ReturnUrl)
        {
            SimpleMemberShipUser.LogOut();

            if (!string.IsNullOrEmpty(ReturnUrl))
            {
                ViewBag.ReturnUrl = ReturnUrl;
                ModelState.AddModelError("", "Nemáte oprávnenie na túto sekciu, prosím prihláste sa!");
            }
            return View();
        }


        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(SimpleMemberShipUser user, string ReturnUrl)
        {
            if (ModelState.IsValid && user.LogIn())
            {
                if (!string.IsNullOrEmpty(ReturnUrl))
                {
                    return Redirect(ReturnUrl);
                }

                return RedirectToAction("Index", "TimeTable");
            }

            ModelState.AddModelError("", "Chybné prihlasovacie meno alebo heslo!");
            return View(user);
        }


        //
        // GET: _LoginPartial
        [ChildActionOnly]
        [AllowAnonymous]
        public PartialViewResult LoginStatus()
        {
            if (!string.IsNullOrEmpty(User.Identity.Name))
            {
                var userRepository = new Repository<UserProfile>(context);

                ViewBag.UserName = userRepository.GetFullUserName(User.Identity.Name);
                ViewBag.Roles = String.Join(", ", SimpleMemberShipUser.GetLocalizedRoles(User.Identity.Name).ToArray());
            }
            ViewBag.Controller = RouteData.Values["controller"];

            return PartialView("_LoginPartial");
        }


        //
        // GET: /Account/LogOut
        [AllowAnonymous]
        public ActionResult LogOut()
        {
            SimpleMemberShipUser.LogOut();
            return RedirectToAction("Index", "TimeTable");
        }


        //
        // GET: _RoleBasedMenuPartial
        [ChildActionOnly]
        [AllowAnonymous]
        public PartialViewResult RoleBasedMenu()
        {
            var model = SimpleMemberShipUser.GetRoles(User.Identity.Name);
            return PartialView("_RoleBasedMenuPartial", model);
        }


        //
        // GET: /Account/UserProfile
        [Authorize]
        public ActionResult UserProfile(string ReturnUrl)
        {
            var profileRepository = new Repository<UserProfile>(context);
            string login = SimpleMemberShipUser.CurrentUserName();

            var model = (from u in profileRepository.GetAll()
                         where u.Login == login
                         select u).FirstOrDefault();

            return View(model);
        }
    }
}
