using DAL._Shared.SharedModels;
using HRD.LDAPService;
using HRD.WebApi.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DAL._Shared.SharedRepositories
{
    public class WebAppEmployeeInfoRepository : BaseRepository<WebAppEmployeeInfo>

    {
        public WebAppEmployeeInfoRepository(WebApiContext context) : base(context)
        {
        }

        private IQueryable<WebAppEmployeeInfo> PrepareList(List<int> departmentIds)
        {
            IQueryable<WebAppEmployeeInfo> items = RepositoryContext.Set<WebAppEmployeeInfo>().AsNoTracking();
            return items.Where(x => departmentIds.Contains(x.DepartmentId));
        }

        public async Task<List<string>> GetEmailListForDepartmentsAsync(List<int> departmentIds)
        {
            return await PrepareList(departmentIds).Select(x => x.Email).ToListAsync();
        }

        public async Task<List<string>> GetLoginnameListForDepartmentsAsync(List<int> departmentIds)
        {
            return await PrepareList(departmentIds).Select(x => x.LoginName).ToListAsync();
        }

        public async Task<List<string>> GetShortnameListForDepartmentsAsync(List<int> departmentIds)
        {
            return await PrepareList(departmentIds).Select(x => x.ShortName).ToListAsync();
        }

        public async Task<string> GetShortnameForLoginAsync(string loginname)
        {
            return (await RepositoryContext.Set<WebAppEmployeeInfo>().FirstAsync(x => x.LoginName == loginname)).ShortName;
        }

        //----------------
        public List<string> GetEmailListForDepartments(List<int> departmentIds)
        {
            return PrepareList(departmentIds).Select(x => x.Email).ToList();
        }

        public List<string> GetLoginnameListForDepartments(List<int> departmentIds)
        {
            return PrepareList(departmentIds).Select(x => x.LoginName).ToList();
        }

        public List<string> GetShortnameListForDepartments(List<int> departmentIds)
        {
            return PrepareList(departmentIds).Select(x => x.ShortName).ToList();
        }

        public string GetShortnameForLogin(string loginname)
        {
            return RepositoryContext.Set<WebAppEmployeeInfo>().First(x => x.LoginName == loginname).ShortName;
        }

        //----------------
        public bool PrepareForUserLoginnameOrLoginnameListFilter(LdapUser ldapUser, ref string loginName, ref List<string> loginNameList)
        {
            if (ldapUser.IsAdmin() || ldapUser.IsMaster())
            {
            }
            else if (ldapUser.IsDepartmentMaster() || ldapUser.IsDepartmentUser())
            {
                loginNameList = GetLoginnameListForDepartments(ldapUser.DepartmentIdListAll());
                loginName = default;
            }
            else if (ldapUser.IsUser())
            {
                loginNameList = default;
                loginName = ldapUser.LoginName;
            }
            else
            {
                return false; //shouldn't happen
            }
            return true;
        }

        public bool PrepareForUserEmailOrEmailListFilter(LdapUser ldapUser, ref string EmailBV, ref List<string> EmailBVList)
        {
            if (string.IsNullOrEmpty(EmailBV)) //check for user & master
            {
                if (ldapUser.IsAdmin() || ldapUser.IsMaster())
                {
                    // No correction
                }
                else if (ldapUser.IsDepartmentMaster() || ldapUser.IsDepartmentUser())
                {
                    EmailBVList = GetEmailListForDepartments(ldapUser.DepartmentIdListAll());
                    EmailBV = default;
                }
                else if (ldapUser.IsUser())
                {
                    EmailBVList = default;
                    EmailBV = ldapUser.Email;
                }
                else
                {
                    return false; //shouldn't happen
                }
            }
            return true;
        }
    }
}