using DAL._Shared.SharedModels;
using DAL._Shared.SharedRepositories;
using HRD.LDAPService;
using HRD.LDAPService.JWT;
using StaffDBServer.SharedControllers;
using Xunit;
using XUnitWebApi.SharedConfig;

namespace XUnitWebApi.SharedLDAP
{
    public class Shared_Test_LDAP
    {
        [Theory]
        [InlineData("user", "pwd")]
        [InlineData("user2", "pwd")]
        public async System.Threading.Tasks.Task Validate_Credentials_WebAppUser(string login, string passwort)
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                WebAppUser userFromClient = new WebAppUser(
                    login,
                    login,
                    "User",
                    login);
                userFromClient.Password = passwort;

                WebAppEmployeeInfoRepository webAppEmployeeInfoRepository = new WebAppEmployeeInfoRepository();
                WebAppEmployeeInfo webAppEmployeeInfo = await webAppEmployeeInfoRepository.GetByAsync(x => x.LoginName == userFromClient.LoginName);
                Assert.NotNull(webAppEmployeeInfo);

                WebAppUserHelper webAppUserHelper = new WebAppUserHelper();
                LdapUser ldapUserFromClient = new LdapUser(userFromClient.LoginName, webAppEmployeeInfo.EmployeeId, userFromClient.Password, webAppEmployeeInfo.DepartmentId, webAppEmployeeInfo.ExtendedDepartmentIdList);
                Assert.NotNull(ldapUserFromClient);

                bool ldapUserOk = JwtManager.GenerateLdapUserWithJwtToken(ldapUserFromClient);
                Assert.True(ldapUserOk);

                WebAppUser newUser = await webAppUserHelper.CheckLoginWithNameAndPasswordAsync(userFromClient);

                Assert.NotNull(newUser);
            }
            catch (Exception)
            {
                throw;
            }
        }


        [Theory]
        [InlineData("visitoripad", "pwd")]
        public void Validate_Credentials_with_password(string login, string passwort)
        {
            try
            {
                LdapUser ldapUser = new LdapUser(login, 0, passwort);
                var result = LdapAuthenticationService.CheckAndUpdateIdentityWithPassword(ldapUser);

                Assert.True(result);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [Theory]
        [MemberData(nameof(GetLdapUsers))]
        public void Validate_Credentials_with_password_Version2(LdapUser ldapUser)
        {
            try
            {
                var result = LdapAuthenticationService.CheckAndUpdateIdentityWithPassword(ldapUser);

                Assert.True(result);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static IEnumerable<object[]> GetLdapUsers()
        {
            LdapUser ldapUser = new LdapUser("user1", 0, "pwd");
            LdapUser ldapUser2 = new LdapUser("Error", 0, "pwd");

            var allData = new List<object[]>
            {
                new object[] { ldapUser },
                new object[] { ldapUser2 }
            };

            return allData;
        }
    }
}