#ifndef FORMAUTHENTICATION_H
#define FORMAUTHENTICATION_H

#include <QWidget>
#include <QLabel>
#include <QToolButton>
#include <QTimer>
#include <QScopedPointer>
#include <QMutex>
#include <QReadWriteLock>
#include <QThread>

#include "DataTypeDef.h"
#include "NFCReaderWorker.h"

namespace Ui {
class FormAuthentication;
}

class FormAuthentication : public QWidget
{
    Q_OBJECT

public:
    explicit FormAuthentication(int gun_id, QWidget *parent = nullptr);
    ~FormAuthentication();

    // 禁止拷贝和赋值
    FormAuthentication(const FormAuthentication&) = delete;
    FormAuthentication& operator=(const FormAuthentication&) = delete;

    // 公共接口
    void setBackground(QLabel* label, const QString& imageUrl);
    void setAuthenticationType(int type = START_CHARGING_AUTH);

    uint32_t gun_id() const;

    void stopTimer();

    void setRequestPowerByMinutes(bool newRequestPowerByMinutes);
    void setTime(int time);
    void setRequestPowerByPowerKw(bool newRequestPowerPowerKw);
    void setEnergy(int energy);
    void setRequestPowerBySOC(bool newRequestPowerBySOC);
    void setSoc(int soc);

public slots:
    void slotStartAuth(int gun_id, int type);

signals:
    void signalCancelAuth();
    void signalAuthResult(int result);
    void signalChargingCtl(charging_control_info_t& data);
    void signalStartNFCReading(charging_control_info_t& data);
    void signalStartNFCReadCard(charging_control_info_t& data);

private slots:
    void authTimeout();
    void onNFCReadCard(charging_control_info_t &data);

protected:
    void showEvent(QShowEvent* event) override;
    void hideEvent(QHideEvent* event) override;

private:
    // 初始化相关
    void initWidgets();
    void initConnect();
    void setupNFCWorker();

    // NFC相关操作
    bool configureNFCReader();
    void cleanupNFCReader();

    // ICON 相关
    void setCardIconVisible(const bool& visible);
    void setPosIconVisible(const bool& visible);
    void setQrIconVisible(const bool& visible);

    // 定时器相关
    void restartTimer(bool isStop);
    void updateTimerDisplay();

    void startAuth();

private:
    Ui::FormAuthentication* ui;
    QScopedPointer<QTimer> m_timer;
    
    QThread* m_nfcThread;
    NFCReaderWorker* m_nfcWorker;

    QLabel*         posBackgroundImage;
    QLabel*         authBackgroundImage;
    QToolButton*    cancelBackgroundImage;
    QLabel*         tipsBackGroundImage;

    struct GunState{
        bool                        is_stop_charging;
        int                         cur_time;
        bool                        is_reading_card;
        charging_control_info_t     charging_info;
    };
    GunState m_gun;

    tcu_config_info_t m_tcu_cfg;    
};

#endif // FORMAUTHENTICATION_H
