package com.jpsoft.ehome.callback;

import com.sun.jna.Pointer;
import com.jpsoft.ehome.device.EHomeDeviceManager;
import com.jpsoft.ehome.sdk.HCISUPCMS;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FRegisterCallBack implements HCISUPCMS.DEVICE_REGISTER_CB {
    private String alarmServerIP;
    private short alarmServerUdpPort;
    private short alarmServerTcpPort;
    private short pictureServerPort;
    private DeviceRegisterCallBack deviceRegisterCallBack;

    public FRegisterCallBack(String serverIP, short alarmServerUdpPort, short alarmServerTcpPort, short pictureServerPort){
        this.alarmServerIP = serverIP;
        this.alarmServerUdpPort = alarmServerUdpPort;
        this.alarmServerTcpPort = alarmServerTcpPort;
        this.pictureServerPort = pictureServerPort;
    }

    public FRegisterCallBack(String serverIP, short alarmServerUdpPort, short alarmServerTcpPort, short pictureServerPort,DeviceRegisterCallBack deviceRegisterCallBack){
        this.alarmServerIP = serverIP;
        this.alarmServerUdpPort = alarmServerUdpPort;
        this.alarmServerTcpPort = alarmServerTcpPort;
        this.pictureServerPort = pictureServerPort;
        this.deviceRegisterCallBack = deviceRegisterCallBack;
    }

    @Override
    public boolean invoke(int lUserID, int dwDataType, Pointer pOutBuffer, int dwOutLen, Pointer pInBuffer, int dwInLen, Pointer pUser)
    {
        log.warn("FRegisterCallBack,dwDataType:" + dwDataType);

        try {
            String deviceNo = "";

            switch (dwDataType) {
                case 0:  //ENUM_DEV_ON
                    HCISUPCMS.NET_EHOME_DEV_REG_INFO_V12 strDevRegInfo = new HCISUPCMS.NET_EHOME_DEV_REG_INFO_V12();

                    strDevRegInfo.write();
                    Pointer pDevRegInfo = strDevRegInfo.getPointer();
                    pDevRegInfo.write(0, pOutBuffer.getByteArray(0, strDevRegInfo.size()), 0, strDevRegInfo.size());
                    strDevRegInfo.read();

                    deviceNo = new String(strDevRegInfo.struRegInfo.byDeviceID).trim();

                    HCISUPCMS.NET_EHOME_SERVER_INFO_V50 strEhomeServerInfo = new HCISUPCMS.NET_EHOME_SERVER_INFO_V50();
                    strEhomeServerInfo.read();

                    //strEhomeServerInfo.dwSize = strEhomeServerInfo.size();
                    try {
                        byte[] byIP = this.alarmServerIP.getBytes("UTF-8");

                        System.arraycopy(byIP, 0, strEhomeServerInfo.struUDPAlarmSever.szIP, 0, byIP.length);
                        System.arraycopy(byIP, 0, strEhomeServerInfo.struTCPAlarmSever.szIP, 0, byIP.length);

                        //告知客户端报警服务器外网IP及端口
                        strEhomeServerInfo.dwAlarmServerType = 0; //报警服务器类型：0- 只支持UDP协议上报，1- 支持UDP、TCP两种协议上报 2-MQTT
                        strEhomeServerInfo.struTCPAlarmSever.wPort = this.alarmServerTcpPort;
                        strEhomeServerInfo.struUDPAlarmSever.wPort = this.alarmServerUdpPort;

                        //告知客户端存储服务器外网IP及端口
                        strEhomeServerInfo.dwPicServerType = 3;
                        System.arraycopy(byIP, 0, strEhomeServerInfo.struPictureSever.szIP, 0, byIP.length);
                        strEhomeServerInfo.struPictureSever.wPort = this.pictureServerPort;

                        strEhomeServerInfo.dwKeepAliveSec = 15; //心跳间隔
                        strEhomeServerInfo.dwTimeOutCount = 6;

                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                    strEhomeServerInfo.write();
                    dwInLen = strEhomeServerInfo.size();
                    pInBuffer.write(0, strEhomeServerInfo.getPointer().getByteArray(0, dwInLen), 0, dwInLen);

                    log.warn("设备上线!" + deviceNo + ",服务器端编号：" + lUserID);

                    if (deviceRegisterCallBack!=null) {
                        deviceRegisterCallBack.register(deviceNo, lUserID, true);
                    }

                    EHomeDeviceManager.add(deviceNo, lUserID);

                    break;
                case 1: //ENUM_DEV_OF
                    //查询当前对应lUserID的deviceNo
                    deviceNo = EHomeDeviceManager.findDeviceNoByLoginID(lUserID);

                    if (deviceRegisterCallBack!=null && deviceNo!=null) {
                        deviceRegisterCallBack.register(deviceNo, lUserID, false);
                    }

                    log.warn("设备下线!" + deviceNo + ",服务器端编号：" + lUserID);
                    EHomeDeviceManager.remove(deviceNo);
                    break;
                default:
                    System.out.println("FRegisterCallBack default type:" + dwDataType);
                    break;
            }
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }

        return true;
    }
}