using AutoMapper;
using Lada.Framework.Data.Domains.Settings;
using Ladaer.BackEnd.Infrastructures.DTO.Items.Settings;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Settings;

namespace Ladaer.BackEnd.Infrastructures.DTO.Profiles
{
    public class CallPathMappingProfile : Profile
    {
        public CallPathMappingProfile()
        {
            #region CallPath
            CreateMap<CallPath, CallPathItem>()
                .ForMember(dest => dest.Version, opt => opt.MapFrom(src => src.CallPathVersion != null ? src.CallPathVersion.Version : ""))
                .ForMember(dest => dest.Build, opt => opt.MapFrom(src => src.CallPathVersion != null ? src.CallPathVersion.Build : ""))
                .ForMember(dest => dest.Domain, opt => opt.MapFrom(src => src.CallPathVersion != null ? src.CallPathVersion.Domain : ""));

            CreateMap<CallPath, CallPathModel>()
                .ForMember(dest => dest.Version, opt => opt.MapFrom(src => src.CallPathVersion != null ? src.CallPathVersion.Version : ""))
                .ForMember(dest => dest.Build, opt => opt.MapFrom(src => src.CallPathVersion != null ? src.CallPathVersion.Build : ""))
                .ForMember(dest => dest.Domain, opt => opt.MapFrom(src => src.CallPathVersion != null ? src.CallPathVersion.Domain : ""))
                .ReverseMap();
            #endregion

            #region CallPathVersion
            CreateMap<CallPathVersion, CallPathVersionItem>()
                .ForMember(dest => dest.CallPathCount, opt => opt.MapFrom(src => src.CallPaths != null ? src.CallPaths.Count : 0));

            CreateMap<CallPathVersion, CallPathVersionModel>()
                .ReverseMap();
            #endregion
        }
    }

    public static class CallPathMappingExtensions
    {
        public static CallPathItem ToItem(this CallPath entity)
            => entity.MapTo<CallPath, CallPathItem>();

        public static CallPathModel ToModel(this CallPath entity)
            => entity.MapTo<CallPath, CallPathModel>();

        public static CallPath ToEntity(this CallPathModel model)
            => model.MapTo<CallPathModel, CallPath>();

        public static CallPathVersionItem ToItem(this CallPathVersion entity)
            => entity.MapTo<CallPathVersion, CallPathVersionItem>();

        public static CallPathVersionModel ToModel(this CallPathVersion entity)
            => entity.MapTo<CallPathVersion, CallPathVersionModel>();

        public static CallPathVersion ToEntity(this CallPathVersionModel model)
            => model.MapTo<CallPathVersionModel, CallPathVersion>();
    }
}
