#include <QDebug>
#include <QFile>
#include <QDir>
#include "finger.h"
#include "config.h"
#include <QImage>
#include <QBuffer>

extern config *pconfig;

finger::finger(QObject *parent) : QThread(parent)
{

}

void finger::slot_set_thread_stop(void)
{
    this->threadstatus = false;
}

void finger::slot_fingerWork(bool stat)
{
    finger_workStat = stat;
    // if(stat == true)
    // {
    //     if(pconfig->fingerStat == true)
    //     {
    //         workstat = fingerWorkStat::adcquireFingerprint;
    //     }
    // }
    // else{
    //     if(pconfig->fingerStat == true)
    //     {
    //         workstat = fingerWorkStat::sleep;
    //     }
    // }
}

void finger::finger_toImage(unsigned char *data)
{
    QByteArray byteArray(reinterpret_cast<const char*>(data), m_imgFPWidth * m_imgFPHeight);

    QImage::Format format = QImage::Format_Grayscale8;

    // 将字节流转换为 QImage
    QImage image(reinterpret_cast<const uchar*>(byteArray.constData()), m_imgFPWidth, m_imgFPHeight, format);

    m_pBmpSize = image.bytesPerLine();

    QBuffer buffer(&m_pBmpBuf);
    buffer.open(QIODevice::WriteOnly);

    image.save(&buffer, "BMP");
}

void finger::finger_init(void)
{
    int stat = ZKFPM_Init(); // 调用DLL中的函数
    if(stat == ZKFP_ERR_OK || stat == ZKFP_ERR_ALREADY_INIT)
    {
        fingerInit = true;
        workstat = fingerWorkStat::opendev;
    }
}

void finger::finger_opendev(void)
{
    int count = ZKFPM_GetDeviceCount();
    qDebug() << "zkfpm_init success, dev count:"<< count;
    if(count > 0)
    {
        fingerHandle = ZKFPM_OpenDevice(0);
        qDebug() << "open fingerdev: "<< fingerHandle;
        if(fingerHandle != NULL)
        {
            m_nFakeFunOn = 1;
            ZKFPM_SetParameters(fingerHandle, 2002, (unsigned char *)&m_nFakeFunOn, 4);
            ZKFPM_SetParameters(fingerHandle, 104, (unsigned char *)&m_nFakeFunOn, 4);

            unsigned int size = 4;
            ZKFPM_GetParameters(fingerHandle, 1, (unsigned char *)&m_imgFPWidth, &size);
            ZKFPM_GetParameters(fingerHandle, 2, (unsigned char *)&m_imgFPHeight, &size);
            ZKFPM_GetParameters(fingerHandle, 3, (unsigned char *)&m_impDpi, &size);
            m_pImgBuf = new unsigned char[m_imgFPWidth * m_imgFPHeight];
            qDebug() << "zkfpm_OpenDevice success";
            pconfig->fingerStat = true;
            workstat = fingerWorkStat::adcquireFingerprint;
        }
    }
}

void finger::finger_acquirefingerprint(void)
{
    szFingerLen = MAX_TEMPLATE_SIZE;
    int ret = ZKFPM_AcquireFingerprint(fingerHandle, m_pImgBuf, m_imgFPWidth * m_imgFPHeight, szFinger, &szFingerLen);
    if(ret == ZKFP_ERR_OK)
    {
        if(finger_workStat == true)
        {
            finger_toImage(m_pImgBuf);
            emit signal_fingerStat(true, m_pBmpBuf, m_pBmpSize);
            workstat = fingerWorkStat::adcquireFingerprint;
        }
        qDebug() << "zkfpm_AcquireFingerprint success:" << szFingerLen;
    }
    // else{
    //     qDebug() << "zkfpm_AcquireFingerprint fail:" << ret;
    //     emit signal_fingerStat(ret, m_pBmpBuf, m_pBmpSize);
    //     workstat = (finger_workStat == true)?fingerWorkStat::adcquireFingerprint:fingerWorkStat::sleep;
    // }
}

void finger::run()
{
    while(this->threadstatus)
    {
        switch(workstat)
        {
        case fingerWorkStat::init:finger_init();break;
        case fingerWorkStat::opendev:finger_opendev();break;
        case fingerWorkStat::adcquireFingerprint:finger_acquirefingerprint();break;
        default:break;
        }

        msleep(100);
    }

    if(fingerHandle != NULL)
    {
        ZKFPM_CloseDevice(fingerHandle);
    }

    if(fingerInit == true)
    {
        ZKFPM_Terminate();
    }

    // if(workstat != fingerWorkStat::loadDll)
    // {
    //     // 卸载库
    //     library->unload(); // 卸载DLL
    // }

    qDebug() << "finger thread exit!";
}
