package com.tuoheng.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.enums.AiAnalyseStatusEnum;
import com.tuoheng.admin.service.IDspService;
import com.tuoheng.common.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Slf4j
@Service
public class DspServiceImpl implements IDspService {

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

    @Override
    public JSONObject serviceInstApplication(JSONObject jsonObject) {
        String url = dspDomainUrl + "api/web/serviceInst/aaa474c694a2df700cd3a2f2860e12369/application";
        String result = HttpUtils.doSend(url, jsonObject, null,"POST");
        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 JSONArray getQuestionType() {
        String url = dspDomainUrl + "api/web/serviceInst/2/questionTypeList";
        String result = HttpUtils.sendGet(url, "");
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray jsonArray=(JSONArray)jsonObject.get("data");
        if(jsonObject.getIntValue("code") == 0){
            return jsonArray;
        }else {
            log.error("林场的问题数据：{}", jsonObject);
        }
        return jsonArray;
    }
}
