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

import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.request.inspection.MissionStatusRequest;
import com.tuoheng.common.core.utils.HttpUtils;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Locale;

@Slf4j
@Service
public class DspServiceImpl implements IDspService {

    @Autowired
    private StartAIService startAIService;

    @Autowired
    private StopAIService stopAIService;

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

    @Override
    public 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);
        return JSONObject.parseObject(result);
    }

    @Override
    public JSONObject serviceStopApplication(JSONObject jsonObject) {
        String url = dspDomainUrl + "/api/web/serviceInst/" + jsonObject.getString("requestId") + "/stop";
        String result = HttpUtils.doSend(url, jsonObject, null, "PUT");
        return JSONObject.parseObject(result);
    }

    @Override
    public JsonResult startAI(MissionStatusRequest missionStatusRequest, String dspServiceInstId) {
        return startAIService.startAI(missionStatusRequest, dspServiceInstId);
    }

    @Override
    public JsonResult stopAI(String msgId, String inspectionId) {
        return stopAIService.stopAI(msgId, inspectionId);
    }
}
