package com.mortals.xhx.module.idCard;

import com.mortals.xhx.common.osSelect;
import com.mortals.xhx.module.idCard.service.TMZParseSDK;
import com.mortals.xhx.module.idCard.service.TMZSDK;
import com.sun.jna.Native;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TMZMain {

    public static TMZSDK tmzSDK = null;
    public static TMZParseSDK tmzParseSDK = null;

    /**
     * 根据不同操作系统选择不同的库文件和库路径
     *
     * @return
     */
    public static boolean createSDKInstance() {
        if (tmzSDK == null) {
            synchronized (TMZSDK.class) {
                String strDllPath = "";
                try {
                    //System.setProperty("jna.debug_load", "true");
               /*     if (osSelect.isWindows())
                        //win系统加载库路径
                        strDllPath = System.getProperty("user.dir") + "\\lib\\tmz\\libSDTReader.so";
                    else if (osSelect.isLinux())*/
                        //Linux系统加载库路径
                    strDllPath = System.getProperty("user.dir") + "/lib/tmz/libSDTReader.so";
                    tmzSDK = Native.loadLibrary(strDllPath, TMZSDK.class);
                    strDllPath = System.getProperty("user.dir") + "/lib/tmz/libwlt.so";
                    tmzParseSDK = Native.loadLibrary(strDllPath, TMZParseSDK.class);
                } catch (Exception ex) {
                    log.error("loadLibrary: " + strDllPath + " Error: " + ex.getMessage());
                    return false;
                }
            }
        }
        return true;
    }


    public static void main(String[] args) {
        if (tmzSDK == null) {
            if (!createSDKInstance()) {
                System.out.println("Load SDK fail");
                return;
            }
        }

        //linux系统建议调用以下接口加载组件库
        if (osSelect.isLinux()) {
            TMZSDK.BYTE_ARRAY ptrByteArray1 = new TMZSDK.BYTE_ARRAY(256);
            TMZSDK.BYTE_ARRAY ptrByteArray2 = new TMZSDK.BYTE_ARRAY(256);
            //这里是库的绝对路径，请根据实际情况修改，注意改路径必须有访问权限
            String strPath1 = System.getProperty("user.dir") + "/lib/libcrypto.so.1.1";
            String strPath2 = System.getProperty("user.dir") + "/lib/libssl.so.1.1";
/*
            System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());
            ptrByteArray1.write();
            hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());

            System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());
            ptrByteArray2.write();
            hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());

            String strPathCom = System.getProperty("user.dir")+"/lib/";
            HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
            System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
            struComPath.write();
            hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());*/
        }
    }

}
