package com.tuoheng.admin.service.third.dsp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.enums.code.inspection.EditInspectionStatusCodeEnum;
import com.tuoheng.admin.entity.request.inspection.MissionStatusRequest;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Locale;

@Slf4j
@Service
public class StartAIService {

    /**
     * DSP服务API接口域名
     */
    @Value("${tuoheng.dsp-domain-url:}")
    private String dspDomainUrl;

    @Value("${tuoheng.dsp-callback-url:}")
    private String dspCallbackUrl;

    /**
     * 调用DSP接口
     *
     * @param missionStatusRequest
     * @return
     */
    public JsonResult startAI(MissionStatusRequest missionStatusRequest, String dspServiceInstId) {
        log.info("进入调用DSP接口方法, missionStatusRequest={}", missionStatusRequest.getId());
        log.info("进入调用DSP接口方法, dspServiceInstId={}", dspServiceInstId);
        JSONObject request = new JSONObject();
        JSONArray configList = new JSONArray();
        JSONObject instConfigMode = new JSONObject();
        instConfigMode.put("ename", "service_mode");
        instConfigMode.put("evalue", "1");
        configList.add(instConfigMode);
        JSONObject instConfigUrl = new JSONObject();
        instConfigUrl.put("ename", "callback_url");
        instConfigUrl.put("evalue", dspCallbackUrl);
        configList.add(instConfigUrl);
        request.put("serviceInstConfigList", configList);

        JSONArray reqList = new JSONArray();

        JSONObject instReqUrlPush = new JSONObject();
        instReqUrlPush.put("ename", "push_url");
        instReqUrlPush.put("evalue", missionStatusRequest.getPushUrl());
        reqList.add(instReqUrlPush);

        JSONObject instReqUrlPull = new JSONObject();
        instReqUrlPull.put("ename", "pull_url");
        instReqUrlPull.put("evalue", missionStatusRequest.getPullUrl());
        reqList.add(instReqUrlPull);

        request.put("serviceInstReqList", reqList);
        JSONObject responseJson = this.serviceInstApplication(request, dspServiceInstId);
        if (responseJson.getIntValue("code") == 0) {
            String requestId = responseJson.getJSONObject("data").getString("requestId");
            log.info("dsp实时调用响应requestId：{}", requestId);
        } else {
            log.error("dsp实时调用失败：{}", responseJson);
            return JsonResult.error(EditInspectionStatusCodeEnum.TASK_ANALYSIS_FAILED.getCode(), EditInspectionStatusCodeEnum.TASK_ANALYSIS_FAILED.getMsg());
        }
        return JsonResult.success(responseJson);
    }

    private JSONObject serviceInstApplication(JSONObject jsonObject, String dspServiceInstId) {
        String url = String.format(Locale.ENGLISH, "%s/api/web/serviceInst/%s/application", dspDomainUrl, dspServiceInstId);
        log.info("调用DSP服务, url:{}", url);
        log.info("调用DSP服务, jsonObject:{}", jsonObject);
        String result = SendPost.doPost(url, jsonObject, null);
        log.info("调用DSP服务, result:{}", result);
        String stringStr = "post请求失败！";
        if(result.equals(stringStr)){
            log.info("调用dsp服务, 处理post请求失败");
            throw new SecurityException("调用dsp服务,处理post请求失败");
        }
        return JSONObject.parseObject(result);
    }

}
