using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Collections.Generic;
using System.Web.Security;
using WebMatrix.WebData;

namespace DataClasses
{
    public class SimpleMemberShipUser
    {
        #region "Properties"

        [Required(ErrorMessage = "Povinné vyplňiť")]
        [Display(Name = "Heslo")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Display(Name = "Trvalé prihlásenie")]
        public bool RemeberMe { get; set; }

        public UserProfile Profile { get; set; }

        #endregion


        #region "Public methods"

        /// <summary>
        /// Saves user as registered user.
        /// </summary>
        /// <returns>Returns error message, if error occurs</returns>
        public string SaveAsRegisteredUser()
        {
            if (WebSecurity.UserExists(Profile.Login))
            {
                return ("Užívateľ s danými prihlasovacími údajmi už existuje!");
                
            }

            WebSecurity.CreateUserAndAccount(Profile.Login, Password,
                                             new
                                             {
                                                 FirstName = Profile.FirstName,
                                                 LastName = Profile.LastName,
                                                 Email = Profile.Email
                                             });
            return string.Empty;
        }


        /// <summary>
        /// Adds user to specific Role.
        /// </summary>
        /// <param name="role">Role</param>
        /// <returns>Returns error message, if error occurs</returns>
        public string AddUserToRole(RoleEnum role)
        {
            if (!Roles.GetAllRoles().Contains(role.ToString()))
            {
                return("Daná rola neexistuje!");
            }
            if (Roles.GetRolesForUser(Profile.Login).Contains(role.ToString()))
            {
                return ("Užívateľ už je priradený do danej role!");
            }

            Roles.AddUserToRole(Profile.Login, role.ToString());
            return string.Empty;
        }


        /// <summary>
        /// Logs in actual user.
        /// </summary>
        /// <returns>False if not succeeded</returns>
        public bool LogIn()
        {
            return WebSecurity.Login(Profile.Login, Password, RemeberMe);
        }


        /// <summary>
        /// Logs out actual user.
        /// </summary>
        public static void LogOut()
        {
            WebSecurity.Logout();
        }

        #endregion


        #region "Static methods"

        /// <summary>
        /// Gets roles for specific user.
        /// </summary>
        /// <param name="login">login name</param>
        /// <returns>List of roles</returns>
        public static List<string> GetRoles(string login)
        {
            return string.IsNullOrEmpty(login) ? null : Roles.GetRolesForUser(login).ToList();
        }


        /// <summary>
        /// Gets localized roles for specific user.
        /// </summary>
        /// <param name="login">login name</param>
        /// <returns>List of localized roles</returns>
        public static List<string> GetLocalizedRoles(string login)
        {
            List<String> result = GetRoles(login);

            if (result == null)
            {
                return null;
            }
            
            return result.Select(k => StringEnum.GetStringValue(
                (RoleEnum)Enum.Parse(typeof(RoleEnum), k)))
                .ToList();
        }

                    
        /// <summary>
        /// Creates specific role.
        /// </summary>
        /// <param name="role">Role name</param>
        public static void CreateRole(RoleEnum role)
        {
            Roles.CreateRole(role.ToString());
        }


        /// <summary>
        /// Gets current user userName
        /// </summary>
        /// <returns>User name</returns>
        public static string CurrentUserName()
        {
            return WebSecurity.CurrentUserName;
        }

        #endregion
    }
}