using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MTWorkHR.Application.Identity;
using MTWorkHR.Application.Mapper;
using MTWorkHR.Application.Models;
using MTWorkHR.Core.Global;
using MTWorkHR.Core.IRepositories;
using MTWorkHR.Core.UnitOfWork;
using MTWorkHR.Infrastructure.Entities;
using MTWorkHR.Application.Services.Interfaces;
using MTWorkHR.Core.Email;
using MTWorkHR.Core.Entities;
using MTWorkHR.Infrastructure.UnitOfWorks;
using MTWorkHR.Core.IDto;
using MTWorkHR.Infrastructure.Repositories;
using MTWorkHR.Core.Entities.Base;

namespace MTWorkHR.Application.Services
{
    public class LookupService : ILookupService
    {
        private readonly IUnitOfWork _unitOfWork;

        public LookupService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<List<LeaveTypeDto>> GetAllLeaveType()
        {
            var result = await _unitOfWork.LeaveType.GetAllAsync();
            var list = MapperObject.Mapper.Map<List<LeaveTypeDto>>(result.Item1);
            return list;
        }

        public async Task<List<OrderTypeDto>> GetAllOrderType()
        {
            var entity = await _unitOfWork.OrderType.GetAllAsync();
            var response = MapperObject.Mapper.Map<List<OrderTypeDto>>(entity.Item1);
            return response;
        }
        public async Task<List<CountryDto>> GetAllCountries()
        {
            var entity = await _unitOfWork.CountryLookup.GetAllAsync();
            var response = MapperObject.Mapper.Map<List<CountryDto>>(entity.Item1);
            return response;
        }

        public async Task<List<CityDto>> GetCityByCountry(long CountryId)
        {
           // List<CityDto> city = new List<CityDto> { new CityDto { NameEn = "Riyadh", NameAr = "الرياض", CountryId=1, Id = 1 }, new CityDto { NameEn = "Giddah", CountryId = 1, NameAr = "جده", Id = 2 }};
            var cityList = await _unitOfWork.City.GetAllAsync();
            var filterList = cityList.Item1.Where(c=> c.CountryId == CountryId);
            var response = MapperObject.Mapper.Map<List<CityDto>>(filterList);
            return response;
        }
        
        public async Task<List<IndustryDto>> GetAllIndustries()
        {
            var entity = await _unitOfWork.Industry.GetAllAsync();
            var response = MapperObject.Mapper.Map<List<IndustryDto>>(entity.Item1);
            return response;
        }

        public async Task<List<JobTitleDto>> GetAllJobTitles()
        {
            var entity = await _unitOfWork.JobTitle.GetAllAsync();
            var response = MapperObject.Mapper.Map<List<JobTitleDto>>(entity.Item1);
            return response;
        }
        public async Task<List<QualificationDto>> GetAllQualifications()
        {
            var entity = await _unitOfWork.Qualification.GetAllAsync();
            var response = MapperObject.Mapper.Map<List<QualificationDto>>(entity.Item1);
            return response;
        }
        public async Task<List<UniversityDto>> GetAllUniversities()
        {
            var entity = await _unitOfWork.University.GetAllAsync();
            var response = MapperObject.Mapper.Map<List<UniversityDto>>(entity.Item1);
            return response;
        }

        public async Task<List<UserTaskStatusDto>> GetAllUserTaskStatus()
        {
            var entity = await _unitOfWork.UserTaskStatus.GetAllAsync();
            var response = MapperObject.Mapper.Map<List<UserTaskStatusDto>>(entity.Item1);
            return response;
        }

        public async Task<List<CountryDto>> CreateCountries()
        {
            // init the service
            var service = new Countries.NET.CountriesService();

            // to get the list of all countries:
            var countriesEnglish = service.GetAsKeyValue();
            var countriesArabic = service.GetAsKeyValue("ara", true);
            var countries = countriesArabic.Select(x => new CountryLookup { Code = x.Key, NameAr = x.Value }).ToList();
            foreach(var c in countries)
            {
                var countryEng = countriesEnglish.FirstOrDefault(cc => cc.Key == c.Code);
                c.NameEn = countryEng.Value;
            }
            await _unitOfWork.CountryLookup.AddRangeAsync(countries);
            await _unitOfWork.CompleteAsync();
            var response = MapperObject.Mapper.Map<List<CountryDto>>(countries);

            return response;
        }

    }
}
