package com.sjyt.icom.asst.service.impl;

import cn.hutool.core.util.StrUtil;
import com.sjyt.icom.asst.common.model.message.VerifyClaims;
import com.sjyt.icom.asst.common.constant.SrsConstant;
import com.sjyt.icom.asst.common.util.JjwtUtil;
import com.sjyt.icom.asst.conf.properties.RtspAppProperties;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>StreamUrl地址创建服务</p>
 *
 * @author renyanru
 * @version V1.0.0
 * @date 2022/6/17 14:29
 */
@Service
@AllArgsConstructor
public class StreamUrlServiceService extends BaseStreamUrlService{
    private final RtspAppProperties rtspAppProperties;

    @Override
    public String initialRtcUrl(VerifyClaims verifyClaims) {
       return rtspAppProperties.getWebrtc().concat(SrsConstant.APP_NAME)
                .concat(Objects.requireNonNull(creatRtcUrl(verifyClaims))) ;
    }

    @Override
    public String initialRtmpUrl(VerifyClaims verifyClaims) {
        return null;
    }

    @Override
    public String initialHttpFlvUrl(VerifyClaims verifyClaims) {
        return null;
    }

    @Override
    public String initialHttpsFlvUrl(VerifyClaims verifyClaims) {
        return null;
    }

    @Override
    public String initialHttpsHlsUrl(VerifyClaims verifyClaims) {
        return null;
    }

    /**
     * example:
     * /rid/uid?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
     * @param verifyClaims  verifyClaims
     * @return r
     */
    private  String creatRtcUrl(VerifyClaims verifyClaims) {
        String urlSuffix = rtspAppProperties.getTokenName();
         final StringBuilder URL_BUFFER = new StringBuilder(2048);
            if (StrUtil.isBlankIfStr(urlSuffix)) {
                urlSuffix = "token";
            }
            final String jwt = JjwtUtil.createJWT(null, verifyClaims);
            return URL_BUFFER.append("/").append(verifyClaims.getRid()).append("/").append(verifyClaims.getFrom()).append("?")
                    .append(urlSuffix).append("=").append(jwt).toString();
    }
}
