using Hotline.FlowEngine.Workflows;
using Hotline.Orders;
using Hotline.Share.Dtos.Ai;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.Settings;
using Hotline.Share.Enums.Order;
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<AiOrderVisitDetail, AiOrderVisitDetailDto>()
            .IgnoreIf((s, d) => s.Order == null, d => d.No)
            .IgnoreIf((s, d) => s.Order == null, d => d.Title)
            .IgnoreIf((s, d) => s.Order == null, d => d.FromName)
            .IgnoreIf((s, d) => s.Order == null, d => d.FromGender)
            .IgnoreIf((s, d) => s.Order == null, d => d.StartTime)
            .IgnoreIf((s, d) => s.Order == null, d => d.FiledTime)
            .Map(d => d.No, s => s.Order.No)
            .Map(d => d.Title, s => s.Order.Title)
            .Map(d => d.FromName, s => s.Order.FromName)
            .Map(d => d.FromGender, s => s.Order.FromGender)
            .Map(d => d.StartTime, s => s.Order.StartTime)
            .Map(d => d.FiledTime, s => s.Order.FiledTime)
            .Map(d => d.RecordUrl, s => s.OrderVisit.RecordUrl)
            .Map(d=>d.SeatEvaluate,s=>s.AiSeatEvaluate)
            .AfterMapping((s, d) =>
            {
                //d.SeatEvaluate = s.OrderVisit.OrderVisitDetails.FirstOrDefault(x => x.VisitTarget == EVisitTarget.Seat)?.SeatEvaluate;
                //d.OrgProcessingResults = s.OrderVisit.OrderVisitDetails.FirstOrDefault(x => x.VisitTarget == EVisitTarget.Org)?.OrgProcessingResults?.Value;
                //d.IsContact = s.OrderVisit.OrderVisitDetails.FirstOrDefault(x => x.VisitTarget == EVisitTarget.Org)?.IsContact == true ? "是" : s.OrderVisit.OrderVisitDetails.FirstOrDefault(x => x.VisitTarget == EVisitTarget.Org)?.IsContact == false ? "否" : "";
                //d.Volved = s.OrderVisit.OrderVisitDetails.FirstOrDefault(x => x.VisitTarget == EVisitTarget.Org)?.Volved == true ? "是" : s.OrderVisit.OrderVisitDetails.FirstOrDefault(x => x.VisitTarget == EVisitTarget.Org)?.Volved == false ? "否" :"";
                //d.IsSuccessText = s.IsSuccess == true ? "是" : s.IsSuccess == false ? "否" : "";
                d.OrgProcessingResults = s.AiOrgProcessingResults?.Value;
                d.IsContact = s.AiIsContact == true ? "是" : s.AiIsContact == false ? "否" : "";
                d.Volved = s.AiVolved == true ? "是" : s.AiVolved == false ? "否" : "";
                d.IsSuccessText = s.IsSuccess == true ? "是" : s.IsSuccess == false ? "否" : "";
            });

        config.ForType<OrderVisitDetail, OrgVisitDetailListResp>()
            //.IgnoreIf((s, d) => s.OrderVisit == null, d => d.VisitId)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.Id)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.No)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.ReTransactNum)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.HotspotSpliceName)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.OrgLevelOneName)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.ActualHandleOrgName)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.Title)
            .IgnoreIf((s, d) => s.OrderVisit.Employee == null, d => d.VisitUser)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.Content)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.FileOpinion)
            .IgnoreIf((s, d) => s.OrderVisit.Order == null, d => d.FiledTime)
            .Map(d => d.VisitId, s => s.OrderVisit.Id)
            .Map(d => d.Id, s => s.OrderVisit.Order.Id)
            .Map(d => d.No, s => s.OrderVisit.Order.No)
            .Map(d => d.ReTransactNum, s => s.OrderVisit.Order.ReTransactNum)
            .Map(d => d.HotspotSpliceName, s => s.OrderVisit.Order.HotspotSpliceName)
            .Map(d => d.OrgLevelOneName, s => s.OrderVisit.Order.OrgLevelOneName)
            .Map(d => d.ActualHandleOrgName, s => s.OrderVisit.Order.ActualHandleOrgName)
            .Map(d => d.Title, s => s.OrderVisit.Order.Title)
            .Map(d => d.VisitUser, s => s.OrderVisit.Employee.Name)
            .Map(d => d.VisitType, s => s.OrderVisit.VisitType)
            .Map(d => d.VisitTime, s => s.OrderVisit.VisitTime)
            .Map(d => d.Content, s => s.OrderVisit.Order.Content)
            .Map(d => d.FileOpinion, s => s.OrderVisit.Order.FileOpinion)
            .Map(d => d.FiledTime, s => s.OrderVisit.Order.FiledTime)
            .AfterMapping((s, d) =>
            {
                d.OrderScreenStatus = s.OrderVisit.Order.OrderScreens.OrderByDescending(q=>q.CreationTime).FirstOrDefault()?.Status;
                d.OrgProcessingResults = s.OrgProcessingResults.Value;
            });






        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) => !s.NearlyExpiredTimeOne.HasValue, d => d.NearlyExpiredTimeOne)
            .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)
            .Ignore(d => d.ProvinceNo);

        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.AcceptorId)
            .Ignore(d => d.AcceptorName)
            .Ignore(d => d.AcceptorStaffNo)
            //.AfterMapping((s, d) =>
            //{
            //    //d.UpdateHandlingStatus(s.IsInCountersign);
            //    d.CurrentStepAcceptTime = s.CurrentStepAcceptTime;
            //})
            ;

        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);

        config.ForType<OrderScreen, OrderScreenListDto>()
            .IgnoreIf((s, d) => s.VisitDetail == null, d => d.VisitDetail)
            ;
    }
}