package net.northking.cctp.upperComputer.driver.usbmuxd.payload.usbmuxd.response;

import com.dd.plist.NSDictionary;
import com.dd.plist.NSObject;
import com.dd.plist.NSString;
import net.northking.cctp.upperComputer.driver.usbmuxd.payload.usbmuxd.entity.DeviceConnectionDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ListenDeviceResponse {
    private DeviceConnectionDetail detail;

    private Logger log = LoggerFactory.getLogger(ListenDeviceResponse.class);
    public ListenDeviceResponse(NSDictionary responseData) {
        if (responseData == null) throw new IllegalArgumentException("responseData不能为null");
        if (!responseData.containsKey("MessageType") || !responseData.containsKey("DeviceID")) {
            log.warn("解析usbmuxd设备变动数据时缺少键：MessageType、DeviceID");
            return;
        }
        NSObject messageTypeObject = responseData.get("MessageType");
        if (!(messageTypeObject instanceof NSString )) {
            log.warn("解析usbmuxd设备变动数据的键：MessageType 类型不为NSString");
            return;
        }
        NSString messageTypeString = (NSString) messageTypeObject;
        DeviceConnectionDetail.ConnectStatus connectStatus = messageTypeString.getContent().equals("Attached") ? DeviceConnectionDetail.ConnectStatus.ATTACHED : DeviceConnectionDetail.ConnectStatus.DETACHED;
        if (connectStatus == DeviceConnectionDetail.ConnectStatus.ATTACHED) {
            NSObject propertiesObject = responseData.get("Properties");
            if (!(propertiesObject instanceof NSDictionary )) {
                log.warn("解析usbmuxd设备变动数据时，键：Properties 类型不为NSDictionary");
                return;
            }
            NSDictionary properties = (NSDictionary) propertiesObject;
            detail = new DeviceConnectionDetail(properties, connectStatus);
        } else {
            detail = new DeviceConnectionDetail(responseData, connectStatus);
        }

    }

    public DeviceConnectionDetail getDetail() {
        return detail;
    }
}
