using System;
using System.Web.Mvc;
using System.Web.Security;
using Qusion.Cms.Users.Core;
using Qusion.Cms.Users.Domain;
using Qusion.Framework.Data;
using Qusion.WebApp.Areas.Qusion.Filters;
using Qusion.WebApp.Areas.Qusion.Mailers.Account;
using WebMatrix.WebData;

namespace Qusion.WebApp.Areas.Qusion.Controllers
{
    [InitializeSimpleMembership]
    public class IndexerController : Controller
    {
        private readonly UserService _userService;

        public IndexerController(UserService userService)
        {
            _userService = userService;
        }

        public ActionResult Reindex()
        {
            LuceneIndex.Reindex();
            return Content("1");
        }

        public ActionResult Install()
        {
            var builder = new SessionBuilder();
            builder.CreateDatabase();
            return Content("1");
        }

        public ActionResult SetRoles()
        {
            
                Roles.CreateRole("Administrators");

          
                Roles.CreateRole("Members");

           
                Roles.CreateRole("Users");
            return null;
        }

        public ActionResult Administrator()
        {
            if (!Roles.RoleExists("Administrators"))
                Roles.CreateRole("Administrators");

            var user = new User
                           {
                               FirstName = "Nenad",
                               LastName = "Jesic",
                               UserName = "master@nenadjesic.com",
                               Roles = "Administrators",
                               
                           };

            _userService.Save(user);


            WebSecurity.CreateAccount(user.UserName, "1234rewq");

            Roles.AddUserToRole("master@nenadjesic.com", "Administrators");
            return Content(user.UserName);
        }

        public ActionResult Posalji()
        {
            var mail = new CreateNewAccount
            {
                Email = "nenad.jesic@laktasi.net",
                UserName = "nenad.jesic@laktasi.net",
                Password = "kolikojesati"
            };
            // send password reset token
            new AccountMailer().CreateUser(mail).Send();
            return Json(1, JsonRequestBehavior.AllowGet);
        }
    }
}