package com.qqchen.deploy.backend.workflow.delegate;

import com.qqchen.deploy.backend.deploy.enums.JenkinsBuildStatus;
import com.qqchen.deploy.backend.deploy.integration.IJenkinsServiceIntegration;
import com.qqchen.deploy.backend.deploy.integration.response.JenkinsQueueBuildInfoResponse;
import com.qqchen.deploy.backend.workflow.constants.WorkFlowConstants;
import com.qqchen.deploy.backend.workflow.dto.definition.node.localVariables.DeployNodeLocalVariables;
import com.qqchen.deploy.backend.workflow.dto.definition.node.localVariables.NotificationNodeLocalVariables;
import com.qqchen.deploy.backend.workflow.dto.definition.node.panelVariables.DeployNodePanelVariables;
import com.qqchen.deploy.backend.workflow.dto.definition.node.panelVariables.NotificationNodePanelVariables;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.BpmnError;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Shell脚本任务的委派者实现
 */
@Slf4j
@Component
public class NotificationNodeDelegate extends BaseNodeDelegate<NotificationNodePanelVariables, NotificationNodeLocalVariables> {

    private final RestTemplate restTemplate = new RestTemplate();

    private final String WX_HOOK_API = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=614b110b-8957-4be8-95b9-4eca84c15028";

    // 用于存储实时输出的Map
    private static final Map<String, StringBuilder> outputMap = new ConcurrentHashMap<>();

    private static final Map<String, StringBuilder> errorMap = new ConcurrentHashMap<>();

    @Override
    protected Class<NotificationNodePanelVariables> getPanelVariablesClass() {
        return NotificationNodePanelVariables.class;
    }

    @Override
    protected Class<NotificationNodeLocalVariables> getLocalVariablesClass() {
        return NotificationNodeLocalVariables.class;
    }

    @Override
    protected void executeInternal(DelegateExecution execution, NotificationNodePanelVariables panelVariables, NotificationNodeLocalVariables localVariables) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String format = String.format("{\n" +
            "  \"msgtype\": \"text\",\n" +
            "  \"text\": {\n" +
            "    \"content\": \"%s\"\n" +
            "  }\n" +
            "}", panelVariables.getText() + ":这是一个面板填写的变量");

        HttpEntity<String> entity = new HttpEntity<>(format, headers);

        restTemplate.exchange(
            WX_HOOK_API,
            HttpMethod.POST,
            entity,
            String.class
        );
    }

}
