using Hotline.KnowledgeBase;
using Hotline.Share.Dtos.Knowledge;
using Mapster;

namespace Hotline.Application.Mappers;

public class KnowledgeMapperConfigs : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        #region Knowledge
        config.NewConfig<KnowledgeApply, KnowledgeApplyHandlePageDto>()
            .Map(d => d.CreationName, x => x.User.Name)
            .Map(d => d.CreationOrgName, x => x.SystemOrganize.Name);

        config.NewConfig<Hotline.KnowledgeBase.Knowledge, KnowledgeDataDto>()
            .IgnoreIf((s, d) => s.HotspotType == null, d => d.HotspotType)
            .IgnoreIf((s, d) => s.HotspotType == null, d => d.HotspotName)
            .Map(d => d.CreatorName, x => x.CreatorName)
            .Map(d => d.HotspotName, x => x.HotspotType.HotSpotFullName)
            .AfterMapping((s, d) => d.KnowledgeTypeText = s.KnowledgeTypes != null && s.KnowledgeTypes.Any()
                ? string.Join(',', s.KnowledgeTypes.Select(x => x.Name))
                : string.Empty);
        ;

        config.ForType<KnowledgeBase.Knowledge, KnowledgeDto>()
            .Map(d=>d.HotspotName,s=>s.HotspotType.HotSpotFullName)
            .IgnoreIf((s, d) => s.HotspotType == null, d => d.HotspotName)
            .IgnoreIf((s, d) => s.SourceOrganize == null, d => d.SourceOrganize)
            .Ignore(d => d.Collect)
            .AfterMapping((s, d) => d.KnowledgeTypeText = s.KnowledgeTypes != null && s.KnowledgeTypes.Any()
                ? string.Join(',', s.KnowledgeTypes.Select(x => x.Name))
                : string.Empty); 

        config.ForType<KnowledgeBase.Knowledge, KnowledgeInfoDto>()
            .AfterMapping((s, d) => d.KnowledgeTypeText = s.KnowledgeTypes != null && s.KnowledgeTypes.Any()
                ? string.Join(',', s.KnowledgeTypes.Select(x => x.Name))
                : string.Empty);

        config.NewConfig<Hotline.KnowledgeBase.Knowledge, KnowledgeApprovalDataDto>()
            .Map(d => d.CreatorName, x => x.User.Name)
            .Map(d => d.CreateBMName, x => x.SystemOrganize.Name);

        config.NewConfig<KnowledgeWorkFlow, KnowledgeApprovalDataDto>()
            .Map(d => d.CreatorName, x => x.User.Name)
            .Map(d => d.CreateBMName, x => x.SystemOrganize.Name)
            .Map(d => d.Id, x => x.Knowledge.Id)
            .Map(d => d.Title, x => x.Knowledge.Title)
            .Map(d => d.PageView, x => x.Knowledge.PageView)
            .Map(d => d.Status, x => x.Knowledge.Status)
            .Map(d => d.WorkflowModuleStatus, x => x.WorkflowModuleStatus)
            .Map(d => d.CurrentStepTime, x => x.Workflow.ActualHandleTime);

        config.NewConfig<KnowledgeWorkFlow, KnowledgeDeleteApplyDataDto>()
            .Map(d => d.Id, x => x.Knowledge.Id)
            .Map(d => d.Title, x => x.Knowledge.Title)
            .Map(d => d.HotspotName, x => x.Knowledge.HotspotType.HotSpotFullName)
            .Map(d => d.CreatorName, x => x.User.Name)
            .Map(d => d.CreationBMName, x => x.SystemOrganize.Name)
            .Map(d => d.PageView, x => x.Knowledge.PageView)
            .Map(d => d.Status, x => x.Knowledge.Status)
            .Map(d => d.WorkFlowApplyStatus, x => x.WorkFlowApplyStatus);

        config.NewConfig<Hotline.KnowledgeBase.Knowledge, KnowledgeSendDto>()
            .Map(d => d.CategoryCode, x => x.HotspotType.ProvinceCode)
            .Map(d => d.CategoryName, x => x.HotspotType.HotSpotName)
            .Map(d => d.PublishPerson, x => x.CreatorName)
            .Map(d => d.OuName, x => x.CreatorOrgName)
            .Map(d => d.KName, x => x.Title)
            .Map(d => d.KContent, x => x.Content)
            .Map(d => d.CreatDate, x => x.OnShelfTime)
            .Map(d => d.BeginDate, x => x.OnShelfTime)
            .Map(d => d.DisableDate, x => x.ExpiredTime)
            .Map(d => d.UID, x => x.Code)
            .IgnoreIf((s, d) => s.FileJson == null, d => d.FileJson)
            .IgnoreIf((s,d)=>s.HotspotType == null, d=>d.CategoryCode)
            .IgnoreIf((s,d)=>s.HotspotType == null, d=>d.CategoryName)
            //.IgnoreIf((s,d)=>s.User == null, d=>d.PublishPerson)
            //.IgnoreIf((s,d)=>s.SystemOrganize == null, d=>d.OuName)
            ;

        config.ForType<KnowledgeRelationTypeDto, KnowledgeType>()
            .Map(d => d.Name, s => s.KnowledgeTypeName)
            .Map(d => d.SpliceName, s => s.KnowledgeTypeSpliceName)
            .AfterMapping((s, d) => d.Id = s.KnowledgeTypeId)
            ;

        config.ForType<KnowledgeApprove, KnowledgeApproveDto>()
            .IgnoreIf((s, d) => s.Approver == null, d => d.Approver);

        config.ForType<KnowledgeImportTemplate, KnowledgeBase.Knowledge>()
            .Ignore(d => d.IsPublic);

        #endregion
    }
}