using AutoMapper;
using AutoMapper.Internal.Mappers;
using Entities.Contracts;
using Entities.DTO;
using Newtonsoft.Json;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace TechHelper.Context
{
	public class AutoMapperProFile : Profile
	{

		public static class EnumMappingHelpers
		{
			public static TEnum ParseEnumSafe<TEnum>(string sourceString, TEnum defaultValue) where TEnum : struct, Enum
			{
				if (Enum.TryParse(sourceString, true, out TEnum parsedValue))
				{
					return parsedValue;
				}
				return defaultValue;
			}
		}

		public AutoMapperProFile()
		{
			CreateMap<UserForRegistrationDto, User>()
				.ForMember(dest => dest.Id, opt => opt.Ignore())
				.ForMember(dest => dest.UserName, opt => opt.MapFrom(src => src.Name))
				.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email))
				.ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.PhoneNumber))
				.ForMember(dest => dest.Address, opt => opt.MapFrom(src => src.HomeAddress))
				.ForMember(dest => dest.PasswordHash, opt => opt.Ignore())
				.ForMember(dest => dest.EmailConfirmed, opt => opt.Ignore());

			CreateMap<ClassDto, Class>()
				.ForMember(d => d.Number, o => o.MapFrom(src => src.Class))
				.ReverseMap();



			// Assignment 
			CreateMap<AssignmentDto, Assignment>().ReverseMap();

			CreateMap<AssignmentQuestionDto, AssignmentQuestion>().ReverseMap();

			CreateMap<QuestionDto, Question>().ReverseMap();

			CreateMap<QuestionContext, QuestionContextDto>().ReverseMap();




			// Submission
			CreateMap<SubmissionDto, Submission>().ReverseMap();

			CreateMap<SubmissionDetailDto, SubmissionDetail>().ReverseMap();


			CreateMap<SubjectTypeMetadataDto, Global>()
					   .ForMember(dest => dest.Info, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Data)));
			CreateMap<Global, SubjectTypeMetadataDto>()
					   .ForMember(dest => dest.Data, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<Dictionary<string, (string Color, string DisplayName)>>(src.Info)));

		}
	}

}
