#ifndef LOGINOPTIONSWIDGET_H
#define LOGINOPTIONSWIDGET_H

#include <QWidget>
#include <QMap>
#include "bioauth.h"
#include "biodevices.h"

class QLabel;
class QButtonGroup;
class QHBoxLayout;
class QVBoxLayout;
class QToolButton;
class QTimer;

enum LOGINOPT_TYPE {
    LOGINOPT_TYPE_PASSWORD = 0,         // 密码
    LOGINOPT_TYPE_FACE,                 // 人脸
    LOGINOPT_TYPE_FINGERPRINT,          // 指纹
    LOGINOPT_TYPE_IRIS,                 // 虹膜
    LOGINOPT_TYPE_VOICEPRINT,           // 声纹
    LOGINOPT_TYPE_FINGERVEIN,           // 指静脉
    LOGINOPT_TYPE_QRCODE,               // 二维码
    LOGINOPT_TYPE_OTHERS,               // 其他
    LOGINOPT_TYPE_COUNT
};

class LoginOptionsWidget : public QWidget
{
    Q_OBJECT
public:
    explicit LoginOptionsWidget(QWidget *parent = nullptr);
    virtual ~LoginOptionsWidget();

    bool getCurLoginOpt(int& nLoginOptType, int& nDrvId);
    unsigned getLoginOptCount();
    DeviceInfoPtr getFirstDevInfo();
    int convertDeviceType(int nDevType);
    void updateUIStatus(bool update);
    void setUser(int uid);
    void setCurrentDevice(int drvid);
    void setCurrentDevice(const QString &deviceName);
    void setCurrentDevice(const DeviceInfoPtr &pDeviceInfo);
    DeviceInfoPtr findDeviceById(int drvid);
    DeviceInfoPtr findDeviceByName(const QString &name);
    void setDeviceDisable(int nDevId, bool bDisable = true);
    bool isDeviceDisable(int nDevId);
    void lockStatusChanged(bool locked);

    /**
     * @brief 获取默认设备
     * @return
     */
    QString GetDefaultDevice(uid_t uid);

    /**
     * @brief 进行生物识别认证
     * @param deviceInfo    使用的设备
     * @param uid           待认证的用户id
     */
    void startAuth(DeviceInfoPtr device, int uid);

    /**
     * @brief 终止生物识别认证
     */
    void stopAuth();

    /**
     * @brief 是否正在认证
     * @return
     */
    bool isAuthenticating() {
        return m_isInAuth;
    }
    QPixmap loadSvg(QString path, QString color, int size);

public slots:
    void readDevicesInfo();
    void onIdentifyComplete(int uid, bool ret, int retErrNo);
    void onStatusChanged(int drvid, const QString &message);
    void onFrameWritten(int drvid);
    void onUSBDeviceCountChange(int newNum);
    void onOptionSelected(int nIndex);
    void setQRCodeMsg(QString strMsg);
    void setQRCode(QImage& imgQRCode);
    void setFaceImg(QImage& imgFace, int nStatus = 0);

signals:
    void notifyOptionsChange(unsigned uOptionsCount);
    void optionSelected(unsigned uLoginOptType, const DeviceInfoPtr &deviceInfo);
    void updateImage(QImage img);
    // nStatus -1 dbus错误，0 无错误，1 超时，2 其他错误
    void authComplete(uid_t uid, bool bResult, int nStatus);
    void updateAuthMsg(QString strMsg);
    void updateWndSize(unsigned uLoginOptType, unsigned uLoginOptSize);

private:
    void initUI();
    void initConnections();
    void addOptionButton(unsigned uLoginOptType, int nDrvId, QString strDrvName);
    void clearOptionButtons();
    void updateOptionButtons();
    void startAuth_();
    QPixmap PixmapToRound(const QPixmap &src, int radius);
    QPixmap scaledPixmap(int width, int height, QString url);
    QPixmap drawSymbolicColoredPixmap(QPixmap &source, QString cgColor);
    void updatePixmap();

private:
    BioAuth *m_biomericProxy = nullptr;
    BioDevices *m_bioDevices = nullptr;
    DeviceMap m_mapDevices;
    unsigned m_curLoginOptType = LOGINOPT_TYPE_PASSWORD;
    int m_uid = -1;
    QString m_strUserName;
    DeviceInfoPtr m_curDevInfo = nullptr;     // 当前选择的设备信息
    int m_dupFD = -1;               // 透传的图像文件句柄
    bool m_isInAuth;                // 是否正在验证
    bool m_isStopped;               // 是否被强制终止
    QTimer *m_retrytimer = nullptr;           // 重试定时器

    // UI
    QVBoxLayout *m_layoutMain = nullptr;
    QHBoxLayout *m_layoutOptBtns = nullptr;
    QHBoxLayout *m_layoutImage = nullptr;

    QLabel *m_labelOptTitle = nullptr;
    QButtonGroup *m_btnGroup = nullptr;
    QList<int> m_listDriveId;
    QMap<int, QToolButton*> m_mapOptBtns;
    QWidget *m_widgetImage = nullptr;
    QLabel          *m_labelFace = nullptr;
    QLabel          *m_labelFaceLoad = nullptr;
    QLabel          *m_labelQRCode = nullptr;   // 二维码图标
    QLabel          *m_labelQRCodeMsg = nullptr; // 二维码状态消息提示
    QLabel          *m_labelQRCodeTip = nullptr;
    QMap<int, QMap<int,bool>> m_mapDisableDev;
    bool            is_Lock = false;
    QPixmap         m_waitingPixmap;
    QTimer          *w_timer;
};

#endif // LOGINOPTIONSWIDGET_H
