using Hotline.Api.Controllers;
using Hotline.Application.FlowEngine;
using Hotline.FlowEngine.WorkflowModules;
using Hotline.Identity.Accounts;
using Hotline.Share.Dtos.Knowledge;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Tools;
using Hotline.Tests.Infrastructure;
using XF.Domain.Repository;

namespace Hotline.Tests.Mock;
public class KnowledgeServiceMock
{
    private readonly KnowledgeController _knowledgeController;
    private string KnowledgeId;
    private readonly IWorkflowApplication _workflowApplication;
    private readonly IRepository<Account> _accountRepository;

    public KnowledgeServiceMock(KnowledgeController knowledgeController, IWorkflowApplication workflowApplication, IRepository<Account> accountRepository)
    {
        _knowledgeController = knowledgeController;
        _workflowApplication = workflowApplication;
        _accountRepository = accountRepository;
    }

    public KnowledgeServiceMock 创建并审核知识()
    {
        var json = "{\"data\":{\"attribution\":\"中心知识库\",\"isPublic\":true,\"keywords\":[\"08dcfe29-01a5-4b6e-8b47-640d75aa8b23\",\"08dcfe29-01a7-4488-8412-65907c33576d\",\"08dcfe29-01a8-4217-84ae-45eda2481c9b\"],\"keywordsName\":\"张继科扥,金卡,希望\",\"knowledgeTypeId\":[\"08dc903c-aad8-4515-89f3-1ae2d6961b90\"],\"hotspotId\":\"\",\"knowledges\":[],\"files\":[],\"template\":\"\",\"content\":\"<p style=\\\"line-height: 2;\\\"><span style=\\\"font-size: 20px; font-family: 仿宋;\\\">你张继科扥金卡希望智啊</span></p>\",\"sourceOrganizeId\":\"\",\"title\":\"你张继科扥金卡希望智啊\",\"knowledgeType\":[{\"KnowledgeTypeName\":\"类型11\",\"KnowledgeTypeId\":\"08dc903c-aad8-4515-89f3-1ae2d6961b90\",\"KnowledgeTypeSpliceName\":\"类型11\"}]},\"workflow\":{\"isPass\":true,\"opinion\":\"123\",\"nextStepCode\":\"da354c75-d8f7-4c8c-af6f-1e69fb09c343\",\"nextStepName\":\"流程节点\",\"backToCountersignEnd\":false,\"nextHandlers\":[{\"userId\":\"08dc3c21-8e9b-4387-8306-6cc32d127397\",\"username\":\"班长2\",\"orgId\":\"001\",\"orgName\":\"市民热线服务中心\",\"roleId\":\"zhongxinlingdao\",\"roleName\":\"中心领导\",\"key\":\"08dc3c21-8e9b-4387-8306-6cc32d127397\",\"value\":\"班长2\"}],\"nextMainHandler\":\"08dc3c21-8e9b-4387-8306-6cc32d127397\",\"isSms\":false,\"isStartCountersign\":false,\"stepId\":null,\"nextHandler\":{},\"workflowId\":\"\",\"handlerType\":0,\"businessType\":0,\"flowDirection\":2,\"external\":{},\"files\":[]}}";
        var inDto = json.FromJson<AddStartFlowDto>();
        var nextStep = _workflowApplication.GetStartStepsAsync(WorkflowModuleConsts.KnowledgeAdd, CancellationToken.None).GetAwaiter().GetResult();
        inDto.Data.Title = "单元测试" + DateTime.Now.ToString("yyyyMMddhhmmss");
        inDto.Workflow.FlowDirection = EFlowDirection.CenterToCenter;
        inDto.Workflow.NextStepCode = nextStep.Steps.First().Key;
        inDto.Workflow.NextStepName = nextStep.Steps.First().Value;
        var account = _accountRepository.GetAsync(m => m.UserName == TestSettingConstants.BanZhangAccountName).GetAwaiter().GetResult();
        var handler = nextStep.Steps.First().Items.Where(m => m.Username == account.Name).First();
        inDto.Workflow.NextHandlers.Add(handler);
        KnowledgeId = _knowledgeController.AddKnowledge(inDto).GetAwaiter().GetResult();
        return this;
    }

    public string GetKnowledgeId()
    {
        return KnowledgeId;
    }
}
