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

import com.sjyt.icom.asst.common.event.SrsCallBackEvent;
import com.sjyt.icom.asst.common.model.srs.*;
import com.sjyt.icom.asst.service.ISrsCallBack;

import java.util.Optional;

/**
 * <p>回调服务基类</p>
 *
 * @author renyanru
 * @version V1.0.0
 * @date 2022/6/24 10:56
 */
public abstract class BaseSrsCallBackService implements ISrsCallBack {
    /**
     * 公共属性转化
     * @param obj obj
     * @return r
     */
    protected SrsCallBackEvent baseConvert(SrsBaseCallBack obj) {
        SrsCallBackEvent srsCallBackEvent = new SrsCallBackEvent(obj);
        return Optional.ofNullable(obj).map(it -> {
            //base
            srsCallBackEvent.setAction(obj.getAction());
            srsCallBackEvent.setApp(obj.getApp());
            srsCallBackEvent.setIp(obj.getIp());
            srsCallBackEvent.setParam(obj.getParam());
            srsCallBackEvent.setServerId(obj.getServer_id());
            srsCallBackEvent.setClientId(obj.getClient_id());
            srsCallBackEvent.setVhost(obj.getVhost());
            srsCallBackEvent.setStream(obj.getStream());
            return srsCallBackEvent;
        }).orElse(null);
    }

    /**
     * 连接事件对象转化
     *
     * @param obj obj
     * @return SrsCallBackEvent
     */
    protected SrsCallBackEvent convertConnect(SrsCallBackOnConnect obj) {
        SrsCallBackEvent srsCallBackEvent = baseConvert(obj);
        return Optional.ofNullable(srsCallBackEvent).map((it) -> {
            //particular
            it.setEventType(1);
            it.setPageUrl(obj.getPageUrl());
            it.setTcUrl(obj.getTcUrl());
            return it;
        }).orElse(null);
    }

    /**
     * 关闭事件对象转化
     *
     * @param obj obj
     * @return SrsCallBackEvent
     */
    protected SrsCallBackEvent convertClose(SrsCallBackOnClose obj) {
        SrsCallBackEvent srsCallBackEvent = baseConvert(obj);
        return Optional.ofNullable(srsCallBackEvent).map((it) -> {
            //particular
            it.setEventType(2);
            it.setSendBytes(obj.getSend_bytes());
            it.setRecvBytes(obj.getRecv_bytes());
            return it;
        }).orElse(null);

    }

    /**
     * 推流事件对象转化
     *
     * @param obj obj
     * @return SrsCallBackEvent
     */
    protected SrsCallBackEvent convertUnPublish(SrsCallBackOnPublish obj) {
        SrsCallBackEvent srsCallBackEvent = baseConvert(obj);
        return Optional.ofNullable(srsCallBackEvent).map((it) -> {
            //particular
            it.setEventType(3);
            it.setTcUrl(obj.getTcUrl());
            return it;
        }).orElse(null);
    }

    /**
     * 停止推流事件对象转化
     *
     * @param obj obj
     * @return SrsCallBackEvent
     */
    protected SrsCallBackEvent convertUnPublish(SrsCallBackOnUnPublish obj) {
        SrsCallBackEvent srsCallBackEvent = baseConvert(obj);
        srsCallBackEvent.setEventType(4);
        return srsCallBackEvent;
    }

    /**
     * 拉流事件对象转化
     *
     * @param obj obj
     * @return SrsCallBackEvent
     */
    protected SrsCallBackEvent convertPlay(SrsCallBackOnPlay obj) {
        SrsCallBackEvent srsCallBackEvent = baseConvert(obj);
        return Optional.ofNullable(srsCallBackEvent).map((it) -> {
            //particular
            it.setPageUrl(obj.getPageUrl());
            it.setEventType(5);
            return it;
        }).orElse(null);
    }

    /**
     * 停止拉流事件对象转化
     *
     * @param obj obj
     * @return SrsCallBackEvent
     */
    protected SrsCallBackEvent convertStop(SrsCallBackOnStop obj) {
        SrsCallBackEvent srsCallBackEvent = baseConvert(obj);
        srsCallBackEvent.setEventType(6);
        return srsCallBackEvent;
    }
}
