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

import com.dd.plist.NSArray;
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;

import java.util.ArrayList;

/**
 * 列出所有已连接的设备的响应
 */
public class ListDeviceResponse {

    private Logger log = LoggerFactory.getLogger(ListDeviceResponse.class);
    public final ArrayList<DeviceConnectionDetail> connectionDetails = new ArrayList<>();

    public ListDeviceResponse(NSDictionary responseData) {
        if (responseData == null) throw new IllegalArgumentException("responseData不能为null");
        if (!responseData.containsKey("DeviceList")) return;

        Object deviceListObject = responseData.get("DeviceList");
        if (!(deviceListObject instanceof NSArray)) {
            log.warn("解析usbmuxd连接设备列表数据时，键DeviceList结构类型不为NSArray，跳过整个设备列表解析。");
            return;
        }
        NSArray deviceList = (NSArray) deviceListObject;
        if (deviceList.count() == 0) return;

        for (int i = 0; i < deviceList.count(); i++) {
            Object listObject = deviceList.objectAtIndex(i);
            if (!(listObject instanceof NSDictionary)) continue;
            NSDictionary deviceItem = (NSDictionary) listObject;
            if (!deviceItem.containsKey("MessageType")
                    || !deviceItem.containsKey("DeviceID")
                    || !deviceItem.containsKey("Properties")
            ) {
                log.warn("解析usbmuxd连接设备列表数据时，位于坐标" + i + "的数据缺少键：MessageType、DeviceID、Properties中的至少一个，跳过此项解析。");
                continue;
            }

            NSObject messageTypeObject = deviceItem.get("MessageType");
            if (!(messageTypeObject instanceof NSString)) {
                log.warn("解析usbmuxd连接设备列表数据时，位于坐标" + i + "的数据的键：MessageType 类型不为NSString，跳过此项解析");
                continue;
            }
            NSString messageTypeString = (NSString) messageTypeObject;
            DeviceConnectionDetail.ConnectStatus connectStatus = messageTypeString.getContent().equals("Attached") ? DeviceConnectionDetail.ConnectStatus.ATTACHED : DeviceConnectionDetail.ConnectStatus.DETACHED;

            NSObject propertiesObject = deviceItem.get("Properties");
            if (!(propertiesObject instanceof NSDictionary)) {
                log.warn("解析usbmuxd连接设备列表数据时，位于坐标" + i + "的数据的键：Properties 类型不为NSDictionary，跳过此项解析");
                continue;
            }

            try {
                NSDictionary properties = (NSDictionary) propertiesObject;
                DeviceConnectionDetail connectionDetail = new DeviceConnectionDetail(properties, connectStatus);
                connectionDetails.add(connectionDetail);
            } catch (IllegalArgumentException illegalArgumentException) {
                log.error("解析usbmuxd失败，illegalArgumentException",illegalArgumentException);
                log.warn("解析usbmuxd连接设备列表数据时，位于坐标" + i + "的数据解析失败，跳过此项解析");
            }
        }
    }
}
