using Hotline.Api.Controllers;
using Hotline.Application.Tests.Dto;
using Hotline.Orders;
using Hotline.Repository.SqlSugar.Orders;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Repository;

namespace Hotline.Application.Tests.Mock;
public class OrderServiceMock
{
    private readonly OrderController _orderController;
    private CreateOrderOutDto CreateOrderOutDto;
    private AddOrderDto AddOrderDto;
    private readonly IRepository<Order> _orderRepository;

    public OrderServiceMock(OrderController orderController, IRepository<Order> orderRepository)
    {
        _orderController = orderController;
        _orderRepository = orderRepository;
    }

    public OrderServiceMock CreateOrder()
    {
        var json = "{\"sourceChannel\":\"因特网\",\"sourceChannelCode\":\"YTW\",\"transferPhone\":null,\"fromPhone\":null,\"acceptorName\":\"单元测试\",\"acceptorStaffNo\":\"\",\"fromName\":\"1233333333\",\"fromGender\":1,\"identityType\":1,\"licenceType\":null,\"licenceTypeCode\":null,\"licenceNo\":null,\"ageRange\":null,\"ageRangeCode\":null,\"contact\":\"12333333333\",\"isSecret\":false,\"acceptSms\":false,\"no\":null,\"title\":\"\",\"hotspotId\":\"1912\",\"eventCategoryId\":null,\"incidentTime\":null,\"incidentPurpose\":null,\"areaCode\":\"519800\",\"city\":\"省内\",\"street\":null,\"isRepeat\":\"false\",\"pushType\":null,\"pushTypeCode\":null,\"content\":\"单元测试内容\",\"duplicateIds\":[],\"duplicateTitle\":null,\"callAddress\":null,\"repeatableEventDetails\":[],\"orderExtension\":null,\"transpond\":false,\"isEnforcementOrder\":false,\"focusOnEventsArr\":[],\"focusOnEvents\":null,\"isFormalistWorkOrder\":false,\"isSensitiveWorkOrders\":false,\"isUrgent\":false,\"isThreePartyConference\":false,\"is24HoursComplete\":false,\"company\":null,\"orderPushTypes\":[],\"acceptType\":\"咨询\",\"acceptTypeCode\":\"10\",\"files\":[],\"hotspotSpliceName\":\"互联互通-转接乐山市12345\",\"hotspotName\":\"转接乐山市12345\",\"hotspotCode\":\"1912\",\"hotspotExternal\":\"19\",\"county\":\"\",\"town\":\"\"}";
        AddOrderDto = json.FromJson<AddOrderDto>();
        AddOrderDto.Title = "单元测试" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        CreateOrderOutDto = _orderController.Add(AddOrderDto).GetAwaiter().GetResult().ToJson().FromJson<CreateOrderOutDto>();
        return this;
    }

    public CreateOrderOutDto GetCreateResult()
    {
        return CreateOrderOutDto;
    }

    public OrderServiceMock 办理到派单员()
    { 
        var stepNextInfo = _orderController.GetFlowStartOptions(CreateOrderOutDto.Id).GetAwaiter().GetResult().ToJson().FromJson<NextStepsDto<NextStepOption>>();
        var stepInfo = stepNextInfo.Steps.FirstOrDefault(m => m.Value == "派单组");
        var stepOrg = stepInfo.Items.FirstOrDefault(m => m.Username == "单元测试派单员");

        var handleDto = new StartWorkflowDto<OrderHandleFlowDto>
        {
            Data = new OrderHandleFlowDto
            {
                OrderId = CreateOrderOutDto.Id,
            },
            Workflow = new BasicWorkflowDto
            {
                NextHandlers = [stepOrg],
                NextStepCode = stepInfo.Key,
                NextStepName = stepInfo.Value,
                Opinion = "办理到派单组意见",
                BackToCountersignEnd = false,
                IsSms = false,
                IsForwarded = false,
                HandlerType = EHandlerType.OrgLevel,
                BusinessType = EBusinessType.Send,
                FlowDirection = EFlowDirection.CenterToCenter,
            }
        };
        _orderController.StartFlow(handleDto).GetAwaiter().GetResult();
        return this;

    }

    public OrderServiceMock 办理到一级部门()
    {
        var stepNextInfo = _orderController.GetFlowStartOptions(CreateOrderOutDto.Id).GetAwaiter().GetResult().ToJson().FromJson<NextStepsDto<NextStepOption>>();
        var stepInfo = stepNextInfo.Steps.FirstOrDefault(m => m.Value == "一级部门");
        var stepOrg = stepInfo.Items.FirstOrDefault(m => m.OrgName == "测试部门");

        var handleDto = new StartWorkflowDto<OrderHandleFlowDto>
        {
            Data = new OrderHandleFlowDto
            {
                OrderId = CreateOrderOutDto.Id,
            },
            Workflow = new BasicWorkflowDto
            {
                NextHandlers = [stepOrg],
                NextStepCode = stepInfo.Key,
                NextStepName = stepInfo.Value,
                Opinion = "办理意见",
                BackToCountersignEnd = false,
                IsSms = false,
                IsForwarded = false,
                HandlerType = EHandlerType.OrgLevel,
                BusinessType = EBusinessType.Department,
            }
        };
        _orderController.StartFlow(handleDto).GetAwaiter().GetResult();
        return this;
    }

    public OrderServiceMock 办理到归档(Action action = null, Action<OrderHandleFlowDto> dataAction = null, Action<NextWorkflowDto> workflowAction = null)
    {
        action?.Invoke();
        var stepNextInfo = _orderController.GetNextStepsWithRecommend(CreateOrderOutDto.Id).GetAwaiter().GetResult().ToJson().FromJson<NextStepsDto<NextStepOption>>();
        var stepInfo = stepNextInfo.Steps.FirstOrDefault(m => m.Value == "归档");
        var stepOrg = stepInfo.Items.FirstOrDefault();

        var order = _orderRepository.Get(CreateOrderOutDto.Id);

        var handleDto = new NextWorkflowDto<OrderHandleFlowDto>
        {
            Data = new OrderHandleFlowDto
            {
                OrderId = CreateOrderOutDto.Id,
                RealHandlerName = "经办人",
                RealHandlerPhone = "13666666666",
                RealContactLocale = true,
                RealIsContacted = true,
                IsOther = true,
                OtherRemark = "其它原因",
                RealCommunicationAddress = "地点地点地点",
                RealCommunicationTime = DateTime.Now,
            },
            Workflow = new NextWorkflowDto
            {
                NextStepCode = "end",
                NextStepName = "归档",
                Opinion = "办理到归档",
                BackToCountersignEnd = false,
                IsSms = false,
                IsForwarded = false,
                HandlerType = EHandlerType.OrgLevel,
                BusinessType = EBusinessType.Department,
                FlowDirection = EFlowDirection.OrgToOrg,
                WorkflowId = order.WorkflowId,
                StepId = stepNextInfo.StepId,
            }
        };
        dataAction?.Invoke(handleDto.Data);
        workflowAction?.Invoke(handleDto.Workflow);
        try
        {
            _orderController.Handle(handleDto).GetAwaiter().GetResult();
        }
        catch (Exception e)
        {
            var msg = e.Message;
            if (msg.Contains("RealtimeService") == false)
            {
                throw;
            }
            // ignore
        }
        return this;
    }

    public OrderServiceMock 办理到二级部门(Action action = null)
    {
        action?.Invoke();
        var stepNextInfo = _orderController.GetNextStepsWithRecommend(CreateOrderOutDto.Id).GetAwaiter().GetResult().ToJson().FromJson<NextStepsDto<NextStepOption>>();
        var stepInfo = stepNextInfo.Steps.FirstOrDefault(m => m.Value == "二级部门");
        var stepOrg = stepInfo.Items.FirstOrDefault(m => m.OrgName == "测试二级部门");

        var order = _orderRepository.Get(CreateOrderOutDto.Id);

        var handleDto = new NextWorkflowDto<OrderHandleFlowDto>
        {
            Data = new OrderHandleFlowDto
            {
                OrderId = CreateOrderOutDto.Id,
            },
            Workflow = new NextWorkflowDto
            {
                NextHandlers = [stepOrg],
                NextStepCode = stepInfo.Key,
                NextStepName = stepInfo.Value,
                Opinion = "办理到二级部门",
                BackToCountersignEnd = false,
                IsSms = false,
                IsForwarded = false,
                HandlerType = EHandlerType.OrgLevel,
                BusinessType = EBusinessType.Department,
                FlowDirection = EFlowDirection.OrgToOrg,
                WorkflowId = order.WorkflowId,
                StepId = stepNextInfo.StepId,
            }
        };
        _orderController.Handle(handleDto).GetAwaiter().GetResult();
        return this;
    }

    public OrderServiceMock 办理一级部门汇总(Action aciton = null)
    {
        aciton?.Invoke();
        var stepNextInfo = _orderController.GetNextStepsWithRecommend(CreateOrderOutDto.Id).GetAwaiter().GetResult().ToJson().FromJson<NextStepsDto<NextStepOption>>();
        var stepInfo = stepNextInfo.Steps.FirstOrDefault(m => m.Value == "一级部门汇总");
        var stepOrg = stepInfo.Items.FirstOrDefault(m => m.OrgName == "测试部门");

        var order = _orderRepository.Get(CreateOrderOutDto.Id);

        var handleDto = new NextWorkflowDto<OrderHandleFlowDto>
        {
            Data = new OrderHandleFlowDto
            {
                OrderId = CreateOrderOutDto.Id,
                RealHandlerName = "经办人",
                RealHandlerPhone = "13666666666",
                RealContactLocale = true,
                RealIsContacted = true,
                IsOther = true,
                IsEvasive = true,
                OtherRemark = "其它原因",
                RealCommunicationAddress = "地点地点地点",
                RealCommunicationTime = DateTime.Now
            },
            Workflow = new NextWorkflowDto
            {
                NextHandlers = [stepOrg],
                NextStepCode = stepInfo.Key,
                NextStepName = stepInfo.Value,
                Opinion = "办理到一级部门汇总",
                BackToCountersignEnd = false,
                IsSms = false,
                IsForwarded = false,
                HandlerType = EHandlerType.AssignedUser,
                BusinessType = EBusinessType.Department,
                FlowDirection = EFlowDirection.OrgToOrg,
                StepType = EStepType.Summary,
                WorkflowId = order.WorkflowId,
                StepId = stepNextInfo.StepId,
            }
        };
        _orderController.Handle(handleDto).GetAwaiter().GetResult();
        return this;
    }
}
