using AutoFixture;
using Hotline.Api.Controllers;
using Hotline.Api.Controllers.Snapshot;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Tools;
using Hotline.Tests.Dto;
using Hotline.Tests.Mock.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using XF.Domain.Authentications;

namespace Hotline.Tests.Mock;
public class OrderServiceStartWorkflow : IOrderServiceStartWorkflow
{
    public readonly IFixture _fixture;
    public readonly ISessionContext _sessionContext;
    private readonly OrderController _orderController;
    private readonly SnapshotController _snapshotController;
    public OrderServiceMock _orderServiceMock;

    public OrderServiceStartWorkflow(ISessionContext sessionContext, OrderController orderController, SnapshotController snapshotController)
    {
        _orderController = orderController;
        _orderController.ControllerContext = new ControllerContext
        {
            HttpContext = new DefaultHttpContext()
        };
        _snapshotController = snapshotController;
        _snapshotController.ControllerContext = new ControllerContext
        {
            HttpContext = new DefaultHttpContext()
        };
        _fixture = new Fixture();


        _sessionContext = sessionContext;
    }

    public OrderServiceMock orderServiceMock { set => _orderServiceMock = value; }

    public CreateOrderOutDto GetCreateResult()
    {
        return _orderServiceMock.CreateOrderOutDto;
    }

    public IOrderServiceStartWorkflow StepHandle(Func<CreateOrderOutDto, Task> handle)
    {
        handle(_orderServiceMock.CreateOrderOutDto).GetAwaiter().GetResult();
        return this;
    }

    public OrderServiceMock 办理到一级部门(Action action = null)
    {
        action?.Invoke();
        var stepNextInfo = _orderController.GetFlowStartOptions(_orderServiceMock.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 = _orderServiceMock.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 _orderServiceMock;
    }

    public OrderServiceMock 办理到工单标注(Action action = null)
    {
        action?.Invoke();
        var stepNextInfo = _orderController.GetFlowStartOptions(_orderServiceMock.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 = _orderServiceMock.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 _orderServiceMock;
    }

    public OrderServiceMock 办理到归档(Action action = null)
    {
        action?.Invoke();
        var stepNextInfo = _orderController.GetFlowStartOptions(_orderServiceMock.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 = _orderServiceMock.CreateOrderOutDto.Id,
            },
            Workflow = new BasicWorkflowDto
            {
                NextHandlers = null,
                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 _orderServiceMock;
    }

    public OrderServiceMock 办理到派单员(Action action = null)
    {
        action?.Invoke();
        var stepNextInfo = _orderController.GetFlowStartOptions(_orderServiceMock.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 = _orderServiceMock.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 _orderServiceMock;
    }

    public OrderServiceMock 办理到网格员(Action action = null)
    {
        action?.Invoke();
        var stepNextInfo = _orderController.GetFlowStartOptions(_orderServiceMock.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 = _orderServiceMock.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,
                FlowDirection = EFlowDirection.CenterToOrg,
            }
        };
        _orderController.StartFlow(handleDto).GetAwaiter().GetResult();
        return _orderServiceMock;
    }
}
