using Hotline.Tests.Mock;
using System.Diagnostics;
using Hotline.Api.Controllers;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.Settings;
using Hotline.ThirdAccountDomainServices;
using Hotline.ThirdAccountDomainServices.Interfaces;
using Hotline.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using XF.Domain.Cache;
using XF.Domain.Repository;
using Hotline.Orders;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Enums.Order;
using MapsterMapper;
using Xunit.Abstractions;

namespace Hotline.Tests.Controller
{
    public class OrderDelayControllerTest : TestBase
    {
        private readonly OrderController _orderController;
        private readonly OrderServiceMock _orderServiceMock;
        private readonly IOrderDelayRepository _orderDelayRepository;
        private readonly IOrderRepository _orderRepository;
        private readonly IMapper _mapper;
        private readonly ITestOutputHelper _testOutputHelper;

        public OrderDelayControllerTest(IAccountRepository accountRepository, IRepository<Role> roleRepository,
            UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository,
            IHttpContextAccessor httpContextAccessor, IThirdIdentiyService thirdIdentiyService,
            IThirdAccountRepository thirdAccountRepository, ITypedCache<SystemSetting> cacheSettingData,
            ThirdAccounSupplierFactory thirdAccountDomainFactory,
            OrderController orderController,
            OrderServiceMock orderServiceMock,
            IOrderDelayRepository orderDelayRepository,
            IOrderRepository orderRepository,
            IMapper mapper,
            ITestOutputHelper testOutputHelper, IServiceProvider serviceProvider) : base(accountRepository, roleRepository,
            userController, scopeFactory, userRepository, httpContextAccessor, thirdIdentiyService,
            thirdAccountRepository, cacheSettingData, thirdAccountDomainFactory, serviceProvider)
        {
            _orderController = orderController;
            _orderServiceMock = orderServiceMock;
            _orderDelayRepository = orderDelayRepository;
            _orderRepository = orderRepository;
            _mapper = mapper;
            _testOutputHelper = testOutputHelper;
        }

        [Fact]
        public async Task OrderDelayBatchApproveTest()
        {
            SetZuoXi();

            var delayIds = new List<string>();
            for (int i = 0; i < 10; i++)
            {
                var orderId = _orderServiceMock.CreateOrder()
                    .办理到一级部门()
                    .办理到二级部门(Set一级部门)
                    .GetCreateResult().Id;

                //var order = await _orderRepository.Queryable()
                //    .FirstAsync(o => o.Id == orderId);
                var delayId = _orderServiceMock.申请延期(Set二级部门);
                delayIds.Add(delayId);
            }

            var delay = await _orderDelayRepository.GetAsync(delayIds.First());
            
            Set一级部门();
            var option =  _orderController.OrderDelayNextsteps(delay.WorkflowId).Result as NextStepsWithOpinionDto<NextStepOption>;
            var step = option.Steps.First();
            
            var req = new BatchDelayNextFlowDto
            {
                DelayId = delayIds.ToArray(),
                IsPass = true,
                NextWorkflow = new NextWorkflowDto
                {
                    NextStepCode = step.Key,
                    NextStepName = step.Value,
                }
            };

            _testOutputHelper.WriteLine($"开始审批");
            var sw = new Stopwatch();
            sw.Start();
            var result = await _orderController.BatchAuditDelay(req);
            sw.Stop();
            
            _testOutputHelper.WriteLine($"耗时：{sw.ElapsedMilliseconds}ms");
            _testOutputHelper.WriteLine(result);

            //var delay = await _orderDelayRepository.Queryable()
            //    .Includes(d => d.Order)
            //    .FirstAsync(d => d.Id == delayId);

            //Assert.Equal(EDelayState.Pass, delay.DelayState);
            //Assert.True(delay.Order.ExpiredTime > order.ExpiredTime);

            //Console.WriteLine(delay.Id);
        }

        [Fact]
        public void QueryWaitForApproveDelaysTest()
        {
            _testOutputHelper.WriteLine("123");
        }
    }
}
