using AutoMapper;
using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Domains.Crm;
using Lada.Framework.Data.Domains.Hr;
using ladaexpress.vn.Infrastructures.DTO.Models.Categories;
using ladaexpress.vn.Infrastructures.DTO.Models.Hr;
using ladaexpress.vn.Infrastructures.DTO.Models.Post.Detail;
using ladaexpress.vn.Infrastructures.DTO.Models.Post.Index.Childs;
using ladaexpress.vn.Infrastructures.DTO.Models.Product.Index.Childs;
using ladaexpress.vn.Infrastructures.DTO.Models.Products.Detail;
using ladaexpress.vn.Infrastructures.DTO.Models.Service;
using ladaexpress.vn.Infrastructures.DTO.Models.Tags;
using ladaexpress.vn.Infrastructures.DTO.Models.Topic;
using ladaexpress.vn.Infrastructures.Helpers;

namespace ladaexpress.vn.Infrastructures.DTO
{
    public class AutoMapperConfig
    {
        #region Properties
        private static MapperConfiguration _mapperConfiguration;
        private static IMapper _mapper;

        /// <summary>
        /// Mapper
        /// </summary>
        public static IMapper Mapper => _mapper;

        /// <summary>
        /// Mapper configuration
        /// </summary>
        public static MapperConfiguration MapperConfiguration => _mapperConfiguration;
        #endregion

        /// <summary>
        /// Init
        /// </summary>
        public static void Init()
        {
            try
            {
                _mapperConfiguration = new MapperConfiguration(cfg =>
                {

                    #region CMS

                    #region Tags
                    cfg.CreateMap<Tag, TagDetailModel>();
                    cfg.CreateMap<Tag, TagItem>();
                    #endregion

                    #region Category
                    cfg.CreateMap<Category, CategoryDetailModel>();
                    #endregion

                    #region Post
                    cfg.CreateMap<Post, PostItem>()
                    .ForMember(dest => dest.Slug, opt => opt.MapFrom(src => src.UrlRecord != null && src.UrlRecord.Id > 0 ? src.UrlRecord.Slug : ""))
                    .ForMember(dest => dest.ReadingTime, opt => opt.MapFrom(src => !string.IsNullOrEmpty(src.Description) ? src.Description.EstimateReadTime() : 0))
                    .ForMember(dest => dest.PublishDateStr, opt => opt.MapFrom(src => src.ValidFrom.HasValue ? src.ValidFrom.Value.ToString("dd/MM/yyyy HH:mm") : ""));
                    cfg.CreateMap<Post, PostDetailModel>()
                     .ForMember(dest => dest.Slug, opt => opt.MapFrom(src => src.UrlRecord != null && src.UrlRecord.Id > 0 ? src.UrlRecord.Slug : ""))
                    .ForMember(dest => dest.ReadingTime, opt => opt.MapFrom(src => !string.IsNullOrEmpty(src.Description) ? src.Description.EstimateReadTime() : 0))
                     .ForMember(dest => dest.PublishDate, opt => opt.MapFrom(src => src.ValidFrom.HasValue ? src.ValidFrom.Value.ToString("dd/MM/yyyy HH:mm") : ""));

                    cfg.CreateMap<Tag, PostTagItem>();

                    cfg.CreateMap<Category, PostIndexCategory>()
                        .ForMember(dest => dest.Slug, opt => opt.MapFrom(src => src.UrlRecord != null && src.UrlRecord.Id > 0 ? src.UrlRecord.Slug : ""));

                    #endregion

                    #region Product
                    cfg.CreateMap<Product, ProductItem>()
                    .ForMember(dest => dest.Slug, opt => opt.MapFrom(src => src.UrlRecord != null && src.UrlRecord.Id > 0 ? src.UrlRecord.Slug : ""))
                    .ForMember(dest => dest.ReadingTime, opt => opt.MapFrom(src => !string.IsNullOrEmpty(src.Description) ? src.Description.EstimateReadTime() : 0));
                    cfg.CreateMap<Product, ProductDetailModel>()
                     .ForMember(dest => dest.Slug, opt => opt.MapFrom(src => src.UrlRecord != null && src.UrlRecord.Id > 0 ? src.UrlRecord.Slug : ""))
                    .ForMember(dest => dest.ReadingTime, opt => opt.MapFrom(src => !string.IsNullOrEmpty(src.Description) ? src.Description.EstimateReadTime() : 0));

                    cfg.CreateMap<Tag, ProductTagItem>();

                    #endregion


                    #region Topic

                    cfg.CreateMap<Topic, TopicDetailModel>()
                     .ForMember(dest => dest.Slug, opt => opt.MapFrom(src => src.UrlRecord != null && src.UrlRecord.Id > 0 ? src.UrlRecord.Slug : ""))
                    .ForMember(dest => dest.ReadingTime, opt => opt.MapFrom(src => !string.IsNullOrEmpty(src.Description) ? src.Description.EstimateReadTime() : 0));


                    #endregion

                    #endregion

                    #region HR
                    #region JobPost
                    cfg.CreateMap<JobPost, JobDetailModel>();
                    #endregion

                    #region Candidate
                    cfg.CreateMap<ApplyModel, Candidate>()
                    .ForMember(dest => dest.JobId, opt => opt.MapFrom(src => src.JobPostId));
                    #endregion

                    #endregion

                    #region CRM

                    #region Lead
                    cfg.CreateMap<Lead, LeadModel>();
                    cfg.CreateMap<LeadModel, Lead>();
                    #endregion

                    #endregion

                });

                _mapper = _mapperConfiguration.CreateMapper();
            }
            catch (System.Exception)
            {

                throw;
            }

        }
    }
}
