using Hotline.Api.Controllers;
using Hotline.Application.Knowledge;
using Hotline.Application.Tools;
using Hotline.File;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.KnowledgeBase;
using Hotline.KnowledgeBase.Notifies;
using Hotline.Settings;
using Hotline.Share.Dtos.File;
using Hotline.Share.Dtos.Knowledge;
using Hotline.Share.Enums.Article;
using Hotline.Share.Tools;
using Hotline.ThirdAccountDomainServices;
using Hotline.ThirdAccountDomainServices.Interfaces;
using Hotline.Users;
using Mapster;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Senparc.Weixin.WxOpen.AdvancedAPIs.Tcb;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using XF.Domain.Cache;
using XF.Domain.Repository;

namespace Hotline.Tests.Application;
public class KnowApplicationTest : TestBase
{
    private readonly IKnowApplication _knowApplication;
    private readonly IMediator _mediator;
    private readonly IRepository<KnowledgeBase.Knowledge> _knowledgeRepository;
    private readonly IKnowledgeDomainService _knowledgeDomainService;
    private readonly IRepository<KnowledgeWord> _knowledgeWordRepository;
    private readonly IRepository<KnowledgeHotWord> _knowledgeHotWordRepository;
    private readonly IFileDomainService _fileDomainService;

    public KnowApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IKnowApplication knowApplication, IMediator mediator, IRepository<KnowledgeBase.Knowledge> knowledgeRepository, IKnowledgeDomainService knowledgeDomainService, IRepository<KnowledgeWord> knowledgeWordRepository, IRepository<KnowledgeHotWord> knowledgeHotWordRepository, IThirdIdentiyService thirdService, IThirdAccountRepository thirdAccount, ITypedCache<SystemSetting> cacheSettingData, ThirdAccounSupplierFactory thirdAccountDomainFactory, IFileDomainService fileDomainService, IServiceProvider serviceProvider) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdService, thirdAccount, cacheSettingData, thirdAccountDomainFactory, serviceProvider)
    {
        _knowApplication = knowApplication;
        _mediator = mediator;
        _knowledgeRepository = knowledgeRepository;
        _knowledgeDomainService = knowledgeDomainService;
        _knowledgeWordRepository = knowledgeWordRepository;
        _knowledgeHotWordRepository = knowledgeHotWordRepository;
        _fileDomainService = fileDomainService;
    }

    [Fact]
    public async Task TitleParticiple_Test()
    {
        var result = await _knowApplication.TitleParticiple("短信回访的相关解释口径、规范用语");
        result.ShouldNotBeNull();
    }

    [Fact]
    public void SplitKeywords_Test()
    {
        var keywords = "短信 筷子, 天才， 脑洞, 大开";
        var items = keywords.SplitKeywords();
        items.Count.ShouldBe(5);
        items[0] = "短信";
        items[1] = "筷子";
        items[2] = "天才";
        items[3] = "脑洞";
        items[4] = "大开";
    }

    //[Fact]
    public async Task GetPageViewList_Test()
    {
        //var r = await _knowledgeRelationTypeRepository.Queryable()
        //    .OrderByDescending(m => m.CreationTime)
        //    .FirstAsync();
        //var knowledge = await _knowledgeRepository.GetAsync(r.KnowledgeId);
        //await _knowledgeDomainService.KnowledgePvIncreaseAsync(knowledge, new CancellationToken());

        //var inDto = new PageViewInDto
        //{
        //    KnowledgeTypeId = r.KnowledgeTypeId
        //};
        //var (total, items) = await _knowApplication.GetPageViewListAsync(inDto);
        //total.ShouldNotBe(0);
    }

    [Fact]
    public async Task UpdateKnowledgeWord_Test()
    {
        var entity = await _knowledgeHotWordRepository.Queryable()
            .OrderByDescending(m => m.CreationTime)
            .FirstAsync();
        entity.KeyWord = "单元测试修改";
        var inDto = entity.Adapt<UpdateKnowledgeHotWordInDto>();
        await _knowApplication.UpdateKnowledgeHotWordAsync(inDto);
        var updateEntity = await _knowledgeHotWordRepository.GetAsync(entity.Id);
        updateEntity.KeyWord.ShouldBe(entity.KeyWord);
    }

    [Fact]
    public async Task AddKnowledgeHotWord_Test()
    {
        var addDto = new AddKnowledgeHotWordInDto
        {
            KeyWord = "单元测试" + new Random().Next(1, 100),
        };
        await _knowApplication.AddKnowledgeHotWordAsync(addDto);

        try
        {
            await _knowApplication.AddKnowledgeHotWordAsync(addDto);

        }
        catch (Exception e)
        {
            e.Message.ShouldBe("热词已存在");
        }
    }

    /// <summary>
    /// 测试扩展方法是否能正确判断集合空和非空的情况
    /// </summary>
    [Fact]
    public void ListEx_Test()
    {
        List<string> a = null;
        a.IsNullOrEmpty().ShouldBeTrue();
        a.NotNullOrEmpty().ShouldBeFalse();
        a = new List<string>();
        a.IsNullOrEmpty().ShouldBeTrue();
        a.NotNullOrEmpty().ShouldBeFalse();
    }

    [Fact]
    public async Task Word_Upload_Test()
    {

        var fileJson = await _fileDomainService.UploadFileFromMemoryAsync("<div>123</div>".HtmlToWord(), "测试文件" + EFileType.word.GetFileExtension(), EFileType.word);
        fileJson.Id.ShouldNotBeNull();
    }
}
