using Hotline.FlowEngine.Workflows;
using Hotline.Orders;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.Settings;
using Mapster;

namespace Hotline.Application.Mappers;

public class OrderMapperConfigs : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.ForType<Order, OrderDto>()
            .IgnoreIf((s, d) => s.OrderExtension == null, d => d.OrderExtension)
            .IgnoreIf((s, d) => s.Hotspot == null, d => d.Hotspot)
            ;

        config.ForType<AddOrderDto, Order>()
            .IgnoreIf((s, d) => s.OrderExtension == null, d => d.OrderExtension)
            .Map(d => d.Additions, s => s.Files)
            .IgnoreIf((s, d) => !s.ExpiredTime.HasValue, d => d.ExpiredTime)
            .IgnoreIf((s, d) => !s.NearlyExpiredTime.HasValue, d => d.NearlyExpiredTime)
            .IgnoreIf((s, d) => string.IsNullOrEmpty(s.TimeLimit), d => d.TimeLimit)
            .IgnoreIf((s, d) => !s.TimeLimitCount.HasValue, d => d.TimeLimitCount)
            .IgnoreIf((s, d) => !s.TimeLimitUnit.HasValue, d => d.TimeLimitUnit)
            ;

        config.ForType<UpdateOrderDto, Order>()
            .Inherits<AddOrderDto, Order>()
            .Map(d => d.OrderExtension.Id, x => x.Id);

        config.ForType<Workflow, Order>()
            .Map(d => d.WorkflowId, s => s.Id)
            .Map(d => d.StartTime, s => s.CreationTime)
            .Ignore(d => d.Status)
            .Ignore(d => d.Title)
            .Ignore(d => d.ExternalId)
            .AfterMapping((s, d) =>
            {
                d.UpdateHandlingStatus(s.IsInCountersign);
            })
            ;

        config.ForType<Order, PublishDto>()
            .Inherits<Order, OrderDto>()
            .IgnoreIf((s, d) => s.OrderPublish == null, d => d.PublishState)
            .IgnoreIf((s, d) => s.OrderPublish == null, d => d.ArrangeContent)
            .IgnoreIf((s, d) => s.OrderPublish == null, d => d.PublishTime)
            .IgnoreIf((s, d) => s.OrderPublish == null, d => d.PublishMan)
            .Map(d => d.PublishState, s => s.OrderPublish.PublishState)
            .Map(d => d.ArrangeContent, s => s.OrderPublish.ArrangeContent)
            .Map(d => d.PublishTime, s => s.OrderPublish.CreationTime)
            .Map(d => d.PublishMan, s => s.OrderPublish.CreatorName);


        config.ForType<OrderPublish, PublishedDto>()

            .Map(d => d.PublishState, s => s.PublishState)
            .Map(d => d.ArrangeContent, s => s.ArrangeContent)
            .Map(d => d.PublishTime, s => s.CreationTime)
            .Map(d => d.PublishMan, s => s.CreatorName);

        config.ForType<Order, OrderWaitedDto>()
            .Inherits<Order, OrderDto>();

        config.ForType<OrderVisit, OrderVisitDto>()
            .IgnoreIf((s, d) => s.Employee == null, d => d.EmployeeName)
            .Map(d => d.EmployeeName, s => s.Employee.Name);

        config.ForType<VisitDetailDto, OrderVisitDetail>()
            .Map(d => d.VoiceEvaluate, s => s.VoiceEvaluate)
            .Map(d => d.SeatEvaluate, s => s.SeatEvaluate);

        config.ForType<OrderDelay, OrderDelayDto>()
            //.Inherits<Order, OrderDto>()
            .Map(d => d.CurrentStepName, s => s.Workflow.ActualHandleStepName)
            .Map(d => d.ActualHandlerName, s => s.Workflow.ActualHandlerName)
            .IgnoreIf((s, d) => s.Workflow == null, d => d.CurrentStepName)
            .IgnoreIf((s, d) => s.Workflow == null, d => d.ActualHandlerName);

        config.ForType<ExpiredTimeWithConfig, UpdateOrderDto>()
            .Map(d => d.TimeLimit, s => s.TimeText)
            .Map(d => d.TimeLimitCount, s => s.Count)
            .Map(d => d.TimeLimitUnit, s => s.TimeType);

        config.ForType<ExpiredTimeWithConfig, Order>()
            .Map(d => d.TimeLimit, s => s.TimeText)
            .Map(d => d.TimeLimitCount, s => s.Count)
            .Map(d => d.TimeLimitUnit, s => s.TimeType);
    }
}