#ifndef LOGINOPTIONSWIDGET_H
#define LOGINOPTIONSWIDGET_H

#include <QWidget>
#include "biometricproxy.h"
#include "biometricdeviceinfo.h"

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

typedef enum {
    BioT_FingerPrint,		/** 指纹 **/
    BioT_FingerVein,		/** 指静脉 **/
    BioT_Iris,				/** 虹膜 **/
    BioT_Face,				/** 人脸 **/
    BioT_VoicePrint,		/** 声纹 **/

    UniT_KCM,				/**	安全管控 **/
    UniT_General_Ukey,		/** 普通的Ukey **/
    UniT_Advanced_Ukey,		/** 高阶的Ukey **/
    UniT_Remote,            /** 远程账户 **/
}BioType;

class LoginOptionsWidget : public QWidget
{
    Q_OBJECT
public:
    explicit LoginOptionsWidget(BiometricProxy* proxy, int uid, QWidget *parent = nullptr);
    virtual ~LoginOptionsWidget();

    bool getCurLoginOpt(int& nLoginOptType, int& nDrvId);
    unsigned getLoginOptCount();
    DeviceInfoPtr getFirstDevInfo();
    int convertDeviceType(int nDevType);

    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);

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

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

    /**
     * @brief 是否正在认证
     * @return
     */
    bool isAuthenticating() {
        return m_isInAuth;
    }

public Q_SLOTS:
    void readDevicesInfo();
    void onIdentifyComplete(QDBusPendingCallWatcher *watcher);
    void onStatusChanged(int drvid, int status);
    void onFrameWritten(int drvid);
    void onUSBDeviceHotPlug(int drvid, int action, int devNum);
    void onOptionSelected(int nIndex);

Q_SIGNALS:
    void notifyOptionsChange(unsigned uOptionsCount);
    void optionSelected(unsigned uLoginOptType, const DeviceInfoPtr &deviceInfo);
    void updateImage(QImage img);
    void authComplete(bool bResult, int nStatus);
    void updateAuthMsg(QString strMsg);

private:
    void initUI();
    void initConnections();
    void addOptionButton(unsigned uLoginOptType, int nDrvId, QString strDrvName);
    void clearOptionButtons();
    void updateOptionButtons();
    void startAuth_();
    bool getAuthDouble();
    void updateUIStatus();
    QPixmap loadSvg(QString path, QString color, int size);
    QPixmap drawSymbolicColoredPixmap(QPixmap &source, QString cgColor);

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

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

    QLabel *m_labelOptTitle = nullptr;
    QButtonGroup *m_btnGroup = nullptr;
    QList<int> m_listDriveId;
    QMap<int, QToolButton*> m_mapOptBtns;
    QMap<int, QMap<int,bool>> m_mapDisableDev;
};

#endif // LOGINOPTIONSWIDGET_H
