using Hotline.Application.Knowledge;
using Hotline.KnowledgeBase;
using Hotline.KnowledgeBase.Notifies;
using Hotline.Share.Dtos.Knowledge;
using Hotline.Share.Tools;
using Mapster;
using MediatR;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using XF.Domain.Repository;

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

    public KnowApplicationTest(IKnowApplication knowApplication, IRepository<KnowledgeRelationType> knowledgeRelationTypeRepository, IMediator mediator, IRepository<KnowledgeBase.Knowledge> knowledgeRepository, IKnowledgeDomainService knowledgeDomainService, IRepository<KnowledgeWord> knowledgeWordRepository, IRepository<KnowledgeHotWord> knowledgeHotWordRepository)
    {
        _knowApplication = knowApplication;
        _knowledgeRelationTypeRepository = knowledgeRelationTypeRepository;
        _mediator = mediator;
        _knowledgeRepository = knowledgeRepository;
        _knowledgeDomainService = knowledgeDomainService;
        _knowledgeWordRepository = knowledgeWordRepository;
        _knowledgeHotWordRepository = knowledgeHotWordRepository;
    }

    [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();
    }
}
