#include "InteractiveHand.h"

InteractiveHand::InteractiveHand() : QQuickImageProvider(QQuickImageProvider::Image)
{
    initHandle();

    m_switch = 0;
    // 图像取1s 1帧
    m_timerId = startTimer(1000);
}

InteractiveHand::~InteractiveHand()
{
    destroyHandle();
}

void InteractiveHand::setFingerImageAppearCallBack(QJSValue callback)
{
    m_imageAppearCallback = callback;
}

void InteractiveHand::fingerImagePlay()
{
    m_switch = 1;
}

void InteractiveHand::fingerImageStop()
{
    m_switch = 0;
}

void InteractiveHand::initHandle()
{
    m_templateSize = MAX_TEMPLATE_SIZE;
    m_templateData.resize(MAX_TEMPLATE_SIZE);

    int zkfpErr = ZKFPM_Init(); // 调用DLL中的函数
    if(zkfpErr == ZKFP_ERR_OK || zkfpErr == ZKFP_ERR_ALREADY_INIT)
    {
        int count = ZKFPM_GetDeviceCount();
        if(count > 0)
        {
            m_handle = ZKFPM_OpenDevice(0);

            if(m_handle != NULL)
            {
                unsigned int uArgs = 1;
                unsigned int uSize = 4;
                ZKFPM_SetParameters(m_handle, 2002, (unsigned char *)&uArgs, 4);
                ZKFPM_SetParameters(m_handle, 104,  (unsigned char *)&uArgs, 4);

                // 获取宽高
                ZKFPM_GetParameters(m_handle, 1, (unsigned char *)&m_imageW, &uSize);
                ZKFPM_GetParameters(m_handle, 2, (unsigned char *)&m_imageH, &uSize);
                // ZKFPM_GetParameters(m_handle, 3, (unsigned char *)&m_impDpi, &uSize);

                m_imageData.resize(m_imageW * m_imageH);

                m_isInit = true;
            }
        }
    }
}

void InteractiveHand::destroyHandle()
{
    if(m_handle != NULL)
    {
        ZKFPM_CloseDevice(m_handle);
        m_isInit = false;
    }

    ZKFPM_Terminate();
}

QString InteractiveHand::getImageUrl()
{
    // image://InteractiveHandImage/(0-9999)
    return QString("image://") +
           QString(INTERACTIVE_HAND_IMAGE_URL) +
           QString("/") +
           // 取随机数，确保图片刷新(Qt机制，两次url地址相同，图片不会显示)
           QString::number(QRandomGenerator::global()->bounded(10000));
}

QImage InteractiveHand::requestImage(const QString &, QSize *, const QSize &)
{
    return m_image;
}

void InteractiveHand::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == m_timerId)
    {
        if (m_switch && m_isInit)
        {
            unsigned int templateSize = m_templateSize;
            int zkfpErr = ZKFPM_AcquireFingerprint(m_handle,
                                                   reinterpret_cast<unsigned char *>(m_imageData.data()),
                                                   static_cast<unsigned int>(m_imageData.size()),
                                                   reinterpret_cast<unsigned char *>(m_templateData.data()),
                                                   &templateSize);
            if(zkfpErr == ZKFP_ERR_OK)
            {
                QImage::Format format = QImage::Format_Grayscale8;

                // 将字节流转换为 QImage
                m_image = QImage(reinterpret_cast<const uchar*>(m_imageData.constData()),
                                 m_imageW,
                                 m_imageH,
                                 QImage::Format_Grayscale8);

                if (m_imageAppearCallback.isCallable())
                {
                    QJSValueList args;
                    args << getImageUrl();
                    m_imageAppearCallback.call(args);
                }
            }

        }
    }
}

// 获取图像
QImage InteractiveHand::getImage()
{
    return m_image.copy();
}
