package com.zd.iot.vmp.gb28181.transmit;

import com.zd.iot.vmp.conf.SipConfig;
import com.zd.iot.vmp.gb28181.auth.RegisterLogicHandler;
import com.zd.iot.vmp.gb28181.event.DeviceOffLineDetector;
import com.zd.iot.vmp.gb28181.event.EventPublisher;
import com.zd.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
import com.zd.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
import com.zd.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
import com.zd.iot.vmp.gb28181.transmit.request.ISIPRequestProcessor;
import com.zd.iot.vmp.gb28181.transmit.request.impl.*;
import com.zd.iot.vmp.gb28181.transmit.response.ISIPResponseProcessor;
import com.zd.iot.vmp.gb28181.transmit.response.impl.*;
import com.zd.iot.vmp.media.zlm.ZLMRTPServerFactory;
import com.zd.iot.vmp.service.IDeviceAlarmService;
import com.zd.iot.vmp.service.IMediaServerService;
import com.zd.iot.vmp.service.IPlayService;
import com.zd.iot.vmp.storager.IRedisCatchStorage;
import com.zd.iot.vmp.storager.IVideoManagerStorager;
import com.zd.iot.vmp.utils.SpringBeanFactory;
import com.zd.iot.vmp.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.sip.RequestEvent;
import javax.sip.ResponseEvent;
import javax.sip.SipProvider;
import javax.sip.header.CSeqHeader;
import javax.sip.message.Request;
import javax.sip.message.Response;

/**
 * @Description: SIP信令处理分配
 * @author: dgs
 * @date:   2020年5月3日 下午4:24:37
 */
@Component
public class SIPProcessorFactory {

	// private final static Logger logger = LoggerFactory.getLogger(SIPProcessorFactory.class);

	@Autowired
	private SipConfig sipConfig;

	@Autowired
	private RegisterLogicHandler handler;

	@Autowired
	private IVideoManagerStorager storager;

	@Autowired
	private IRedisCatchStorage redisCatchStorage;

	@Autowired
	private EventPublisher publisher;

	@Autowired
	private SIPCommander cmder;

	@Autowired
	private SIPCommanderFroPlatform cmderFroPlatform;

	@Autowired
	private IDeviceAlarmService deviceAlarmService;

	@Autowired
	private RedisUtil redis;

	@Autowired
	private DeferredResultHolder deferredResultHolder;

	@Autowired
	private DeviceOffLineDetector offLineDetector;

	@Autowired
	private InviteResponseProcessor inviteResponseProcessor;

	@Autowired
	private ByeResponseProcessor byeResponseProcessor;

	@Autowired
	private CancelResponseProcessor cancelResponseProcessor;

	@Autowired
	@Lazy
	private RegisterResponseProcessor registerResponseProcessor;


	@Autowired
	private OtherResponseProcessor otherResponseProcessor;

	@Autowired
	private IPlayService playService;

	@Autowired
	private ZLMRTPServerFactory zlmrtpServerFactory;

	@Autowired
	private IMediaServerService mediaServerService;

	// 注：这里使用注解会导致循环依赖注入，暂用springBean
	private SipProvider tcpSipProvider;

	// 注：这里使用注解会导致循环依赖注入，暂用springBean
	private SipProvider udpSipProvider;

	public ISIPRequestProcessor createRequestProcessor(RequestEvent evt) {
		Request request = evt.getRequest();
		String method = request.getMethod();
//		logger.info("接收到消息："+request.getMethod());
//		sipSubscribe.getSubscribe(evt.getServerTransaction().getBranchId()).response(evt);
		if (Request.INVITE.equals(method)) {
			InviteRequestProcessor processor = new InviteRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setTcpSipProvider(getTcpSipProvider());
			processor.setUdpSipProvider(getUdpSipProvider());

			processor.setCmder(cmder);
			processor.setCmderFroPlatform(cmderFroPlatform);
			processor.setPlayService(playService);
			processor.setStorager(storager);
			processor.setRedisCatchStorage(redisCatchStorage);
			processor.setZlmrtpServerFactory(zlmrtpServerFactory);
			processor.setMediaServerService(mediaServerService);
			return processor;
		} else if (Request.REGISTER.equals(method)) {
			RegisterRequestProcessor processor = new RegisterRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setTcpSipProvider(getTcpSipProvider());
			processor.setUdpSipProvider(getUdpSipProvider());
			processor.setHandler(handler);
			processor.setPublisher(publisher);
			processor.setSipConfig(sipConfig);
			processor.setVideoManagerStorager(storager);
			return processor;
		} else if (Request.SUBSCRIBE.equals(method)) {
			SubscribeRequestProcessor processor = new SubscribeRequestProcessor();
			processor.setTcpSipProvider(getTcpSipProvider());
			processor.setUdpSipProvider(getUdpSipProvider());
			processor.setRequestEvent(evt);
			return processor;
		} else if (Request.ACK.equals(method)) {
			AckRequestProcessor processor = new AckRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setRedisCatchStorage(redisCatchStorage);
			processor.setZlmrtpServerFactory(zlmrtpServerFactory);
			processor.setMediaServerService(mediaServerService);
			return processor;
		} else if (Request.BYE.equals(method)) {
			ByeRequestProcessor processor = new ByeRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setRedisCatchStorage(redisCatchStorage);
			processor.setStorager(storager);
			processor.setZlmrtpServerFactory(zlmrtpServerFactory);
			processor.setSIPCommander(cmder);
			processor.setMediaServerService(mediaServerService);
			return processor;
		} else if (Request.CANCEL.equals(method)) {
			CancelRequestProcessor processor = new CancelRequestProcessor();
			processor.setRequestEvent(evt);
			return processor;
		} else if (Request.MESSAGE.equals(method)) {
			MessageRequestProcessor processor = new MessageRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setTcpSipProvider(getTcpSipProvider());
			processor.setUdpSipProvider(getUdpSipProvider());
			processor.setPublisher(publisher);
			processor.setRedis(redis);
			processor.setDeferredResultHolder(deferredResultHolder);
			processor.setOffLineDetector(offLineDetector);
			processor.setCmder(cmder);
			processor.setCmderFroPlatform(cmderFroPlatform);
			processor.setDeviceAlarmService(deviceAlarmService);
			processor.setStorager(storager);
			processor.setRedisCatchStorage(redisCatchStorage);
			return processor;
		} else if (Request.NOTIFY.equalsIgnoreCase(method)) {
			NotifyRequestProcessor processor = new NotifyRequestProcessor();
			processor.setRequestEvent(evt);
			processor.setTcpSipProvider(getTcpSipProvider());
			processor.setUdpSipProvider(getUdpSipProvider());
			processor.setPublisher(publisher);
			processor.setRedis(redis);
			processor.setDeferredResultHolder(deferredResultHolder);
			processor.setOffLineDetector(offLineDetector);
			processor.setCmder(cmder);
			processor.setStorager(storager);
			processor.setRedisCatchStorage(redisCatchStorage);
			return processor;
		} else {
			OtherRequestProcessor processor = new OtherRequestProcessor();
			processor.setRequestEvent(evt);
			return processor;
		}
	}

	public ISIPResponseProcessor createResponseProcessor(ResponseEvent evt) {

		Response response = evt.getResponse();
		CSeqHeader cseqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
		String method = cseqHeader.getMethod();
		if(Request.INVITE.equals(method)){
			return inviteResponseProcessor;
		} else if (Request.BYE.equals(method)) {
			return byeResponseProcessor;
		} else if (Request.CANCEL.equals(method)) {
			return cancelResponseProcessor;
		}else if (Request.REGISTER.equals(method)) {
			return registerResponseProcessor;
		} else {
			return otherResponseProcessor;
		}
	}

	private SipProvider getTcpSipProvider() {
		if (tcpSipProvider == null) {
			tcpSipProvider = (SipProvider) SpringBeanFactory.getBean("tcpSipProvider");
		}
		return tcpSipProvider;
	}

	private SipProvider getUdpSipProvider() {
		if (udpSipProvider == null) {
			udpSipProvider = (SipProvider) SpringBeanFactory.getBean("udpSipProvider");
		}
		return udpSipProvider;
	}

}
