package com.yfd.monitor.gdw2019.transmit.event.request.impl.message.query.cmd_parent;

import com.yfd.monitor.gdw2019.bean.Device;
import com.yfd.monitor.gdw2019.bean.ParentPlatform;
import com.yfd.monitor.gdw2019.transmit.SIPSender;
import com.yfd.monitor.gdw2019.transmit.cmd.impl.SIPCommanderFroPlatform;
import com.yfd.monitor.gdw2019.transmit.event.request.SIPRequestProcessorParent;
import com.yfd.monitor.gdw2019.transmit.event.request.impl.message.IMessageHandler;
import com.yfd.monitor.gdw2019.transmit.event.request.impl.message.query.QueryMessageByparentHandler;
import com.yfd.monitor.storager.IVideoManagerStorage;
import gov.nist.javax.sip.message.SIPRequest;
import gov.nist.javax.sip.message.SIPResponse;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sip.InvalidArgumentException;
import javax.sip.RequestEvent;
import javax.sip.SipException;
import javax.sip.header.FromHeader;
import java.text.ParseException;

@Component
public class AlarmQueryByParentMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {

    private Logger logger = LoggerFactory.getLogger(AlarmQueryByParentMessageHandler.class);
    private final String cmdType = "Request_History_Alarm";

    @Autowired
    private QueryMessageByparentHandler queryMessageHandler;


    @Autowired
    private SIPCommanderFroPlatform cmderFroPlatform;

    @Autowired
    private SIPSender sipSender;

    @Override
    public void afterPropertiesSet() throws Exception {
        queryMessageHandler.addHandler(cmdType, this);
    }

    @Override
    public void handForDevice(RequestEvent evt, Device device, Element element) {

    }

    @Override
    public void handForPlatform(RequestEvent evt, ParentPlatform parentPlatform, Element rootElement) throws InvalidArgumentException, ParseException, SipException {
        //处理上级系统发出的目录查询要求
        FromHeader fromHeader = (FromHeader) evt.getRequest().getHeader(FromHeader.NAME);
        Element itemElement = rootElement.element("Item");
        String Code = itemElement.attributeValue("Code");
        String UserCode = itemElement.attributeValue("UserCode");
        String Type = itemElement.attributeValue("Type");
        String BeginTime = itemElement.attributeValue("BeginTime");
        String EndTime = itemElement.attributeValue("EndTime");
        String Level = itemElement.attributeValue("Level");
        String FromIndex = itemElement.attributeValue("FromIndex");
        String ToIndex = itemElement.attributeValue("ToIndex");
        String  resultXml=cmderFroPlatform.queryHistoryAlarm(parentPlatform,Code,UserCode,Type,BeginTime,EndTime,Level,FromIndex,ToIndex);
        SIPRequest request = (SIPRequest) evt.getRequest();
        SIPResponse response = responseXmlAck(request, resultXml, parentPlatform, 0);
        sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response);
    }

}
