package com.sz.admin.monitor.utils;


import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sz.admin.monitor.sdk.hkSdk.HCNetSDK;

import java.nio.charset.Charset;

/**
 * @author zxsong
 * @date 2026/11/31
 * 描述：检查摄像头是否支持3D PTZ
 */
public class Hik3DptzChecker {

    private static final int DEVICE_SOFTHARDWARE_ABILITY = 0x001;

    public static boolean isSupport3DPTZ(HCNetSDK sdk, int lUserID) {
        int outSize = 64 * 1024;
        Memory outBuf = new Memory(outSize);
        outBuf.clear();

        IntByReference pRetLen = new IntByReference(0);

        boolean ok = sdk.NET_DVR_GetDeviceAbility(
                lUserID,
                DEVICE_SOFTHARDWARE_ABILITY,
                Pointer.NULL,
                0,
                outBuf,
                outSize
        );

        if (!ok) {
            return false;
        }

        int realLen = pRetLen.getValue();
        if (realLen <= 0) realLen = cStringLen(outBuf, outSize);

        byte[] bytes = outBuf.getByteArray(0, realLen);

        String xml = new String(bytes, Charset.forName("UTF-8")).trim();
        if (xml.isEmpty() || !xml.contains("Support3DPTZ")) {
            xml = new String(bytes, Charset.forName("GBK")).trim();
        }

        return xml.contains("<Support3DPTZ>1</Support3DPTZ>")
                || xml.contains("<Support3DPTZ>true</Support3DPTZ>");
    }

    private static int cStringLen(Memory mem, int max) {
        for (int i = 0; i < max; i++) {
            if (mem.getByte(i) == 0) return i;
        }
        return max;
    }
}