using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using LHDN.CRM.BC.Common;
using LHDN.CRM.BC.Services;
using LHDN.CRM.Models.ViewModels;

namespace LHDN.CRM.WebAPI
{
    public class CRMRoleProvider : RoleProvider
    {
        //Implemented at least the two methods IsUserInRole and GetRolesForUser.
        //All other methods can be left throwing a NotImplementedException.
        public static string CachePostfix = "_roles";
        private UserProfileService userSvc = new UserProfileService();
        
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            UserInfoViewModel user = userSvc.GetUserByLoginID(); //GetCurrentUserInfo(username);

            return user.RolesObj.Select(a=> a.RoleCode.ToString()).ToList().ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            UserInfoViewModel user = userSvc.GetUserByLoginID(); //GetCurrentUserInfo(username);

            return (user.RolesObj.Count>0 && user.RolesObj.Any(x => x.RoleCode == roleName));
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        private UserInfoViewModel GetCurrentUserInfo(string username)
        {
            string strCacheName = Utilities.GetUserCacheName(username);
            UserInfoViewModel user;
            List<string> lstRoles = new List<string>();

            if (!CachingService.TryGet<UserInfoViewModel>(strCacheName, out user))
            {
                CachingService.TryGetOrAdd<UserInfoViewModel>(strCacheName, () => userSvc.GetUserByLoginID(), out user, 3600);
            }
            return user;
        }
    }
}