using Hotline.FlowEngine.Definitions;
using Hotline.FlowEngine.Workflows;
using Hotline.Orders;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Dtos.FlowEngine.Definition;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Tools;
using Mapster;

namespace Hotline.Application.Mappers;

public class WorkflowMapperConfigs : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.ForType<UpdateDefinitionDto, WorkflowDefinition>()
            .Ignore(d => d.Id);

        config.ForType<WorkflowStep, WorkflowStep>()
            .Ignore(d => d.Id)
            .Ignore(d => d.PrevStepId)
            .Ignore(d => d.IsMain)
            .Ignore(d => d.Status)
            //.Ignore(d => d.ParentId)
            .Ignore(d => d.Handlers)
            //.Ignore(d => d.Steps)
            .Ignore(d => d.StartCountersignId)
            .Ignore(d => d.CountersignId)
            .Ignore(d => d.IsStartedCountersignEnd)
            .Ignore(d => d.IsOrigin)

            .Ignore(d => d.NextHandlers)
            .Ignore(d => d.NextMainHandler)
            .Ignore(d => d.NextStepCode)
            .Ignore(d => d.BackToCountersignEnd)
            .Ignore(d => d.IsSms)
            .Ignore(d => d.Opinion)
            .Ignore(d => d.FileJson)
            .Ignore(d => d.StepExpiredTime)
            .Ignore(d => d.Workflow)
            .Ignore(d => d.WorkflowTrace)
            //.IgnoreIf((d, s) => s.StepHandlers == null || !s.StepHandlers.Any(), d => d.StepHandlers)
            ;

        config.ForType<WorkflowStep, WorkflowTrace>()
            .Ignore(d=>d.Workflow)
            .Ignore(d=>d.OrderPublish)
            .Ignore(d=>d.OrderVisit)
            .Ignore(d=>d.Traces)
            .Ignore(d => d.ParentId)
            .Ignore(d => d.TraceType)
            .Map(d => d.StepId, s => s.Id)
            .AfterMapping((s, d) => d.Id = s.Id)
            ;

        config.ForType<WorkflowTrace, WorkflowTrace>()
            .Ignore(d=>d.Id)
            .Ignore(d => d.Workflow)
            .Ignore(d => d.OrderPublish)
            .Ignore(d => d.OrderVisit)
            .Ignore(d => d.Traces)
            .Ignore(d => d.ParentId)
            .Ignore(d => d.TraceType)
            ;

        config.ForType<Workflow, WorkflowDto>()
            //.IgnoreIf((s, d) => s.Assigns == null || !s.Assigns.Any(), d => d.AssignOrgs)
            //.IgnoreIf((s, d) => s.Supplements == null || !s.Supplements.Any(), d => d.Supplements)
            .IgnoreIf((s, d) => s.Traces == null || !s.Traces.Any(), d => d.Traces)
            //.IgnoreIf((s, d) => s.StepBoxes == null || !s.StepBoxes.Any(), d => d.StepBoxes)
            //.IgnoreIf((s, d) => s.Definition == null, d => d.Definition)
            //.Map(d => d.AssignOrgs, s => string.Join(',', s.Assigns.Select(d => d.OrgName)))
            .Ignore(d => d.Definition)
            .Ignore(d => d.StepBoxes)
            //.Ignore(d=>d.Supplements)
            //.Ignore(d => d.Traces)
            ;

        config.ForType<Workflow, WorkflowStep>()
            .Map(d => d.WorkflowId, s => s.Id)
            .Map(d => d.ExternalId, s => s.ExternalId)
            .Map(d => d.ModuleId, s => s.ModuleId)
            .Map(d => d.ModuleCode, s => s.ModuleCode)
            .Map(d => d.ModuleName, s => s.ModuleName)
            .IgnoreNonMapped(true)
            ;

        config.ForType<WorkflowTrace, WorkflowTraceDto>()
            .Map(d => d.Handler, s => s.GetHandler().Value)
            .IgnoreIf((s, d) => s.Status != EWorkflowStepStatus.Handled, d => d.HandlerName)
            ;
    }
}