using Hotline.Api.Controllers;
using Hotline.Application.Snapshot.Contracts;
using Hotline.Caching.Interfaces;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.Repository.SqlSugar.Snapshot;
using Hotline.Settings;
using Hotline.Share.Dtos.Article;
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 SnapshotBulletionApplicationTest : TestBase
{
    private readonly ISnapshotBulletinApplication _snapshotBulletinApplication;
    private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
    private readonly ISafetyTypeRepository _safetyTypeRepository;
    private readonly ISnapshotUserApplication _snapshotUserApplication;
    private readonly ISessionContext _sessionContext;
    private readonly ISnapshotApplication _snapshotApplication;
    private readonly ISnapshotBulletinRepository _bulletinRepository;

    public SnapshotBulletionApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IThirdIdentiyService thirdIdentiyService, IThirdAccountRepository thirdAccountRepository, ITypedCache<SystemSetting> cacheSettingData, ThirdAccounSupplierFactory thirdAccountDomainFactory, IServiceProvider serviceProvider, ISnapshotBulletinApplication snapshotBulletinApplication, ISystemDicDataCacheManager systemDicDataCacheManager, ISafetyTypeRepository safetyTypeRepository, ISnapshotUserApplication snapshotUserApplication, ISessionContext sessionContext, ISnapshotApplication snapshotApplication, ISnapshotBulletinRepository bulletinRepository) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdIdentiyService, thirdAccountRepository, cacheSettingData, thirdAccountDomainFactory, serviceProvider)
    {
        _snapshotBulletinApplication = snapshotBulletinApplication;
        _systemDicDataCacheManager = systemDicDataCacheManager;
        _safetyTypeRepository = safetyTypeRepository;
        _snapshotUserApplication = snapshotUserApplication;
        _sessionContext = sessionContext;
        _snapshotApplication = snapshotApplication;
        _bulletinRepository = bulletinRepository;
    }

    [Fact]
    public async Task Bulletin_Test()
    {
        SetWeiXin();
        var bulletinType =  _systemDicDataCacheManager.SnapshotBulletinType.First(m => m.DicDataName == "安全卫士");
        var safetyType = await _safetyTypeRepository.Queryable().FirstAsync();
        var addRelationInDto = new AddCitizenRelationSafetyTypeInDto { SafetyTypeId = safetyType.Id, CitizenIds = [_sessionContext.UserId] };
        await _snapshotUserApplication.AddCitizenRelationSafetyType(addRelationInDto, CancellationToken.None);
        var inDto = new AddSnapshotBulletinInDto
        {
            Title = "测试公告" + DateTime.Now.ToLongDateTimeString(),
            Content = "没什么内容",
            BulletinTime = DateTime.Now,
            Shape = Share.Enums.Snapshot.EBulletinShape.Video,
            BulletinTypeId = bulletinType.DicDataValue,
            BulletinTypeName = bulletinType.DicDataName,
            SafetyTypeId = [safetyType.Id],
            VideoCoverImgUrl = "222222",
            VideoPath = "333333"
        };
        var bulletinId = await _snapshotBulletinApplication.AddBulletinAsync(inDto, CancellationToken.None);
        var bulletin = await _bulletinRepository.GetAsync(bulletinId);
        bulletin.Shape.ShouldBe(Share.Enums.Snapshot.EBulletinShape.Video);
        bulletin.SafetyTypeId.First().ShouldBe(safetyType.Id);
        bulletin.VideoPath.ShouldBe(inDto.VideoPath);
        bulletin.VideoCoverImgUrl.ShouldBe(inDto.VideoCoverImgUrl);
        await _snapshotBulletinApplication.CommitBulletinAsync(bulletinId, CancellationToken.None);
        var examineDto = new ExamineBulletinDto
        {
            Id = bulletinId,
            IsPass = true,
            Reason = "审核通过"
        };
        await _snapshotBulletinApplication.ExamineBulletinAsync(examineDto, CancellationToken.None);
        Thread.Sleep(10 * 1000);
        //await _snapshotBulletinApplication.NotifyUserAsync(bulletinId, CancellationToken.None);


        var notifyItems = await _snapshotApplication.GetNotificationAsync(new GetNotifyInDto { NotifyType = Share.Enums.Snapshot.ENotificationType.Video}, CancellationToken.None);
        notifyItems.Any(m => m.Title == inDto.Title).ShouldBeTrue();
    }
}
