using AutoMapper;
using Qusion.Cms.Classifications;
using Qusion.Cms.Classifications.Core;

namespace Qusion.WebApp.Areas.Qusion.Models.Classifications
{
    public class LabelProfile : Profile
    {
        private readonly ClassificationService _classificationService;
        private readonly LabelService _labelService;

        public LabelProfile(ClassificationService classificationService, LabelService labelService)
        {
            _classificationService = classificationService;
            _labelService = labelService;
        }

        protected override void Configure()
        {
            Mapper.CreateMap<Label, LabelModel>();
            Mapper.CreateMap<Label, LabelListModel>();
            Mapper.CreateMap<Label, LabelSimpleModel>();
            Mapper.CreateMap<LabelCreateInput, Label>()
                .ForMember(x => x.Parent, o => o.MapFrom(x => x.ParentID == null ? null : _labelService.GetById(x.ParentID)))
                .ForMember(x => x.Classification, y => y.ResolveUsing(o => _classificationService.GetById(o.ClassificationID)));
            
            Mapper.CreateMap<Label, LabelEditInput>();
            Mapper.CreateMap<LabelEditInput, Label>();
            Mapper.CreateMap<LabelQueryInput, LabelQuery>()
                .ConstructUsing(x => LabelQuery.Default);

            Mapper.CreateMap<Label, LabelSimpleListModel>()
                .ForMember(x => x.Classification, o => o.MapFrom(x => x.Classification.Alias));
        }
    }
}