using DAL.Models.Entities;
using DAL.Models.Filters;
using HRD.WebApi.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DAL.Repositories
{
    public class EmployeeRepository : BaseRepository<Employee>
    {
        public EmployeeRepository(WebApiContext context) : base(context)
        {
        }

        public async Task<List<Employee>> GetListByFilterAsync(EmployeeFullFilter filter, bool asNoTracking = true)
        {
            var items = this.RepositoryContext.Set<Employee>().AsQueryable();

            if (filter.EmployeeId != null && filter.EmployeeId != 0)
            {
                items = items.Where(x => x.EmployeeId == filter.EmployeeId);
                return asNoTracking ? await items.AsNoTracking().ToListAsync() : await items.ToListAsync();
            }

            if (!string.IsNullOrEmpty(filter.Name))
            {
                items = items.Where(x => EF.Functions.Like(x.FirstName.ToLower() + ' ' + x.LastName.ToLower(), $"%{filter.Name.ToLower()}%"));
            }

            if (!string.IsNullOrEmpty(filter.Email))
            {
                items = items.Where(x => EF.Functions.Like(x.Email.ToLower(), $"%{filter.Email.ToLower()}%"));
            }

            if (!string.IsNullOrEmpty(filter.ShortName))
            {
                items = items.Where(x => EF.Functions.Like(x.ShortName.ToLower(), $"%{filter.ShortName.ToLower()}%"));
            }

            if (!string.IsNullOrEmpty(filter.LoginName))
            {
                items = items.Where(x => EF.Functions.Like(x.LoginName.ToLower(), $"%{filter.LoginName.ToLower()}%"));
            }

            if (filter.DepartmentIds != null && filter.DepartmentIds.Length > 0)
            {
                IQueryable<Employee> itemsTmp = null;
                for (int i = 0; i < filter.DepartmentIds.Length; i++)
                {
                    var inx = filter.DepartmentIds[i];
                    var items_ = items.Where(x => EF.Functions.Like("," + x.DepartmentIdList.Replace(" ", "") + ",", $"%,{inx},%"));
                    itemsTmp = itemsTmp != null ? itemsTmp.Union(items_) : items_;
                }
                items = itemsTmp;
            }

            if (filter.WebappIds != null && filter.WebappIds.Length > 0)
            {
                IQueryable<Employee> itemsTmp = null;
                for (int i = 0; i < filter.WebappIds.Length; i++)
                {
                    var inx = filter.WebappIds[i];
                    var items_ = items.Where(x => EF.Functions.Like("," + x.WebappIdList.Replace(" ", "") + ",", $"%,{inx},%"));
                    itemsTmp = (itemsTmp != null) ? itemsTmp.Union(items_) : items_;
                }
                items = itemsTmp;
            }

            if (filter.AttributeIds != null && filter.AttributeIds.Length > 0)
            {
                IQueryable<Employee> itemsTmp = null;
                for (int i = 0; i < filter.AttributeIds.Length; i++)
                {
                    var inx = filter.AttributeIds[i];
                    var items_ = items.Where(x => EF.Functions.Like("," + x.AttributeIdList.Replace(" ", "") + ",", $"%,{inx},%"));
                    itemsTmp = (itemsTmp != null) ? itemsTmp.Concat(items_) : items_;
                }
                items = itemsTmp;
            }

            if (filter.ClientId != null && filter.ClientId != 0)
            {
                items = items.Where(x => x.ClientId == filter.ClientId);
            }

            if (filter.IsActive != null)
            {
                items = items.Where(x => x.IsActive == filter.IsActive);
            }

            return asNoTracking ? await items.AsNoTracking().ToListAsync() : await items.ToListAsync();
        }
    }
}