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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sjyt.icom.asst.common.enums.MsgTypeEnum;
import com.sjyt.icom.asst.common.model.message.VerifyClaims;
import com.sjyt.icom.asst.common.model.room.SrsUrlObj;
import com.sjyt.icom.asst.common.model.sdp.AnswerDto;
import com.sjyt.icom.asst.common.model.sdp.FutureAnswer;
import com.sjyt.icom.asst.common.model.sdp.OfferDTO;
import com.sjyt.icom.asst.common.model.sdp.RespAnswer;
import com.sjyt.icom.asst.conf.properties.RtspAppProperties;
import com.sjyt.icom.asst.service.ISrsRpcService;
import com.sjyt.icom.asst.service.httpapi.SrsHttpApi;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
 * SrsRpc远程调用
 *
 * @author: Tab
 * @date: 2022-06-11 22:49
 * @since 1.0.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class SrsRpcService implements ISrsRpcService {
    private final RtspAppProperties config;
    private final SrsHttpApi srsApi;

    private final StreamUrlServiceService streamUrlServiceService;

    /**
     * 推拉流sdp
     *
     * @param dto offer
     * @return r
     */
    @Override
    public CompletableFuture<FutureAnswer> sdp(final SrsRpcOfferDTO dto) {
        //推拉 or 拉流
        final VerifyClaims claims = BeanUtil.copyProperties(dto, VerifyClaims.class);
        final SrsUrlObj srsUrlObj = streamUrlServiceService.init(claims);
        return Optional.ofNullable(srsUrlObj.getRtcPublishPullUrl())
                .map(it -> {
                    final OfferDTO offerDTO = BeanUtil.copyProperties(dto, OfferDTO.class);
                    offerDTO.setClientip(dto.getClientIp());
                    offerDTO.setStreamurl(it);
                    AnswerDto answerDto;
                    if (ObjectUtil.equal(MsgTypeEnum.ReqSponsor.CODE, dto.getMsgCode())) {
                        offerDTO.setApi(config.getUrl().concat(SrsHttpApi.PUBLISH));
                        answerDto = srsApi.publish(offerDTO);
                    } else {
                        offerDTO.setApi(config.getUrl().concat(SrsHttpApi.PULL));
                        answerDto = srsApi.pull(offerDTO);
                    }
                    return answerDto;
                }).map(it -> {
                    log.info("返回数据：answer 状态码为：{}", it.getCode());
                    //answerDto to RespAnswer
                    RespAnswer respAnswer = covertAnswer(it, srsUrlObj);
                    FutureAnswer build = null;
                    if (ObjectUtil.isNotNull(respAnswer.getSdp()) && ObjectUtil.isNotNull(respAnswer.getSessionId())) {
                        build = FutureAnswer.builder().code(0).respAnswer(respAnswer).cmd(dto.getCmd()).from(dto.getFrom()).to(dto.getTo()).build();
                    }
                    return CompletableFuture.completedFuture(build);
                }).orElseGet(() -> {
                    FutureAnswer build = FutureAnswer.builder().code(-1).cmd(dto.getCmd()).from(dto.getFrom()).to(dto.getTo()).build();
                    return CompletableFuture.completedFuture(build);
                });
    }

    /**
     * answerDto 转 RespAnswer
     *
     * @param answerDto answerDto
     * @param url       url
     * @return RespAnswer
     */
    private RespAnswer covertAnswer(AnswerDto answerDto, SrsUrlObj url) {
        return RespAnswer.builder().server(Objects.requireNonNull(answerDto).getServer())
                .sdp(answerDto.getSdp()).urlObj(url).sessionId(answerDto.getSessionid())
                .msgType(MsgTypeEnum.RespAnswer.getCODE()).build();
    }
}
