using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DataAccess;
using DTO;
using Helper;

namespace BusinessLayer
{
    public class UserManager : BLBase
    {
        public UserDTO GetUser(int? UserId, string UserName, string Password)
        {
            GetUser_Result User = context.GetUser(UserId, string.IsNullOrEmpty(UserName) ? null : UserName, string.IsNullOrEmpty(Password) ? null : Password).FirstOrDefault();

            UserDTO UserInfo = null;
            if (User != null)
            {
                Mapper.CreateMap<GetUser_Result, UserDTO>();
                UserInfo = Mapper.Map<GetUser_Result, UserDTO>(User);
            }

            return UserInfo;
        }

        public UserDTO IsValidEmail(string email)
        {
            GetSiteUser_Result user = context.GetSiteUser(null, null, null, null, email).FirstOrDefault();
            UserDTO UserInfo = null;
            if (user != null)
            {
                Mapper.CreateMap<GetSiteUser_Result, UserDTO>();
                UserInfo = Mapper.Map<GetSiteUser_Result, UserDTO>(user);
            }

            return UserInfo;
        }

        public bool ChangePassword(int UserId, string NewPassword)
        {
            if (string.IsNullOrEmpty(NewPassword))
                NewPassword = Common.GenerateRandomPassword(8);
            context.ChangePassword(UserId, NewPassword);
            return true;
        }

        public IList<RoleRightsDTO> GetRoleRights(int RoleId)
        {
            IList<GetRoleRights_Result> roleRightsResult = context.GetRoleRights(RoleId).Where(c => c.ViewAccess == true).ToList();
            IList<RoleRightsDTO> roleRights = null;

            if (roleRightsResult != null)
            {
                Mapper.CreateMap<GetRoleRights_Result, RoleRightsDTO>();
                roleRights = Mapper.Map<IList<GetRoleRights_Result>, IList<RoleRightsDTO>>(roleRightsResult).ToList();
            }

            return roleRights;
        }

        public IList<UserRoleLocationRegionCountryDTO> GetUserRoleLocationRegionCountry(int? UserId, string UserName, string Email, int? Status, int? RoleId, string RoleName,
                                                                     int? LocationId, string LocationName, int? RegionId, string RegionName, int? CountryId, string CountryName)
        {
            bool? stat = null;
            if (Status != null)
            {
                stat = (Status == 1) ? true : false;
            }

            IList<GetUserRoleLocationRegionCountry_Result> Result =
                context.GetUserRoleLocationRegionCountry(UserId,
                                                        (string.IsNullOrEmpty(UserName)) ? null : UserName,
                                                        (string.IsNullOrEmpty(Email)) ? null : Email,
                                                        stat,
                                                        (RoleId == 0) ? null : RoleId,
                                                        (string.IsNullOrEmpty(RoleName)) ? null : RoleName,
                                                        (LocationId == 0) ? null : LocationId,
                                                        (string.IsNullOrEmpty(LocationName)) ? null : LocationName,
                                                        (RegionId == 0) ? null : RegionId,
                                                        (string.IsNullOrEmpty(RegionName)) ? null : RegionName,
                                                        (CountryId == 0) ? null : CountryId,
                                                        (string.IsNullOrEmpty(CountryName)) ? null : CountryName).ToList();

            IList<UserRoleLocationRegionCountryDTO> Users = null;

            if (Result != null)
            {
                Mapper.CreateMap<GetUserRoleLocationRegionCountry_Result, UserRoleLocationRegionCountryDTO>();
                Users = Mapper.Map<IList<GetUserRoleLocationRegionCountry_Result>, IList<UserRoleLocationRegionCountryDTO>>(Result).ToList();
            }

            return Users;
        }

        public IList<ReporteesForUserCreationDTO> GetReporteesForUserCreation(int RoleId, int LocationId)
        {
            int? locID = null;
            if (LocationId != 0 && LocationId != null)
                locID = LocationId;

            IList<GetReporteesForUserCreation_Result> Result = context.GetReporteesForUserCreation(RoleId, locID).ToList();

            IList<ReporteesForUserCreationDTO> Users = null;

            if (Result != null)
            {
                Mapper.CreateMap<GetReporteesForUserCreation_Result, ReporteesForUserCreationDTO>();
                Users = Mapper.Map<IList<GetReporteesForUserCreation_Result>, IList<ReporteesForUserCreationDTO>>(Result).ToList();
            }

            return Users;
        }

        public void InsertUpdateUser(UserDTO User)
        {
            int? UserId = null;
            if (User.Site_User_Id != 0)
                UserId = User.Site_User_Id;

            context.InsertUpdateUser(UserId,
                                     User.Name,
                                     User.Password,
                                     User.Email,
                                     User.Role_Id,
                                     User.Location_Id,
                                     User.Employee_Code,
                                     User.Status,
                                     string.Join(",", User.LinkedExecutiveIds.ToArray()),
                                     User.Language_Id
                                     );
        }

        public bool IsEmailDuplicate(string Email)
        {
            if (context.Site_User.Any(c => c.Email == Email))
                return true;
            else
                return false;
        }

        public void InsertUpdateLinkedExecutives(IList<int> LinkedExecutiveIds, int ManagerId)
        {
            context.InsertUpdateLinkedExecutive(ManagerId, string.Join(",", LinkedExecutiveIds.ToArray()));
        }
    }
}
