package com.yfd.monitor.gdw2019.transmit.cmd.impl;

import cn.hutool.core.date.DateUtil;
import com.yfd.monitor.conf.SipConfig;
import com.yfd.monitor.gdw2019.SipLayer;
import com.yfd.monitor.gdw2019.bean.ParentPlatform;
import com.yfd.monitor.gdw2019.bean.SipTransactionInfo;
import com.yfd.monitor.gdw2019.bean.SubstationNode;
import com.yfd.monitor.gdw2019.event.SipSubscribe;
import com.yfd.monitor.gdw2019.transmit.SIPSender;
import com.yfd.monitor.gdw2019.transmit.cmd.ISIPCommanderForNode;
import com.yfd.monitor.gdw2019.transmit.cmd.SIPRequestHeaderPlarformProvider;
import com.yfd.monitor.gdw2019.transmit.cmd.SIPRequestHeaderProvider;
import com.yfd.monitor.gdw2019.utils.SipUtils;
import gov.nist.javax.sip.message.SIPRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.sip.InvalidArgumentException;
import javax.sip.SipException;
import java.text.ParseException;

@Component
@DependsOn("sipLayer")
public class SIPCommanderFroNode implements ISIPCommanderForNode {

    private final Logger logger = LoggerFactory.getLogger(SIPCommanderFroNode.class);

    @Autowired
    private SipConfig sipConfig;

    @Autowired
    private SipLayer sipLayer;

    @Autowired
    private SIPSender sipSender;

    @Autowired
    private SIPRequestHeaderProvider headerProvider;

    @Override
    public void sendTaskToNode(SubstationNode stationnode) throws InvalidArgumentException, ParseException, SipException {
         StringBuffer sipXml = new StringBuffer(200);
        sipXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        sipXml.append("<Notify>\r\n");
        sipXml.append("<CmdType>TaskMessage</CmdType>\r\n");
        sipXml.append("<SendCode>"+ stationnode.getServerId() +"</SendCode>\r\n");
        sipXml.append("<ReceiveCode>"+ stationnode.getStationGBId() +"</ReceiveCode>\r\n");
        sipXml.append("<Code>"+ stationnode.getStationCode() +"</Code>\r\n");
        sipXml.append("<Type>101</Type>\r\n");
        sipXml.append("<Command>1</Command>\r\n");
        sipXml.append("<Time>"+ DateUtil.now() +"</Time>\r\n");
        sipXml.append("<Items></Items>\r\n");
        sipXml.append("</Notify>\r\n");
        SIPRequest request = (SIPRequest) headerProvider.createSationNodeRequest(stationnode, sipXml.toString(), SipUtils.getNewViaTag(), SipUtils.getNewFromTag(), null,sipSender.getNewCallIdHeader(sipLayer.getLocalIp(stationnode.getStationIp()),stationnode.getTransport()));
        sipSender.transmitRequest(sipLayer.getLocalIp(stationnode.getStationIp()),request);
    }
}


