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 DepartmentToWebAppToEmployeeForWindreamRepository : BaseRepository<DepartmentToWebAppToEmployeeForWindream>
    {
        public DepartmentToWebAppToEmployeeForWindreamRepository(WebApiContext context) : base(context)
        {
        }

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

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

            if (!string.IsNullOrEmpty(filter.ShortName))
            {
                items = items.Where(x => x.ShortName == filter.ShortName);
            }

            if (!string.IsNullOrEmpty(filter.LoginName))
            {
                items = items.Where(x => x.LoginName.ToLower() == filter.LoginName.ToLower());
            }

            return asNoTracking ? await items.OrderBy(x => x.DepartmentName).AsNoTracking().ToListAsync() : await items.OrderBy(x => x.DepartmentName).ToListAsync();
        }
    }
}