package com.jpsoft.ehome.callback;

import com.sun.jna.Pointer;
import com.jpsoft.ehome.sdk.HCISUPAlarm;
import com.jpsoft.ehome.xml.XmlParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class AlarmMsgCallBack implements HCISUPAlarm.EHomeMsgCallBack {
    private MsgNoticeCallBack msgNoticeCallBack;

    public AlarmMsgCallBack(){

    }

    public AlarmMsgCallBack(MsgNoticeCallBack msgNoticeCallBack){
        this.msgNoticeCallBack = msgNoticeCallBack;
    }

    @Override
    public boolean invoke(int iHandle, HCISUPAlarm.NET_EHOME_ALARM_MSG pAlarmMsg, Pointer pUser) {
        log.warn("AlarmMsgCallBack: " + pAlarmMsg.dwAlarmType + ", dwAlarmInfoLen:" + pAlarmMsg.dwAlarmInfoLen + "dwXmlBufLen:" + pAlarmMsg.dwXmlBufLen);

        String strXML = "";

        if(pAlarmMsg.dwXmlBufLen !=0) {
            HCISUPAlarm.BYTE_ARRAY strXMLData = new HCISUPAlarm.BYTE_ARRAY(pAlarmMsg.dwXmlBufLen);
            strXMLData.write();
            Pointer pAlarmInfo = strXMLData.getPointer();
            pAlarmInfo.write(0, pAlarmMsg.pXmlBuf.getByteArray(0, strXMLData.size()), 0, strXMLData.size());
            strXMLData.read();

            strXML = new String(strXMLData.byValue);
            log.warn(strXML);
        }

        if (pAlarmMsg.dwAlarmType == 11){
            log.warn("门禁事件上传");

            if(StringUtils.isNotEmpty(strXML))
            {
                if (strXML.indexOf("PicDataUrl")!=-1) {
                    //读取卡号、温度、截图url
                    try {
                        String picDataUrl = XmlParser.selectNodeValue(strXML, null,"/PPVSPMessage/Params/PicDataUrl");

                        /*
                        <PicDataUrl>Failed-to-upload-picture</PicDataUrl>
                        <thermalURL>http://122.189.98.152:7562/kms/services/rest/dataInfoService/downloadFile?id=BE3956003EC90B546847F66E23DB960A</thermalURL>
                        */
                        if ("Failed-to-upload-picture".equals(picDataUrl)){
                            picDataUrl = XmlParser.selectNodeValue(strXML, null,"/PPVSPMessage/Params/thermalURL");
                            log.warn("大图上传失败!改用热力图,{}", picDataUrl);
                        }

                        if ("Failed-to-upload-picture".equals(picDataUrl)){
                            log.warn("大图及热力图都上传失败!,{}",picDataUrl);
                            return true;
                        }

                        String deviceID = XmlParser.selectNodeValue(strXML, null,"/PPVSPMessage/Params/DeviceID");
                        String cardNo = XmlParser.selectNodeValue(strXML, null,"/PPVSPMessage/Params/CardNo");

                        if (StringUtils.isEmpty(cardNo)){
                            cardNo = "0";
                        }

                        String temperature = XmlParser.selectNodeValue(strXML, null,"/PPVSPMessage/Params/currTemperature");
                        String time = XmlParser.selectNodeValue(strXML, null,"/PPVSPMessage/Params/Time");

                        if (msgNoticeCallBack != null) {
                            msgNoticeCallBack.notice(deviceID, cardNo, temperature, picDataUrl, time);
                        }
                    } catch (Exception ex) {
                        log.error(ex.getMessage(), ex);
                    }
                }
            }
        }

        return true;
    }
}
