using Hotline.Api.Controllers;
using Hotline.Application.Snapshot.Contracts;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.Orders;
using Hotline.Settings;
using Hotline.Share.Dtos.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.Extensions.DependencyInjection;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Authentications;
using XF.Domain.Cache;
using XF.Domain.Repository;

namespace Hotline.Tests.Application;

public class PointsRecordApplicationTest : TestBase
{
    private readonly ISnapshotPointsApplication _pointsRecordApplication;
    private readonly ISnapshotApplication _snapshotApplication;
    private readonly ISessionContext _sessionContext;
    private readonly ICitizenRepository _citizenRepository;
    private readonly ISafetyTypeRepository _safetyTypeRepository;

    public PointsRecordApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IThirdIdentiyService thirdIdentiyService, IThirdAccountRepository thirdAccountRepository, ITypedCache<SystemSetting> cacheSettingData, ThirdAccounSupplierFactory thirdAccountDomainFactory, ISnapshotPointsApplication pointsRecordApplication, ISnapshotApplication snapshotApplication, IServiceProvider serviceProvider, ISessionContext sessionContext, ICitizenRepository citizenRepository, ISafetyTypeRepository safetyTypeRepository) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdIdentiyService, thirdAccountRepository, cacheSettingData, thirdAccountDomainFactory, serviceProvider)
    {
        _pointsRecordApplication = pointsRecordApplication;
        _snapshotApplication = snapshotApplication;
        _sessionContext = sessionContext;
        _citizenRepository = citizenRepository;
        _safetyTypeRepository = safetyTypeRepository;
    }

    [Fact]
    public async Task GetPointsItems_Test()
    {
        var inDto = new PointsItemsInDto
        {
            StartTime = DateTime.Now.AddDays(-1),
            EndTime = DateTime.Now
        };
        var items = await _pointsRecordApplication.GetPointsItems(inDto).ToListAsync();
        items.ShouldNotBeNull();
    }

    [Fact]
    public async Task UpdateIsSecurityMax_Test()
    {
        SetWeiXin();
        var inDto = new UpdateIsSecurityMaxAsync
        {
            UserId = _sessionContext.UserId,
            IsSecurityMax = false
        };
        await _pointsRecordApplication.UpdateIsSecurityMaxAsync(inDto, CancellationToken.None);
        var citizen = await _citizenRepository.Queryable()
            .Where(m => m.Id == inDto.UserId)
            .Includes(m => m.SafetyTypes)
            .FirstAsync();
        citizen.SafetyTypes.Any(m => m.Name == "安全卫士").ShouldBeFalse();

        inDto.IsSecurityMax = true;
        await _pointsRecordApplication.UpdateIsSecurityMaxAsync(inDto, CancellationToken.None);
        citizen = await _citizenRepository.Queryable()
            .Where(m => m.Id == inDto.UserId)
            .Includes(m => m.SafetyTypes)
            .FirstAsync();
        citizen.SafetyTypes.Any(m => m.Name == "安全卫士").ShouldBeTrue();
    }

    [Fact]
    public async Task GetPointsRank_Test()
    {
        SetWeiXin();
        var item = await _snapshotApplication.GetPointsRankAsync();
        item.ShouldNotBeNull();
        item.Bulletins.Items.Any(m => m.Title.IsNullOrEmpty()).ShouldBeFalse();
        item.Bulletins.Items.Any(m => m.BulletinId.IsNullOrEmpty()).ShouldBeFalse();
        item.VideoBulletin.ShouldNotBeNull();
    }
}
