package com.friday.tooldriver.constants.infodator;

import com.alibaba.fastjson.TypeReference;
import com.friday.tooldriver.dto.infodator.response.*;
import com.friday.tooldriver.dto.infodator.response.Process;
import org.springframework.http.HttpMethod;

import java.util.List;
import java.util.Map;

public enum InfodatorApiEnum {
    // 登录相关
    LOGIN_KEY(
            "/login/getKey",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<LoginResponseDto>>(){},
            InfodatorApiErrorCode.LOGIN_KEY_ERROR),
    LOGIN(
            "/login/login",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<LoginResponseDto>>(){},
            InfodatorApiErrorCode.CREATE_TOKEN_ERROR),
    // 组相关
    GROUP_ADD(
            "/norm/group/add",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<GroupResponseDto>>(){},
            InfodatorApiErrorCode.GROUP_ADD_ERROR),
    GROUP_INFO(
            "/norm/group/{groupId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<GroupResponseDto>>(){},
            InfodatorApiErrorCode.GROUP_INFO_ERROR),
    GROUP_GET_ALL_NAME(
            "/norm/group/getAllGroupNames/{uuid}",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<GroupResponseDto>>>(){},
            InfodatorApiErrorCode.GROUP_GET_ALL_NAME_ERROR),
    GROUP_DELETE(
            "/norm/group/delete?groupId={groupId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<GroupResponseDto>>(){},
            InfodatorApiErrorCode.GROUP_DELETE_ERROR),
    GROUP_DELETE_REMOVE(
            "/norm/group/deleteAndRemove?groupId={groupId}&newGroupId={newGroupId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.GROUP_DELETE_REMOVE_ERROR),
    GROUP_UPDATE(
            "/norm/group/update/{groupId}",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<GroupResponseDto>>(){},
            InfodatorApiErrorCode.GROUP_UPDATE_ERROR),
    // 用户相关
    USER_ADD(
            "/norm/user/add",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<UserResponseDto>>(){},
            InfodatorApiErrorCode.USER_ADD_ERROR),
    USER_PASSWORD_CHANGE(
            "/norm/user/changePassword",
            HttpMethod.POST,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.USER_PASSWORD_CHANGE_ERROR),
    USER_PASSWORD_CHANGE_ADMIN(
            "/norm/user/changePasswordByAdmin",
            HttpMethod.POST,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.USER_PASSWORD_CHANGE_ADMIN_ERROR),
    USER_CHANGE(
            "/norm/user/changeUser",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<UserResponseDto>>(){},
            InfodatorApiErrorCode.USER_CHANGE_ERROR),
    USER_CONFORM_PROTOCOL(
            "/norm/user/conformProtocol",
            HttpMethod.POST,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.USER_CONFORM_PROTOCOL_ERROR),
    USER_DELETE(
            "/norm/user/delete?userId={userId}",
            HttpMethod.POST,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.USER_DELETE_ERROR),
    USER_FIND_INFO_BY_TOKEN(
            "/norm/user/findUserInfo",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<UserResponseDto>>(){},
            InfodatorApiErrorCode.USER_FIND_INFO_BY_TOKEN_ERROR),
    USER_INFO(
            "/norm/user/get/{userId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<UserResponseDto>>(){},
            InfodatorApiErrorCode.USER_INFO_ERROR),
    USER_LIST(
            "/norm/user/list",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<UserResponseDto>>>(){},
            InfodatorApiErrorCode.USER_LIST_ERROR),
    USER_RESET_SECRET(
            "/norm/user/resetSecret",
            HttpMethod.POST,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.USER_RESET_SECRET_ERROR),
    // 任务相关
    TASK_ADD(
            "/task/add",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_ADD_ERROR),
    TASK_ADD_EASY(
            "/task/addByEasyParam",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_ADD_EASY_ERROR),
    TASK_START(
            "/task/start?taskId={taskId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<List<Integer>>>(){},
            InfodatorApiErrorCode.TASK_START_ERROR),
    TASK_COLLECT(
            "/task/collect",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_COLLECT_ERROR),
    TASK_COLLECT_CANCEL(
            "/task/collectCancel",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_COLLECT_CANCEL_ERROR),
    TASK_DELETE(
            "/task/delete",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_DELETE_ERROR),
    TASK_UPDATE(
            "/task/update",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_UPDATE_ERROR),
    TASK_DETAIL(
            "/task/detailInfo",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_DETAIL_ERROR),
    TASK_DOWNLOAD(
            "/task/download?logicFileId={logicFileId}",
            HttpMethod.GET,
            String.class,
            InfodatorApiErrorCode.TASK_DOWNLOAD_ERROR),
    TASK_UPLOAD(
            "/task/upload",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_UPLOAD_ERROR),
    TASK_INFO(
            "/task/info?taskId={taskId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Task>>(){},
            InfodatorApiErrorCode.TASK_INFO_ERROR),
    TASK_LIST(
            "/task/list",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<Task>>>(){},
            InfodatorApiErrorCode.TASK_LIST_ERROR),
    TASK_HISTORY_LIST(
            "/taskExecutionHistory/list",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<Task>>>(){},
            InfodatorApiErrorCode.TASK_HISTORY_LIST_ERROR),
    // 任务队列相关
    TASK_QUEUE_ADD_RECORD(
            "/taskQueue/addRecord",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<Task>>>(){},
            InfodatorApiErrorCode.TASK_QUEUE_ADD_RECORD_ERROR),
    TASK_QUEUE_CANCEL(
            "/taskQueue/cancel?taskExecutionQueueId={taskExecutionQueueId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.TASK_QUEUE_CANCEL_ERROR),
    TASK_QUEUE_DELETE_FROM_QUEUE(
            "/taskQueue/deleteFromQueue?taskExecutionQueueId={taskExecutionQueueId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.TASK_QUEUE_DELETE_FROM_QUEUE_ERROR),
    TASK_QUEUE_MOVE_FROM_QUEUE(
            "/taskQueue/moveFromQueue?afterId={afterId}&beforeId={beforeId}&taskExecutionQueueId={taskExecutionQueueId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.TASK_QUEUE_MOVE_FROM_QUEUE_ERROR),
    TASK_QUEUE_BATCH_DELETE_FROM_QUEUE(
            "/taskQueue/deleteFromQueueByList",
            HttpMethod.POST,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.TASK_QUEUE_BATCH_DELETE_FROM_QUEUE_ERROR),
    TASK_QUEUE_GET_TASK_ERROR_INFO(
            "/taskQueue/getTaskErrorInfo?taskQueueId={taskQueueId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.TASK_QUEUE_GET_TASK_ERROR_INFO_ERROR),
    TASK_QUEUE_INFO(
            "/taskQueue/info?taskQueueId={taskQueueId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<TaskStatus>>(){},
            InfodatorApiErrorCode.TASK_QUEUE_INFO_ERROR),
    TASK_QUEUE_LIST(
            "/taskQueue/list",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<TaskStatus>>>(){},
            InfodatorApiErrorCode.TASK_QUEUE_LIST_ERROR),
    TASK_QUEUE_QUEUE_LIST(
            "/taskQueue/queueList",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<Task>>>(){},
            InfodatorApiErrorCode.TASK_QUEUE_QUEUE_LIST_ERROR),
    TASK_PAUSE(
            "/taskQueue/pause?taskExecutionQueueId={taskExecutionQueueId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.TASK_PAUSE_ERROR),
    TASK_RESUME(
            "/taskQueue/restore?taskExecutionQueueId={taskExecutionQueueId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto>(){},
            InfodatorApiErrorCode.TASK_RESUME_ERROR),
    // 流程相关
    PROCESS_UPLOAD(
            "/templates/upload",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<UploadFileResponseDto>>(){},
            InfodatorApiErrorCode.PROCESS_UPLOAD_ERROR),
    PROCESS_ADD(
            "/templates/add",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<com.friday.tooldriver.dto.infodator.response.Process>>(){},
            InfodatorApiErrorCode.PROCESS_ADD_ERROR),
    PROCESS_ADD_VERSION(
            "/templates/addVersion",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<com.friday.tooldriver.dto.infodator.response.Process>>(){},
            InfodatorApiErrorCode.PROCESS_ADD_VERSION_ERROR),
    PROCESS_DELETE(
            "/templates/delete?templateId={templateId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Integer>>(){},
            InfodatorApiErrorCode.PROCESS_DELETE_ERROR),
    PROCESS_UPDATE(
            "/templates/update/{templateId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<com.friday.tooldriver.dto.infodator.response.Process>>(){},
            InfodatorApiErrorCode.PROCESS_UPDATE_ERROR),
    PROCESS_UPDATE_VERSION(
            "/templates/updateVersion",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<com.friday.tooldriver.dto.infodator.response.Process>>(){},
            InfodatorApiErrorCode.PROCESS_UPDATE_VERSION_ERROR),
    PROCESS_CHANGE_DEFAULT(
            "/templates/changeDefault?templateId={templateId}&versionId={versionId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<com.friday.tooldriver.dto.infodator.response.Process>>(){},
            InfodatorApiErrorCode.PROCESS_CHANGE_DEFAULT_ERROR),
    PROCESS_HISTORY_VERSION(
            "/templates/historyVersion",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<HistoryVersionDto>>>(){},
            InfodatorApiErrorCode.PROCESS_HISTORY_VERSION_ERROR),
    PROCESS_INFO(
            "/templates/info?templateId={templateId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<com.friday.tooldriver.dto.infodator.response.Process>>(){},
            InfodatorApiErrorCode.PROCESS_INFO_ERROR),
    PROCESS_LIST(
            "/templates/list",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<com.friday.tooldriver.dto.infodator.response.Process>>>(){},
            InfodatorApiErrorCode.PROCESS_LIST_ERROR),
    PACKAGE_DELETE(
            "",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<ResponseDto<Process>>>(){},
            InfodatorApiErrorCode.PACKAGE_DELETE_ERROR),
    // robot相关
    ROBOT_ADD(
            "/norm/robot/agent/add",
            HttpMethod.POST,
            String.class,
            InfodatorApiErrorCode.ROBOT_ADD_ERROR),
    ROBOT_DELETE(
            "/norm/robot/agent/delete?agentId={agentId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Robot>>(){},
            InfodatorApiErrorCode.ROBOT_DELETE_ERROR),
    ROBOT_UPDATE(
            "/norm/robot/update/{robotId}",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<RpaRobot>>(){},
            InfodatorApiErrorCode.ROBOT_UPDATE_ERROR),
    ROBOT_LIST(
            "/norm/robot/agent/list",
            HttpMethod.POST,
            new TypeReference<DataResponseDto<ResponseDto<Robot>>>(){},
            InfodatorApiErrorCode.ROBOT_LIST_ERROR),
    ROBOT_LIVE_INFO(
            "/norm/robot/agent/liveBot/findById?id={id}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Robot>>(){},
            InfodatorApiErrorCode.ROBOT_LIVE_INFO_ERROR),
    ROBOT_INFO(
            "/norm/robot/get/{robotId}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Robot>>(){},
            InfodatorApiErrorCode.ROBOT_INFO_ERROR),
    ROBOT_CHANGE_STATUS(
            "/norm/robot/changeApproveStatus?robotId={robotId}&approveStatus={approveStatus}",
            HttpMethod.GET,
            new TypeReference<DataResponseDto<Robot>>(){},
            InfodatorApiErrorCode.ROBOT_CHANGE_STATUS_ERROR),

    ;
    private String url;
    private HttpMethod httpMethod;
    private Class responseClass;
    private TypeReference responseTypeReference;
    private InfodatorApiErrorCode systemCode;

    InfodatorApiEnum(String url, HttpMethod httpMethod, Class responseClass, InfodatorApiErrorCode systemCode) {
        this.url = url;
        this.httpMethod = httpMethod;
        this.responseClass = responseClass;
        this.systemCode = systemCode;
    }

    InfodatorApiEnum(String url, HttpMethod httpMethod, TypeReference responseTypeReference, InfodatorApiErrorCode systemCode) {
        this.url = url;
        this.httpMethod = httpMethod;
        this.responseTypeReference = responseTypeReference;
        this.systemCode = systemCode;
    }

    public String getUrl(String baseUrl, Map<String, Object> uriParams) {
        StringBuilder sb = new StringBuilder();
        sb.append(baseUrl);
        if (!baseUrl.equals("/") && !this.url.startsWith("/")) {
            sb.append("/");
        }
        String baseUri = this.url;
        if (uriParams != null) {
            for(Map.Entry<String, Object> entry : uriParams.entrySet()){
                String uri = "{" + entry.getKey() + "}";
                if (baseUri.contains(uri)) {
                    baseUri = baseUri.replace(uri, entry.getValue().toString());
                }
            }
        }
        sb.append(baseUri);
        return sb.toString();
    }

    public String getUrl(String baseUrl) {
        return getUrl(baseUrl, null);
    }

    public HttpMethod getHttpMethod() {
        return this.httpMethod;
    }

    public <T> Class<T> getResponseClass() {
        return this.responseClass;
    }

    public <T> TypeReference<T> getResponseTypeReference() {
        return this.responseTypeReference;
    }

    public InfodatorApiErrorCode getSystemCode() {
        return this.systemCode;
    }
}
