package com.jeelowcode.service.bpm.config.framework.portal.core;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.jeelowcode.framework.plus.core.toolkit.StringUtils;
import com.jeelowcode.service.bpm.config.framework.portal.core.dto.*;
import com.jeelowcode.tool.framework.common.util.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import static com.jeelowcode.service.bpm.config.framework.portal.core.PortalRequestConstant.*;

/**
 * 描述：请求门户信息
 *
 * @author shelly
 */
@Slf4j
public class PortalRequest {

    /**
     * 门户域名，例如 xops.chinasatnet.com.cn 或者 192.168.1.1:8083
     */
    private final String domain;

    /**
     * 门户协议，http 或者 https
     */
    private final String protocol;

    /**
     * 系统编码
     */
    private final String sysCode;

    /**
     * 构造函数
     *
     * @param domain   门户域名
     * @param protocol 门户协议
     */
    public PortalRequest(String sysCode, String domain, String protocol) {
        this.sysCode = sysCode;
        this.domain = domain;
        this.protocol = protocol;
    }

    /**
     * 获取门户基础URL地址
     *
     * @return 基础URL地址，格式为"协议://域名"
     */
    public String getBaseUrl() {
        return protocol + "://" + domain;
    }

    /**
     * 获取门户请求URL地址
     *
     * @param path 请求路径
     * @return 请求URL地址
     */
    public String getRequestUrl(String path) {
        return getBaseUrl() + path;
    }

    /**
     * 初始化Http请求
     *
     * @param url 请求URL地址
     * @return Http请求
     */
    private static HttpRequest initHttpRequest(String url, Object data) {
        return HttpRequest.post(url)
                .header(Header.ACCEPT, "*/*")
                .header(Header.CONNECTION, "Keep-Alive")
                .header(Header.CONTENT_TYPE, "application/json")
                .body(JsonUtils.toJsonString(data), "application/json");
    }

    /**
     * 接收待办流程
     */
    public PortalTodoResponseDTO receiveTodoRequest(ReceiveTodoRequestDTO requestDTO) {
        requestDTO.setSysCode(this.sysCode);
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_RECEIVE_TODO_REQUEST_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 接收待阅流程
     */
    public PortalTodoResponseDTO receiveCCRequest(ReceiveCCRequestDTO requestDTO) {
        requestDTO.setSysCode(this.sysCode);
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_RECEIVE_CC_REQUEST_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 处理待办流程，变成已办
     */
    public PortalTodoResponseDTO processDoneRequest(ProcessDoneRequestDTO requestDTO) {
        requestDTO.setSysCode(this.sysCode);
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_PROCESS_DONE_REQUEST_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 处理办结流程，变成办结
     */
    public PortalTodoResponseDTO processOverRequest(ProcessOverRequestDTO requestDTO) {
        requestDTO.setSysCode(this.sysCode);
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_PROCESS_OVER_REQUEST_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 接收异构系统流程
     */
    public PortalTodoResponseDTO receiveRequestInfo(ReceiveRequestInfoDTO requestDTO) {
        requestDTO.setSysCode(this.sysCode);
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_RECEIVE_REQUEST_INFO_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 删除异构系统流程
     */
    public PortalTodoResponseDTO deleteRequestInfo(DeleteRequestInfoDTO requestDTO) {
        requestDTO.setSysCode(this.sysCode);
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_DELETE_REQUEST_INFO_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 批量删除异构系统流程
     */
    public PortalTodoResponseDTO deleteUserRequestInfo(DeleteUserRequestInfoDTO requestDTO) {
        requestDTO.setSysCode(this.sysCode);
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_DELETE_USER_REQUEST_INFO_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 批量删除异构系统流程
     */
    public PortalTodoResponseDTO batchDeleteUserRequestInfo(List<DeleteUserRequestInfoDTO> requestDTO) {
        requestDTO.forEach(request -> request.setSysCode(this.sysCode));
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_BATCH_DELETE_USER_REQUEST_INFO_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 批量接收异构系统流程
     */
    public PortalTodoResponseDTO batchReceiveRequestInfo(List<ReceiveRequestInfoDTO> requestDTO) {
        requestDTO.forEach(request -> request.setSysCode(this.sysCode));
        HttpRequest httpRequest = initHttpRequest(getRequestUrl(PATH_BATCH_RECEIVE_REQUEST_INFO_BY_JSON), requestDTO);
        return JsonUtils.parseObject(httpRequest.execute().body(), PortalTodoResponseDTO.class);
    }

    /**
     * 检查当前PortalRequest实例是否可用
     *
     * @return 当sysCode、domain和protocol都不为空时返回true，否则返回false
     */
    public boolean available() {
        return !StringUtils.isBlank(sysCode) && !StringUtils.isBlank(domain) && !StringUtils.isBlank(protocol);
    }

}
