import ctypes
import os
import threading
import time
from ctypes import *


from components.hikrobot_sc import mv_lib


# Define the necessary structures and constants from MvVisionSensorDefine.h


def main():
    global MV_VS_OK
    print("SC2016 TEST")

    device_info_list = mv_lib.MV_VS_DEVICE_INFO_LIST()

    nRet = mv_lib.MV_VS_EnumDevices(ctypes.byref(device_info_list))

    if nRet != MV_VS_OK:
        print(f"Enum devices failed! nRet [{nRet:#x}]")
        return -2
    else:
        print("Enum devices OK")

    if device_info_list.nDeviceNum > 0:
        mv_lib.print_device_info(device_info_list)
    else:
        print("Find No Devices!")
        return -2

    if device_info_list.nDeviceNum == 2:
        print("Starting dual camera test!")
        # Define MV_VS_OK (replace with the actual value)
        MV_VS_OK = 0
        MV_VS_DEVICE_INFO_1 = device_info_list.pDeviceInfo[0]
        MV_VS_DEVICE_INFO_2 = device_info_list.pDeviceInfo[1]
        pHandle1 = c_void_p()
        pHandle2 = c_void_p()
        nRet1 = mv_lib.MV_VS_CreateHandle(byref(pHandle1), MV_VS_DEVICE_INFO_1)
        nRet2=0
        nRet2 = mv_lib.MV_VS_CreateHandle(byref(pHandle2), MV_VS_DEVICE_INFO_2)
        if nRet1 != MV_VS_OK or nRet2 != MV_VS_OK:
            print(f"Create Handle failed! [{nRet1:#x} {nRet2:#x}]")
            return

        time.sleep(0.1)
        nRet1 = mv_lib.MV_VS_Login(pHandle1, b"Admin", b"Abc1234")
        time.sleep(0.1)
        nRet2 = mv_lib.MV_VS_Login(pHandle2, b"Admin", b"Abc1234")
        if nRet1 != MV_VS_OK or nRet2 != MV_VS_OK:
            print(f"Login failed! [{nRet1&0xFFFFFFFF:#x} {nRet2&0xFFFFFFFF:#x}]")
            return
        else:
            print("Login OK")

        nRet1 = mv_lib.MV_VS_SetBoolValue(pHandle1, b"CommandImageMode", 0)
        nRet2 = mv_lib.MV_VS_SetBoolValue(pHandle2, b"CommandImageMode", 0)
        if nRet1 != MV_VS_OK or nRet2 != MV_VS_OK:
            print(f"CommandImageMode failed! [{nRet1&0xFFFFFFFF:#x} {nRet2&0xFFFFFFFF:#x}]")
            return
        else:
            print("CommandImageMode OK")

        nRet1 = mv_lib.MV_VS_SetEnumValue(pHandle1, b"AcquisitionMode", 0)
        nRet2 = mv_lib.MV_VS_SetEnumValue(pHandle2, b"AcquisitionMode", 0)
        if nRet1 != MV_VS_OK or nRet2 != MV_VS_OK:
            print(f"AcquisitionMode failed! [{nRet1&0xFFFFFFFF:#x} {nRet2&0xFFFFFFFF:#x}]")
            return
        else:
            print("AcquisitionMode OK")

        # Start the camera test
        nRet1 = mv_lib.MV_VS_StartRun(pHandle1)
        nRet2 = mv_lib.MV_VS_StartRun(pHandle2)

        if nRet1 != MV_VS_OK or nRet2 != MV_VS_OK:
            print(f"Start run failed! [{nRet1 & 0xFFFFFFFF:#x} {nRet2 & 0xFFFFFFFF:#x}]")

        stMemory = mv_lib.PROCESS_MEMORY_COUNTERS()
        ctypes.windll.psapi.GetProcessMemoryInfo(
            ctypes.windll.kernel32.GetCurrentProcess(), ctypes.byref(stMemory), ctypes.sizeof(stMemory)
        )

        # Get the current time
        current_time = time.localtime()

        # Print the information
        print(
            f"Start test Get/Release result data, begin time [{current_time.tm_hour + 8:02d}:{current_time.tm_min:02d}:{current_time.tm_sec:02d}], "
            f"current memory [{stMemory.WorkingSetSize / 1024} KB]"
        )

        while True:
            print("main loop cycle")

            # Create the thread
            thread = threading.Thread(target=mv_lib.MV_VS_GetFrame, args=(pHandle1, "dx"))
            thread2 = threading.Thread(target=mv_lib.MV_VS_GetFrame, args=(pHandle2, "sx"))

            # Start the thread
            thread.start()
            thread2.start()

            # Trigger acquisition & test execution
            nRet1 = mv_lib.MV_VS_SetCommandValue(pHandle1, b"AcquisitionStart")
            nRet2 = mv_lib.MV_VS_SetCommandValue(pHandle2, b"AcquisitionStart")
            if nRet1 != MV_VS_OK or nRet2 != MV_VS_OK:
                print(f"AcquisitionStart failed! [{nRet1 & 0xFFFFFFFF:#x} {nRet2 & 0xFFFFFFFF:#x}]")
            else:
                print("AcquisitionStart OK")
            time.sleep(1)

    return 0


if __name__ == "__main__":
    main()