package cn.iocoder.yudao.module.pms.service.yanfan.sip;

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.utils.ZlmApiUtils;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.media.YfMediaServerDO;
import cn.iocoder.yudao.module.pms.service.yanfan.NullSipProvider;
import cn.iocoder.yudao.module.pms.service.yanfan.config.SysSipConfig;
import cn.iocoder.yudao.module.pms.service.yanfan.media.YfMediaServerService;
import cn.iocoder.yudao.module.pms.service.yanfan.sip.config.IotSipConfigService;
import cn.iocoder.yudao.module.pms.service.yanfan.sip.model.ZlmMediaServer;
import gov.nist.javax.sip.SipStackImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;

import javax.sip.*;
import java.util.Properties;


@Slf4j
@Configuration
@Order(13)
public class SipLayer {
    @Autowired
    private SysSipConfig sipConfig;

    @Autowired
    private IGBListener gbSIPListener;

    @Autowired
    private ZlmApiUtils zlmApiUtils;

    @Autowired
    private ISipCacheService sipCacheService;

    @Autowired
    private IotSipConfigService sipConfigService;

    @Autowired
    private YfMediaServerService mediaServerService;
    private SipStack sipStack;
    private SipFactory sipFactory;


    @Bean("sipFactory")
    SipFactory createSipFactory() {
        sipFactory = SipFactory.getInstance();
        sipFactory.setPathName("gov.nist");
        if (sipConfig.isEnabled()) {
            //缓存zlm服务器配置
            YfMediaServerDO media = mediaServerService.selectMediaServerBytenantId(1L);
            if (media != null) {
                ZlmMediaServer mediaServer = zlmApiUtils.getMediaServerConfig(media);
                if (mediaServer != null) {
                    log.info("zlm配置获取成功成功...");
                    sipCacheService.updateMediaInfo(mediaServer);
                    mediaServerService.syncMediaServer(media, media.getSecret());
                }
            }
        }
        return sipFactory;
    }

    @Bean("sipStack")
    @DependsOn("sipFactory")
    SipStack createSipStack() throws PeerUnavailableException {
        Properties properties = new Properties();
        properties.setProperty("javax.sip.STACK_NAME", "GB28181_SIP");
        properties.setProperty("javax.sip.IP_ADDRESS", sipConfig.getIp());
        properties.setProperty("gov.nist.javax.sip.LOG_MESSAGE_CONTENT", "false");
        properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "0");
        properties.setProperty("gov.nist.javax.sip.SERVER_LOG", "sip_server_log");
        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", "sip_debug_log");
        sipStack = (SipStackImpl) sipFactory.createSipStack(properties);
        return sipStack;
    }

    @Bean("udpSipServer")
    @DependsOn("sipStack")
    SipProvider startUdpListener(IotSipConfigService iotSipConfigService) throws Exception {
        if (sipConfig.isEnabled()) {
            log.info("startUdpListener");
            ListeningPoint udpListeningPoint = sipStack.createListeningPoint(sipConfig.getIp(), sipConfig.getPort(), "UDP");
            SipProvider udpSipProvider = sipStack.createSipProvider(udpListeningPoint);
            udpSipProvider.addSipListener(gbSIPListener);
            log.info("Sip Server UDP 启动成功 port {}", sipConfig.getPort());
            iotSipConfigService.syncSipConfig(sipConfig);
            return udpSipProvider;
        } else {
            return new NullSipProvider();
        }

    }

}
