package com.inspect.ivs.service;

import com.inspect.ivs.config.XinTongConfig;
import com.inspect.ivs.view.IvsVideoRtspView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;

@Component
public class XinTongComponent extends IvsCommonService {
    private static final Logger log = LoggerFactory.getLogger(XinTongComponent.class);
    private final XinTongConfig xinTongConfig;

    public XinTongComponent(XinTongConfig xinTongConfig) {
        this.xinTongConfig = xinTongConfig;
    }

    public String getAddress() {
        return this.xinTongConfig.getUrl();
    }

    public void convert(String rtspUrl, String streamId, IvsVideoRtspView ivsVideoRtspView) {
        String block = http(HttpMethod.GET, String.format("/Rtsp/StartPullStream?StreamName=%s&RtspUrl=%s", streamId, rtspUrl))
                .header("Accept", new String[]{"*/*"})
                .header("Accept-Encoding", new String[]{"gzip, deflate, br"})
                .header("Connection", new String[]{"keep-alive"})
                .retrieve()
                .bodyToMono(String.class)
                .block();
        log.debug("[GAB] XIN-TONG CONVERT:");
        log.debug("{}", block);
        ivsVideoRtspView.setWebrtcURL("webrtc://" + xinTongConfig.getHost() + "/hls/" + streamId);
    }
}
