using DAL;
using DAL._Shared.SharedRepositories;
using HRD.WebApi;
using System.Reflection;
using Xunit;
using XUnitWebApi.SharedConfig;
using XUnitWebApi.SharedUtils;

namespace XUnitWebApi.SahredDAL
{
    public class Shared_Test_DAL
    {
        [Fact]
        public async System.Threading.Tasks.Task Check_Repository_XXXAsync()
        {
            Shared_Test_Config.Init_Webapi_Context();
            WebAppUserRepository webAppUserRepository = new WebAppUserRepository();

            var res = await webAppUserRepository.TakeListAsync(2);
            Assert.NotEmpty(res);
        }

        [Fact]
        public void Check_Repository_By_Name()
        {
            var res = Check_Repositories(0, true, "Case");
            Assert.True(res);
        }

        [Fact]
        public void Check_All_Repositories()
        {
            var res = Check_Repositories(0, true);
            Assert.True(res);
        }

        [Fact]
        public void Check_All_Repositories_With_Values()
        {
            var res = Check_Repositories(1, true);
            Assert.True(res);
        }

        private bool Check_Repositories(int count = 0, bool raiseRepositoryExceptions = false, string repositoryName = "", string namespaceName = "DAL.Repositories")
        {
            Shared_Test_Config.Init_Webapi_Context();

            WebApiConfig.RaiseRepositoryExceptions = raiseRepositoryExceptions;

            string entityName = string.Empty;

            List<string> ExceptionList = new List<string>();

            using WebApiContext ctx = new WebApiContext();
            {
                Assert.True(ctx.Database.CanConnect());
                var typeList = TestHelper.GetAllRepositories(namespaceName);

                foreach (var item in typeList)
                {
                    try
                    {
                        if (repositoryName == "" || item.Name.Contains(repositoryName))
                        {
                            Type type = item as Type;
                            entityName = type.Name;
                            object instance = Activator.CreateInstance(type);
                            MethodInfo takeListMethod = type.GetMethod("TakeList");
                            dynamic list = takeListMethod.Invoke(instance, new object[] { count, true });
                            System.Diagnostics.Debug.WriteLine(entityName);
                            Assert.NotNull(list);
                            Assert.Equal(count, list.Count);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        ExceptionList.Add($"Entity: {entityName} " + ex.Message + " " + ex.InnerException);
                    }
                }
            }
            var errList = "";
            foreach (var item in ExceptionList) errList += "\r\n -- " + item;
            if (ExceptionList.Count > 0) throw new Exception(errList);
            return true;
        }
    }
}