#ifndef FORMGUNDETAIL_H
#define FORMGUNDETAIL_H

#include <QWidget>
#include <QLabel>

#include "DataTypeDef.h"

namespace Ui {
class FormGunDetail;
}

namespace Error {
constexpr int SUCCESS = 0;
constexpr int INIT_FAILED = 1;
constexpr int INVALID_GUN_ID = 2;
constexpr int INVALID_STATE = 3;
}

class FormGunDetail : public QWidget
{
    Q_OBJECT

public:
    explicit FormGunDetail(QWidget *parent = nullptr);
    ~FormGunDetail();

    bool init(const int& gun_id);
    void setGunId(const int& gun_id);

public slots:
    void slotParseControlAuth(int gun_id, int state);

private:
    void initWidget();

    void initForms();

    void initConnect();
    void setupTcpServerConnections();
    void setupGlobalsConnections();
    void setupChargingFormConnections();
    void setupAuthenticationFormConnections();

    void initBackground();
    void setBackground(QLabel* label, const QString& imageUrl);
    void setChargeGunBackground();
    void setChargeProgressBackground();

    void refreshChargeGunBackground(const bool &state = true);

    void showForm(charge_prepare_enum form);

    void refreshChargeGunTips(const QString& tips);
    void refreshLabelStyleSheet(const charge_prepare_enum& form);

    QString getTitles(const charge_prepare_enum& form);

    void setCurrentChargeProgress(const charge_progress_enum& progress);
    void setCurrentChargeProgressText(const charge_progress_enum &progress);

    // 状态管理
    void refreshGunType(const UINT8_QSTRING_MAP& type);
    void refreshGunState(const UINT32_QSTRING_MAP& state);
    void refreshPlugIn(const uint32_t &state);
    void refreshAuthorization();
    void refreshChargePrepare(const uint32_t &state);
    void refreshGunError(const UINT32_QSTRING_MAP& error);

    // 样式管理
    void setPlugInTextColor(const QString &color);
    void setAuthorizationTextColor(const QString &color);
    void setChargingTextColor(const QString &color);
    void setFinishedTextColor(const QString &color);

signals:
    void showPrice();    
    void signalControlAuth(int gun_id,int type);
    void signalChanged(int gun_id, int type);
    void signalStopCharge(int index);

private:
    Ui::FormGunDetail *ui;

    struct GunState {
        int                 gun_id;
        bool                error{false};           // 错误
        bool                plug_in{false};         // 插枪
        bool                auth_result{true};      // 鉴权
        uint32_t            charging_state{0};      // 充电状态
        uint8_t             type{GUNTYPE_NACS};     // 枪类型
        bool                requestPowerByMinutes;
        bool                requestPowerByPowerKW;
        bool                requestPowerBySoc;
        charge_prepare_enum currentForm{PLUG_IN};   // 当前表单
    };
    GunState m_gun;

    // UI组件
    QLabel* backgroundImage{nullptr};
    QLabel* charge_progress_bg_image{nullptr};

    // 表单管理
    QMap<charge_prepare_enum, QWidget*> m_formMap;
    TCU_CFG m_tcu_cfg;
};

#endif // FORMGUNDETAIL_H
