using AutoFixture;
using EasyCaching.Core;
using Hotline.Api.Controllers;
using Hotline.Api.Controllers.Snapshot;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.Orders;
using Hotline.Settings;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Share.Enums.Snapshot;
using Hotline.Share.Tools;
using Hotline.Snapshot.IRepository;
using Hotline.ThirdAccountDomainServices;
using Hotline.ThirdAccountDomainServices.Interfaces;
using Hotline.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Cache;
using XF.Domain.Repository;

namespace Hotline.Tests.Controller;
public class SnapshotControllerTest : TestBase
{
    private readonly SnapshotController _snapshotController;
    private readonly IOrderRepository _orderRepository;
    private readonly IOrderSnapshotRepository _orderSnapshotRepository;
    private readonly IIndustryRepository _industryRepository;
    private readonly IEasyCachingProvider _easyCaching;
    private readonly IRedisCachingProvider _redisCaching;

    public SnapshotControllerTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, SnapshotController snapshotController, IOrderRepository orderRepository, IOrderSnapshotRepository orderSnapshotRepository, IIndustryRepository industryRepository, IThirdIdentiyService thirdService, IThirdAccountRepository thirdAccount, IEasyCachingProvider easyCaching, IRedisCachingProvider redisCaching, ITypedCache<SystemSetting> cacheSettingData, ThirdAccounSupplierFactory thirdAccountDomainFactory, IServiceProvider serviceProvider) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdService, thirdAccount, cacheSettingData, thirdAccountDomainFactory, serviceProvider)
    {
        _snapshotController = snapshotController;
        _snapshotController.ControllerContext = new ControllerContext
        {
            HttpContext = new DefaultHttpContext()
        };
        _orderRepository = orderRepository;
        _orderSnapshotRepository = orderSnapshotRepository;
        _industryRepository = industryRepository;
        _easyCaching = easyCaching;
        _redisCaching = redisCaching;
    }

    [Fact]
    public async Task GetAreaTree_Test()
    {
        var result = await _snapshotController.GetAreaTreeAsync();
        result.ShouldNotBeNull();
        var zgs = result.Where(m => m.AreaName == "自贡市").FirstOrDefault();
        zgs.ShouldNotBeNull();
        var rx = zgs.Children.Where(m => m.AreaName == "荣县").FirstOrDefault();
        rx.ShouldNotBeNull();
        var zzz = rx.Children.Where(m => m.AreaName == "正紫镇").FirstOrDefault();
        zzz.ShouldNotBeNull();
    }

    /// <summary>
    /// 测试创建随手拍工单
    /// </summary>
    /// <returns></returns>
    [Fact]
    public async Task AddOrder_Test()
    {
        var cacheKey = "Hotline:CollectionJobType";
        await _easyCaching.RemoveAsync(cacheKey, CancellationToken.None);
        var exist = await _easyCaching.ExistsAsync(cacheKey);
        await _redisCaching.KeyDelAsync(cacheKey);
        var existRedis = await _redisCaching.KeyExistsAsync(cacheKey);

        var homePage = await _snapshotController.GetHomePageAsync();
        var industry = homePage.Industrys.Where(m => m.IndustryType == EIndustryType.Declare).FirstOrDefault();
        var pageBase = await _snapshotController.GetIndustryBaseAsync(industry.Id);

        var inDto = _fixture.Create<AddSnapshotOrderInDto>();
        inDto.Street = "单元测试街道" + DateTime.Now.ToLongDateTimeString();
        inDto.IndustryId = industry.Id;
        inDto.Town = "仙市镇";
        inDto.County = "沿滩区";
        inDto.Description = "单元测试添加的时间描述";
        inDto.IsSecret = false;
        inDto.JobType = 1;
        foreach (var item in inDto.Files)
        {
            item.FileName = DateTime.Now.ToShortTimeString() + "文件.doc";
        }

        var order = await _snapshotController.AddOrderAsync(inDto);
        var orderEntity = await _orderRepository.GetAsync(order.Id);
        orderEntity.ShouldNotBeNull();
        orderEntity.Latitude.ShouldBe(inDto.Latitude);
        orderEntity.Longitude.ShouldBe(inDto.Longitude);
        orderEntity.Title.ShouldNotBeNullOrEmpty();
        orderEntity.Content.ShouldNotBeNullOrEmpty();
        var orderSnapshotEntity = await _orderSnapshotRepository.GetAsync(order.Id);
        orderSnapshotEntity.ShouldNotBeNull();
        orderSnapshotEntity.JobType.ShouldBe(inDto.JobType);
        orderSnapshotEntity.JobTypeName.ShouldBe("气割");
    }
}
