#ifndef GENERALINTERFACE_H
#define GENERALINTERFACE_H

#include <QObject>
#include <QByteArray>
#include <QtEndian>
#include <QLabel>

#include <QPushButton>
#include <QPalette>
#include <QLineEdit>
#include <QScrollArea>

#include "DataTypeDef.h"
#include "CustomComboBox.h"

namespace AppName {
    constexpr const char* CCU_NAME = "app_CCU";
    constexpr const char* TCU_NAME = "app_TCU";
    constexpr const char* PCU_NAME = "app_PCU";
    constexpr const char* HW_NAME = "HW";
}

class GeneralInterface : public QObject {
public:
    // 安全转换函数
    template<typename T>
    static T toInt(const QByteArray &data,
                   int offset = 0,
                   bool networkOrder = true);

    // 便捷方法
    static uint8_t toUInt8(const QByteArray &data, int offset = 0);

    static uint16_t toUInt16(const QByteArray &data,
                             int offset = 0,
                             bool networkOrder = true);

    static uint32_t toUInt32(const QByteArray &data,
                             int offset = 0,
                             bool networkOrder = true);

    static uint64_t toUInt64(const QByteArray &data,
                             int offset = 0,
                             bool networkOrder = true);


    // 数据类型转换
    template<typename T>
    static QByteArray encodeValue(T& data, const uint8_t& length);

    template<typename T>
    static T decodeValue(const QByteArray& data);

    static QByteArray networkStructToByteArray(const MSG_HEAD& data);

    static QByteArray encodeStartChargeGunInfo(const START_CHARGE_CMD& data);
    static QByteArray encodeStopChargeGunInfo(const STOP_CHARGE_CMD& data);

    static charge_gun_basic_info_t decodeChargeBasicInfo(const QByteArray& data);

    static charge_gun_charge_info_t decodeChargeInfo(const QByteArray& data);

    static QString formatMillisecondDifference(qint64 ms1, qint64 ms2);

    static void makeLabelJump(QLabel *label);

    static QByteArray encodeAuthInfo(const GUN_AUTH& data);

    static QString getGunIconUrl(const uint8_t& type, bool isError = false);

    /**
     * @brief FormStandby::isError  判断是否存在大于最大错误码的报警信息
     * @param error_list            错误码索引列表
     * @return                      存在返回TRUE，否则返回FALSE
     */
    static bool isError(const QList<uint32_t> &error_list);

    static uint8_t getMaxElement(const QList<uint32_t> &state_list);

    static void addRotationAnimation(QLabel* label);

    static uint8_t getMaxElementWithQMax(const QList<uint8_t> &list);

    static QString getTips(const int &state);

    static bool isCrossTime(const uint32_t& start, const uint32_t& end);

    static int getAuthResult(const QByteArray& cardId);

    static bool isTimeout(const QString& date);

    // 心跳报文编解码
    static REPLY_INFO decodeReplyInfo(const QByteArray& data);
    static QByteArray encodeReplyInfo(const REPLY_INFO& info);

    static QMap<uint32_t, BASIC_INFO> m_gun_basic_info;

    static void setCheckBoxChecked(const QWidget* widget, const bool& checked);

    static void setTouchScroller(QScrollArea* area);

    static QString getWindowTitle(const int& type);
    static QString getLogFilePath(const int& type);

    static QString getStateDescriptions(uint32_t code);

    static gui_state_enum getGuiState(const charge_prepare_enum& form, const int& state = 0);
    static gui_state_enum getGuiStateAuthForm(const int& state = 0);
    static gui_state_enum getGuiStatePrepareForm(const int& state = 0);
    static gui_state_enum getGuiStateChargingForm(const int& state = 0);
    static gui_state_enum getGuiStateFinishForm(const int& state = 0);
    static gui_state_enum getGuiStateErrorForm(const int& state = 1);

    static charge_progress_enum getChargeProgress(const charge_prepare_enum& form);
    static int getChangedGunID(const int& gun_id);

    // static QPixmap generateQRCode(const QString& text, int size = 200);

    static int getMaxHeight(QWidget* widget);
    static void initTimezoneComboBox(CustomComboBox* combox);
    static void initLanguageComboBox(CustomComboBox* combox);

    static void clearGridLayoutChiledren(QGridLayout* layout);
    static void gridLayoutAddWidget(QGridLayout* layout, QWidget* widget, int row = -1, int col = 0);
    static void gridLayoutAddStrtch(QGridLayout* layout);
    static int getChargingProfilePurposeIndex(const QString& purpose);
    static bool getChargingProfileByPurpose(charging_profiles_detail_info_t &data,
                                            const QList<charging_profiles_detail_info_t>& dataList,
                                            const QString& purpose);
    static QVARIANTMAP_INT_MAP chargingProfileToMap(const QList<charging_profiles_detail_info_t>& dataList);
    static QList<charging_profiles_detail_info_t> chargingProfileMapToList(const QMap<int, QVariant>& dataMap);

    static QMap<int, QVariant> chargingProfileToMapByConnectorId(const QList<charging_profiles_detail_info_t>& dataList);
    static QList<charging_profiles_detail_info_t> chargingProfileMapToListByConnectorId(const QMap<int, QVariant>& dataMap);

    static QMap<QString, QVariant> chargingProfileToMapByPurpose(const QList<charging_profiles_detail_info_t>& dataList,
                                                                 const int& connectorId);
    static QList<charging_profiles_detail_info_t> chargingProfileMapToListByPurpose(const QMap<QString, QVariant>& dataMap,
                                                                                    const int& connectorId);

    static QMap<QString, QVariant> chargingProfileToMapByKind(const QList<charging_profiles_detail_info_t>& dataList,
                                                              const int& connectorId,
                                                              const QString& purpose);
    static QList<charging_profiles_detail_info_t> chargingProfileMapToListByKind(const QMap<QString, QVariant>& dataMap,
                                                                                 const int& connectorId,
                                                                                 const QString& purpose);

    static QMap<QString, QVariant> chargingProfileToMapByRecurrencyKind(const QList<charging_profiles_detail_info_t>& dataList,
                                                                        const int& connectorId,
                                                                        const QString& purpose,
                                                                        const QString& kind);
    static QList<charging_profiles_detail_info_t> chargingProfileMapToListRecurrencyKind(const QMap<QString, QVariant>& dataMap,
                                                                                         const int& connectorId,
                                                                                         const QString& purpose,
                                                                                         const QString& kind);

    static QMap<int, QVariant> chargingProfileToMapByRecurrencyKind(const QList<charging_profiles_detail_info_t>& dataList,
                                                                        const int& connectorId,
                                                                        const QString& purpose,
                                                                        const QString& kind,
                                                                        const QString& recurrencyKind);
    static QList<charging_profiles_detail_info_t> chargingProfileMapToListRecurrencyKind(const QMap<int, QVariant>& dataMap,
                                                                                         const int& connectorId,
                                                                                         const QString& purpose,
                                                                                         const QString& kind,
                                                                                         const QString& recurrencyKind);

    static QVariantList chargingProfileToMapByStackLevel(const QList<charging_profiles_detail_info_t>& dataList,
                                                         const int& connectorId,
                                                         const QString& purpose,
                                                         const QString& kind,
                                                         const QString& recurrencyKind,
                                                         const int& stackLevel);
    static QList<charging_profiles_detail_info_t> chargingProfileMapToListByStackLevel(const QVariantList& dataMap,
                                                                                       const int& connectorId,
                                                                                       const QString& purpose,
                                                                                       const QString& kind,
                                                                                       const QString& recurrencyKind,
                                                                                       const int& stackLevel);

    static int getMaxElecment(const QList<int>& list);
    static uint32_t getMaxElecment(const QList<uint32_t>& list);
    static uint32_t getMaxErrorCode(const ERROR_NOTE_LIST& info);
    static QString getCurrentDate(const int& seconds);
    static int getCurrentDate(const QString& time);
    static QString getCurrentDateOfWeek(const QString& startTime, const int& seconds);
    static int getCurrentDateSecondOfWeek(const QString& startTime, const int& seconds);
    static int getCurrentDateOfWeek(const QString& startTime, const int& seconds, const int&dayOfWeek);
    static int getCurrentDateOfWeek(const QString& startTime, const QString& time, const int &dayOfWeek);
    static int getDayOfWeek(const QString& time);
    static void sortByStartPeriod(QList<charging_schedule_period_info_t>& list);
    static void sortByStackLevel(QList<charging_profiles_detail_info_t>& list);
    static int getMaxChargingProfileId(QList<charging_profiles_detail_info_t>& list);
    static QString getVersion(const QString& appName);

};


#endif // GENERALINTERFACE_H
