using AutoMapper;
using Azure;
using Countries.NET;
using Microsoft.AspNetCore.Identity;
using MTWorkHR.Application.Models;
using MTWorkHR.Core.Entities;
using MTWorkHR.Core.Entities.User;
using MTWorkHR.Core.Global;
using MTWorkHR.Infrastructure.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTWorkHR.Application.Mapper
{
    public class MappingProfile : Profile
    {
        public MappingProfile() {

            //identity user

            CreateMap<UserDto, ApplicationUser>()
                .ForMember(m => m.UserRoles, op => op.Ignore())
               // .ForMember(m => m.UserAttachments, op => op.Ignore())
               // .ForMember(m => m.UserAddress, op => op.Ignore())
                .ForMember(m => m.Id, op => op.Ignore()
                );

            CreateMap<ApplicationUser, UserDto>().ForMember(m => m.Password, op => op.Ignore())
              .ForMember(s => s.QualificationName, o => o.MapFrom(s => s.Qualification == null ? "" : GlobalInfo.lang == "ar" ? s.Qualification.NameAr : s.Qualification.NameEn))
              .ForMember(s => s.JobTitleName, o => o.MapFrom(s => s.JobTitle == null ? "" : GlobalInfo.lang == "ar" ? s.JobTitle.NameAr : s.JobTitle.NameEn))
              .ForMember(s => s.IndustryName, o => o.MapFrom(s => s.Industry == null ? "" : GlobalInfo.lang == "ar" ? s.Industry.NameAr : s.Industry.NameEn))
              .ForMember(s => s.CountryName, o => o.MapFrom(s => s.Country == null ? "" : GlobalInfo.lang == "ar" ? s.Country.NameAr : s.Country.NameEn))
              .ForMember(s => s.UniversityName, o => o.MapFrom(s => s.University == null ? "" : GlobalInfo.lang == "ar" ? s.University.NameAr : s.University.NameEn)
              );
            CreateMap<ApplicationUser, UserAllDto>()
                .ForMember(s => s.QualificationName, o => o.MapFrom(s => s.Qualification ==null ? "" : GlobalInfo.lang == "ar" ? s.Qualification.NameAr : s.Qualification.NameEn))
                .ForMember(s => s.JobTitleName, o => o.MapFrom(s => s.JobTitle ==null ? "" : GlobalInfo.lang == "ar" ? s.JobTitle.NameAr : s.JobTitle.NameEn))
                .ForMember(s => s.IndustryName, o => o.MapFrom(s => s.Industry ==null ? "" : GlobalInfo.lang == "ar" ? s.Industry.NameAr : s.Industry.NameEn))
                .ForMember(s => s.CountryName, o => o.MapFrom(s => s.Country ==null ? "" : GlobalInfo.lang == "ar" ? s.Country.NameAr : s.Country.NameEn))
                .ForMember(s => s.UniversityName, o => o.MapFrom(s => s.University == null ? "" : GlobalInfo.lang == "ar" ? s.University.NameAr : s.University.NameEn)
                );
            
            CreateMap<UserUpdateDto, ApplicationUser>()
                .ForMember(m => m.UserRoles, op => op.Ignore())
              //  .ForMember(m => m.UserAttachments, op => op.Ignore())
              //  .ForMember(m => m.UserAddress, op => op.Ignore())
                .ForMember(m => m.Id, op => op.Ignore());

            CreateMap<ApplicationUser, UserUpdateDto>().ForMember(m => m.Password, op => op.Ignore());
            CreateMap<UserDto, UserUpdateDto>().ReverseMap();
            CreateMap<UserBasicInfoDto, ApplicationUser>().ReverseMap();
            CreateMap<UserBasicInfoDto, UserDto>().ReverseMap();
            // -------------------view Employee info
            CreateMap<ApplicationUser, EmployeeInfoDto>()
              .ForMember(s => s.QualificationName, o => o.MapFrom(s => s.Qualification == null ? "" : GlobalInfo.lang == "ar" ? s.Qualification.NameAr : s.Qualification.NameEn))
              .ForMember(s => s.JobTitleName, o => o.MapFrom(s => s.JobTitle == null ? "" : GlobalInfo.lang == "ar" ? s.JobTitle.NameAr : s.JobTitle.NameEn))
              .ForMember(s => s.IndustryName, o => o.MapFrom(s => s.Industry == null ? "" : GlobalInfo.lang == "ar" ? s.Industry.NameAr : s.Industry.NameEn))
              .ForMember(s => s.UniversityName, o => o.MapFrom(s => s.University == null ? "" : GlobalInfo.lang == "ar" ? s.University.NameAr : s.University.NameEn)
              );
            CreateMap<UserAddress, EmployeeAddressDto>()
              .ForMember(s => s.CountryName, o => o.MapFrom(s => s.Country == null ? "" : GlobalInfo.lang == "ar" ? s.Country.NameAr : s.Country.NameEn))
              .ForMember(s => s.CityName, o => o.MapFrom(s => s.City == null ? "" : GlobalInfo.lang == "ar" ? s.City.NameAr : s.City.NameEn)
              );
            //--------------------------------------
            CreateMap<ApplicationUser, ChatUserDto>()
            .ForMember(s => s.UserId, o => o.MapFrom(s => s.Id )).ReverseMap();
            
            CreateMap<AttachmentDto, UserAttachment>().ReverseMap();
            CreateMap<UserAddress, UserAddressDto>()
              .ForMember(s => s.CountryName, o => o.MapFrom(s => s.Country == null ? "" : GlobalInfo.lang == "ar" ? s.Country.NameAr : s.Country.NameEn))
              .ForMember(s => s.CityName, o => o.MapFrom(s => s.City == null ? "" : GlobalInfo.lang == "ar" ? s.City.NameAr : s.City.NameEn)
              ).ReverseMap();

            //identity userRoles
            CreateMap<IdentityUserRole<string>, UserRoleDto>().ReverseMap();

            CreateMap<ApplicationRole, UserRoleDto>()
                .ForMember(m => m.RoleId, op => op.MapFrom(mp => mp.Id))
                .ForMember(m => m.RoleName, op => op.MapFrom(mp => mp.Name))
                .ReverseMap();

            //identity role
            CreateMap<RoleDto, ApplicationRole>()
                .ForMember(m => m.Id, op => op.Ignore())
                .ForMember(m => m.RolePermissions, op => op.Ignore())
                ;
            CreateMap<ApplicationRole, RoleDto>();

            CreateMap<RolePermissionDto, RolePermission>().ReverseMap();

            CreateMap<ProjectDto, Project>().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore());
            CreateMap<UserTaskDto, UserTask>().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore()).ReverseMap();
            CreateMap<UserTaskAttachment, AttachmentDto>().ForMember(d => d.CreateDate, o => o.Ignore()).ReverseMap();
            CreateMap<UserTaskHistoryDto, UserTaskHistory>().ForMember(d => d.CreateDate, o => o.Ignore()).ReverseMap();

            CreateMap<UserTask, UserTaskAllDto>()
            .ForMember(s => s.AttachmentCount, o => o.MapFrom(s => s.TaskAttachments == null ? 0 : s.TaskAttachments.Count))
            .ForMember(s => s.CommentsCount, o => o.MapFrom(s => s.TaskHistories== null ? 0 : s.TaskHistories.Count));


            CreateMap<Team, TeamDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore());
            CreateMap<TeamUserDto, TeamUser>().ForMember(d => d.CreateDate, o => o.Ignore()).ReverseMap();
            CreateMap<Team, TeamAllDto>()
             .ForMember(s => s.TeamUserCount, o => o.MapFrom(s => s.TeamUsers == null ? 0 :  s.TeamUsers.Count));
            CreateMap<Project, ProjectDto>()
            .ForMember(s => s.ProjectTeamIds, o => o.MapFrom(s => s.ProjectTeams != null ? s.ProjectTeams.Select(u => u.TeamId).ToList() : new List<long>()));


            CreateMap<Meeting, MeetingDto>();
            CreateMap<MeetingDto , Meeting>()
                .ForMember(d => d.CreateDate, o => o.Ignore())
                .ForMember(d => d.CreateUser, o => o.Ignore());
            CreateMap<MeetingUser, MeetingUserDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore());
            CreateMap<ProjectTeam, ProjectTeamDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore());

            CreateMap<Attendance, AttendanceDto>().ForMember(d=> d.WeekDay , o=> o.MapFrom(s=> s.AttendanceDate.DayOfWeek))
                .ForMember(d => d.TotalHours, o => o.MapFrom(s =>(s.CheckInTime.HasValue && s.CheckOutTime.HasValue) ? (s.CheckOutTime.Value - s.CheckInTime.Value).TotalHours:0));

            CreateMap<AttendanceDto, Attendance>().ForMember(d => d.CreateDate, o => o.Ignore());
            CreateMap<OrderAttachmentDto, OrderAttachment>().ReverseMap()
                .ForMember(d => d.CreateDateStr, o => o.Ignore());
            CreateMap<OverTimeDayAttachment, OrderAttachmentDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore());
            CreateMap<BusinessTripAttachment, OrderAttachmentDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore());
            
            CreateMap<OrderAllocation, OrderAllocationDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore());

            CreateMap<OrderRequestDto, OrderRequest>().ForMember(d => d.CreateDate, o => o.Ignore())
                .ForMember(d => d.OrderAttachments, opt => opt.MapFrom(src => src.OrderAttachments ?? new List<OrderAttachmentDto>()));
            CreateMap<OrderRequest, OrderRequestDto>();
            
            CreateMap<OrderRequest, OrderRequestHRDto>().ForMember(d => d.LeaveTypeName, o=> o.MapFrom(s=> s.LeaveType != null ? GlobalInfo.lang == "ar" ? s.LeaveType.NameAr: s.LeaveType.NameEn:""))
                .ForMember(d => d.OrderTypeName, o => o.MapFrom(s => s.OrderType != null ? GlobalInfo.lang == "ar" ? s.OrderType.NameAr : s.OrderType.NameEn:""));
            
            CreateMap<OverTimeDay, OverTimeDayDto>().ReverseMap();
            CreateMap<BusinessTripDto, OrderRequest>().ReverseMap();
            CreateMap<OverTimeDto, OrderRequest>().ReverseMap();
            CreateMap<ServiceCertificateRequestDto, OrderRequest>().ReverseMap();
            CreateMap<BusinessTripExpensesDto, BusinessTripExpenses>().ReverseMap();
            CreateMap<ServiceCertificate, ServiceCertificateDto>().ReverseMap();


            CreateMap<OrderType, OrderTypeDto>().ReverseMap();
            CreateMap<LeaveType, LeaveTypeDto>().ReverseMap();
            CreateMap<CompanyDto, Company>().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore());
            CreateMap<Company, CompanyDto>()
                .ForMember(s => s.CityName, o => o.MapFrom(s => s.City == null ? "" : GlobalInfo.lang == "ar" ? s.City.NameAr : s.City.NameEn))
                .ForMember(s => s.CountryName, o => o.MapFrom(s => s.Country == null ? "" : GlobalInfo.lang == "ar" ? s.Country.NameAr : s.Country.NameEn));
            CreateMap<CountryLookup, CountryDto>().ReverseMap();
            CreateMap<Industry, IndustryDto>().ReverseMap();
            CreateMap<JobTitle, JobTitleDto>().ReverseMap();
            CreateMap<University, UniversityDto>().ReverseMap();
            CreateMap<Qualification, QualificationDto>().ReverseMap();
            CreateMap<City, CityDto>().ReverseMap();
            CreateMap<UserDto, CompanyUserDto>();
            CreateMap<CompanyUserDto, UserDto>().ForMember(m => m.UserName, o => o.MapFrom(s => s.Email)).ReverseMap();
            CreateMap<CompanyUserDto, UserUpdateDto>().ReverseMap();


            CreateMap<CompanyUserDto, ApplicationUser>()
                .ForMember(m => m.UserRoles, op => op.Ignore())
                .ForMember(m => m.Id, op => op.Ignore());

            CreateMap<CompanyUserDto, ApplicationUser>().ForMember(m => m.UserName, o => o.MapFrom(s => s.Email));
            CreateMap<ApplicationUser, CompanyUserDto>().ForMember(m => m.Password, op => op.Ignore());
            CreateMap<UserTaskStatus, UserTaskStatusDto>().ReverseMap();
            CreateMap<ChatMessageDto, ChatMessage>().ReverseMap();
            
            //-----------Contract----
            CreateMap<Invoice, InvoiceDto>().ReverseMap();
            CreateMap<Contract, ContractDto>().ForMember(d => d.WorkingDays, o => o.Ignore());
            CreateMap<Contract, ContractHRDto>().ForMember(d => d.WorkingDays, o => o.Ignore());
            CreateMap<Contract, ContractAllHRDto>();
            
            CreateMap<ContractDto, Contract>()
                .ForMember(d => d.CreateDate, o => o.Ignore())
                .ForMember(d => d.CreateUser, o => o.Ignore());
            CreateMap<ContractAllowance, ContractAllowanceDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore()); ;
            CreateMap<ContractTask, ContractTaskDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore()); ;
            //CreateMap<ContractWorkingDay, ContractWorkingDayDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore()); ;
            CreateMap<ContractTaskAttachment, ContractTaskAttachmentDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore()); ;
            CreateMap<ProjectStage, ProjectStageDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore()); ;
            CreateMap<ProjectStageAttachment, ProjectStageAttachmentDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore()); ;
            CreateMap<SubscriptionConfiguration, SubscriptionConfigurationDto>().ReverseMap().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore()); ;

            CreateMap<ContractDetail, Contract>();
            CreateMap<Contract, ContractDetail>();
            
            
            CreateMap<JobInterview, JobInterviewDto>().ReverseMap();
            //.ForMember(s => s.ContractDuration, o => o.MapFrom(s => s.ContractDurationId == null ? "" : ((ContractDurationEnum)s.ContractDurationId).ToString()));
            //.ForMember(s => s.WorkCountry, o => o.MapFrom(s => s.WorkCountryId == null ? "" : s.WorkCountryId));
        }
    }
}
