package com.friday.tooldriver.service.rpa.supplier;

import com.friday.tooldriver.dto.infodator.response.AddRobotRequestDto;
import com.friday.tooldriver.dto.infodator.response.RpaRobot;
import com.friday.tooldriver.dto.infodator.response.Task;
import com.friday.tooldriver.dto.infodator.response.TaskStatus;
import com.google.common.collect.Lists;
import com.friday.base.common.response.DubboResult;
import com.friday.operation.api.tooldriver.dto.common.DriverBase;
import com.friday.tooldriver.ServiceUTBase;
import com.friday.tooldriver.dto.infodator.common.FilterEntity;
import com.friday.tooldriver.dto.infodator.common.PageParam;
import com.friday.tooldriver.dto.infodator.request.GroupRequestDto;
import com.friday.tooldriver.dto.infodator.request.QueryRequestDto;
import com.friday.tooldriver.dto.infodator.response.*;
import com.friday.tooldriver.exception.DriverBusinessException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class INFODATORServiceTest extends ServiceUTBase {

    @Autowired
    private INFODATORService infodatorService;

    private DriverBase getDriverBase(){
        return DriverBase.builder()
                .tenantKey("default")
                .groupKey(null)
                .build();
    }

    private void addFilters (String classType, String fieldName, String filterComparator, Object params, List<Map<String, Object>> filters) {
        Map<String,Object> filterMap = new HashMap<>();
        filterMap.put("@classType", classType);
        filterMap.put("fieldName", fieldName);
        filterMap.put("filterComparator", filterComparator);
        filterMap.put("params", params);
        filters.add(filterMap);
    }

    @Test
    public void authenticate() throws DriverBusinessException {
        String result = infodatorService.authenticate(getDriverBase());
        System.out.println("执行结果：" + result);
        Assert.assertNotNull(result);
    }

    @Test
    public void uploadPackage() {
    }

    @Test
    public void addGroup() {
        GroupRequestDto groupRequestDto = GroupRequestDto.builder()
                .name("groupName2")
                .description("测试组")
                .colorExpress("#FDA21C")
                .build();
        DubboResult result = infodatorService.addGroup(getDriverBase(), groupRequestDto);
        System.out.println("执行结果：" + result);
        Assert.assertNotNull(result);
    }

    @Test
    public void getTaskHistoryList() {
        // 查询历史任务
        List<Map<String,Object>> filters = Lists.newArrayList();
        addFilters("comparator", "deleted", "EQ", new boolean[]{false}, filters);
        // 组、流程、机器、任务名
//        addFilters("comparator", "groupId", "EQ", new Integer[]{18}, filters);
//        addFilters("comparator", "templateId", "EQ", new Integer[]{18}, filters);
        addFilters("comparator", "taskExecutionQueueId", "EQ", new Integer[]{29846}, filters);
//        addFilters("comparator", "taskName", "CONTAINS", new String[]{"测试"}, filters);
        QueryRequestDto queryRequestDto = QueryRequestDto.builder()
                .pageParam(PageParam.builder()
                        .pageNo(0)
                        .pageSize(9999)
                        .build())
                .filter(FilterEntity.builder()
                        .classType("logic")
                        .logicOperator("AND")
                        .filters(filters.toArray())
                        .build())
                .sortFields(null)
                .build();
        DubboResult<ArrayList<Task>> taskHistoryResult = infodatorService.getTaskHistoryList(getDriverBase(), queryRequestDto);
        if (taskHistoryResult.success()) {
            ArrayList<Task> taskHistoryList = taskHistoryResult.getObject();
        }
        System.out.println("执行结果：" + taskHistoryResult);
        Assert.assertNotNull(taskHistoryResult);
    }

    @Test
    public void getJobStatus(){
        DubboResult<TaskStatus> taskStatusResult = infodatorService.getJobStatus(getDriverBase(), "29856");
        System.out.println("执行结果：" + taskStatusResult);
        Assert.assertNotNull(taskStatusResult);
    }

    @Test
    public void addRobot(){
        AddRobotRequestDto robotRequestDto = AddRobotRequestDto.builder()
                .agentName("guoway_test")
                .agentDesc("guoway_test")
//                .activateType(null)
                .machineCode("00:50:56:8C:57:B3")
                .ip("160.22.4.251")
//                .features(Lists.newArrayList("LIVE_SHOW", "RECORD"))
                .osInfo("Microsoft Windows 10 企业版 LTSC;10.0.17763;64 位")
                .clientVersion("9.8.0.14080")
                .build();

        System.out.println("执行入参：" + robotRequestDto);
        DubboResult<RpaRobot> robotResult = infodatorService.addRobot(getDriverBase(), robotRequestDto);
        System.out.println("执行结果：" + robotResult);
        Assert.assertNotNull(robotResult);
    }
}