package cn.skcks.docking.gb28181.wvp.sip.request;

import cn.skcks.docking.gb28181.core.sip.gb28181.constant.GB28181Constant;
import cn.skcks.docking.gb28181.core.sip.message.MessageHelper;
import cn.skcks.docking.gb28181.core.sip.utils.SipUtil;
import cn.skcks.docking.gb28181.wvp.config.ProxySipConfig;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDocking;
import gov.nist.javax.sip.message.MessageFactoryImpl;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import javax.sip.SipFactory;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.header.*;
import javax.sip.message.Request;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@DependsOn("proxySipConfig")
@Component
public class SipRequestBuilder implements ApplicationContextAware {
    private static ProxySipConfig sipConfig;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        sipConfig = applicationContext.getBean(ProxySipConfig.class);
    }

    private static SipFactory getSipFactory(){
        return SipFactory.getInstance();
    }

    @SneakyThrows
    private static List<ViaHeader> getViaHeaders(String ip,int port, String transport, String viaTag){
        ViaHeader viaHeader = getSipFactory().createHeaderFactory().createViaHeader(ip, port, transport, viaTag);
        viaHeader.setRPort();
        return Collections.singletonList(viaHeader);
    }

    @SneakyThrows
    private static CSeqHeader getCSeqHeader(long cSeq, String method){
        return getSipFactory().createHeaderFactory().createCSeqHeader(cSeq, method);
    }

    @SneakyThrows
    public static Request createMessageRequest(WvpProxyDocking device, String ip, int port, long cSeq, String content, String fromTag, CallIdHeader callIdHeader) {
        Request request;
        String target = StringUtils.joinWith(":", device.getIp(), device.getPort());
        // sip uri
        SipURI requestURI = MessageHelper.createSipURI(device.getGbDeviceId(), target);

        // via
        List<ViaHeader> viaHeaders = getViaHeaders(ip, port, sipConfig.getTransport(), null );

        String from = StringUtils.joinWith(":", ip, port);
        // from
        SipURI fromSipURI = MessageHelper.createSipURI(device.getGbDeviceId(), from);
        Address fromAddress = MessageHelper.createAddress(fromSipURI);
        FromHeader fromHeader = MessageHelper.createFromHeader(fromAddress, fromTag);
        // to
        SipURI toSipURI = MessageHelper.createSipURI(device.getGbDeviceId(), target);
        Address toAddress = MessageHelper.createAddress(toSipURI);
        ToHeader toHeader = MessageHelper.createToHeader(toAddress, null);

        // Forwards
        MaxForwardsHeader maxForwards = MessageHelper.createMaxForwardsHeader(70);
        // ceq
        CSeqHeader cSeqHeader = getSipFactory().createHeaderFactory().createCSeqHeader(cSeq, Request.MESSAGE);

        // 使用 GB28181 默认编码 否则中文将会乱码
        MessageFactoryImpl messageFactory = (MessageFactoryImpl) getSipFactory().createMessageFactory();
        messageFactory.setDefaultContentEncodingCharset(GB28181Constant.CHARSET);
        request = messageFactory.createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader,
                toHeader, viaHeaders, maxForwards);

        request.addHeader(SipUtil.createUserAgentHeader());

        ContentTypeHeader contentTypeHeader = getSipFactory().createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml");
        request.setContent(content, contentTypeHeader);
        return request;
    }

    @SneakyThrows
    public static Request createMessageRequest(WvpProxyDocking device, String ip, int port,long cSeq,String content, String viaTag, String fromTag, CallIdHeader callIdHeader) {
        Request request;
        String target = StringUtils.joinWith(":", device.getIp(), device.getPort());
        // sip uri
        SipURI requestURI = MessageHelper.createSipURI(device.getGbDeviceId(), target);

        // via
        List<ViaHeader> viaHeaders = getViaHeaders(ip, port, sipConfig.getTransport(), viaTag );

        String from = StringUtils.joinWith(":", ip, port);
        // from
        SipURI fromSipURI = MessageHelper.createSipURI(device.getGbDeviceId(), from);
        Address fromAddress = MessageHelper.createAddress(fromSipURI);
        FromHeader fromHeader = MessageHelper.createFromHeader(fromAddress, fromTag);
        // to
        SipURI toSipURI = MessageHelper.createSipURI(sipConfig.getId(), target);
        Address toAddress = MessageHelper.createAddress(toSipURI);
        ToHeader toHeader = MessageHelper.createToHeader(toAddress, null);

        // Forwards
        MaxForwardsHeader maxForwards = MessageHelper.createMaxForwardsHeader(70);
        // ceq
        CSeqHeader cSeqHeader = getSipFactory().createHeaderFactory().createCSeqHeader(cSeq, Request.MESSAGE);

        // 使用 GB28181 默认编码 否则中文将会乱码
        MessageFactoryImpl messageFactory = (MessageFactoryImpl) getSipFactory().createMessageFactory();
        messageFactory.setDefaultContentEncodingCharset(GB28181Constant.CHARSET);
        request = messageFactory.createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader,
                toHeader, viaHeaders, maxForwards);

        request.addHeader(SipUtil.createUserAgentHeader());

        ContentTypeHeader contentTypeHeader = getSipFactory().createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml");
        request.setContent(content, contentTypeHeader);
        return request;
    }
}
