package thyyxxk.webserver.service.externalhttp;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import thyyxxk.webserver.config.envionment.ApiUrl;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;

@Service
@RequiredArgsConstructor
public class ThirdpartApiService {
    private final RestTemplate template;
    private final ApiUrl apiUrl;

    public String getSpeechUrl(String text, String id) {
        String url = apiUrl.getThirdpartApi() + "/ttsApi/textToSpeech";
        JSONObject body = new JSONObject();
        body.put("text", text);
        body.put("id", id);
        ResultVo<String> response = template.exchange(
                url, HttpMethod.POST, new HttpEntity<>(body),
                new ParameterizedTypeReference<ResultVo<String>>() {
                }
        ).getBody();
        if (null == response) {
            throw new BizException(ExceptionEnum.NETWORK_ERROR);
        }
        return response.getData();
    }

}
