using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.Configuration.Conventions;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.DL.Entities;

namespace WorkFlowCheck.BL.Mappings
{
    public class MapperProfile : Profile
    {
        public MapperProfile()
        {
            CreateMap<User, UserDTO>().ForMember(dest => dest.RoleDTO, opt => opt.MapFrom(src => src.UserRoles.Select(ur => ur.Role)));

            CreateMap<Role, RoleDTO>();

            CreateMap<UserRole, UserRoleDTO>()
                .ForMember(dest => dest.RoleDTO, opt => opt.MapFrom(src => src.Role))
                .ForMember(dest => dest.UserDTO, opt => opt.MapFrom(src => src.User));

            CreateMap<UserDTO, User>()
                  .ForMember(dest => dest.PasswordHash, opt => opt.MapFrom<PasswordHashResolver>());


            CreateMap<CheckListTemplateHeader, CheckListTemplateHeaderDTO>()
                .ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRows));

            CreateMap<CheckListTemplateRow, CheckListTemplateRowDTO>()
                .ForMember(dest => dest.EquipmentDTO, opt => opt.MapFrom(src => src.Equipment))
                .ForMember(dest => dest.CheckPointDTO, opt => opt.MapFrom(src => src.CheckPoint));

            CreateMap<CheckPoint, CheckPointDTO>().ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRows));

            CreateMap<CheckPointDTO, CheckPoint>();

            CreateMap<Location, LocationDTO>();
            CreateMap<LocationDTO, Location>();

            CreateMap<Equipment, EquipmentDTO>();
            CreateMap<EquipmentDTO, Equipment>();

            CreateMap<CheckListHeader, CheckListHeaderDTO>()
                .ForMember(dest => dest.UserDTO, opt => opt.MapFrom(src => src.User))
                .ForMember(dest => dest.CheckListRowDTO, opt => opt.MapFrom(src => src.CheckListRows));

            CreateMap<CheckListRow, CheckListRowDTO>()
                .ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRow))
                .ForMember(dest => dest.Photo, opt => opt.MapFrom(src => GetImageBytes(src.PhotoFileName)));


            CreateMap<RoleCheckListTemplateHeader, RoleCheckListTemplateHeaderDTO>()
                .ForMember(dest => dest.RoleDTO, opt => opt.MapFrom(src => src.Role))
                .ForMember(dest => dest.CheckListTemplateHeaderDTO, opt => opt.MapFrom(src => src.CheckListTemplateHeader));
        }
        private byte[] GetImageBytes(string fileName)
        {
            try
            {
                var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Images", fileName);
                if (File.Exists(filePath))
                {
                    return File.ReadAllBytes(filePath);
                }
                return Array.Empty<byte>(); // Visszaad egy üres byte[]-ot, ha a fájl nem található
            }
            catch (Exception)
            {
                return Array.Empty<byte>(); // Hiba esetén is üres byte[]-ot ad vissza
            }
        }
    }
}
