#ifndef SHORTCUTUI_H
#define SHORTCUTUI_H

#include "settinggroup.h"
#include "addbutton.h"
#include "fixlabel.h"
#include "titlelabel.h"
#include "doubleclicklineedit.h"
#include "clickfixlabel.h"
#include "addshortcutdialog.h"

#include <QHBoxLayout>
#include <QWidget>
#include <QMenu>
#include <QMessageBox>
#include <QGSettings>

class ShortcutUi : public QWidget
{
    Q_OBJECT
public:
    ShortcutUi();
    ~ShortcutUi();
    void addSystemShortcut(const QString &name, const QString &shortcut, const QString &mediaKey, KeyEntryList *systemEntryList, KeyEntryList *windowEntryList, KeyEntryList *customEntryList);
    void addWindowShortcut(const QString &name, const QString &shortcut, const QString &mediaKey, KeyEntryList *systemEntryList, KeyEntryList *windowEntryList, KeyEntryList *customEntryList);
    void addCustomShortcut(KeyEntry keyEntry, KeyEntryList *systemEntryList, KeyEntryList *windowEntryList, KeyEntryList *customEntryList);
    void clearCustomShorcutFrame();
    void setWindowValid(bool isValid);
    void setWindowHide();
    void addDefaultBtn();
    void loadDefaultShortcut();

private:
    void initUi();
    void initSystem();
    void initWindow();
    void initCustom();
    void updateGeneralShort(DoubleClickShortCut *shortCutEdit, QString key, QString value, bool isConflict);

    QString getShowShortcutString(QString src);
    QString keyToLib(QString key);
    QString conflictTip(const QString &key, const QString &value);
    void defaultButtonSlot();
    void setWindowShortcutToNull();
    void defaultShortcut();
    void setCustomShortcutToNull();
    QString keyToSequence(QString key);
    void labelDoubleClickSlot(ClickFixLabel* fixLabel,
                              DoubleClickLineEdit* lineEdit);
    void labelDoubleClickSlot(ClickFixLabel* fixLabel,
                              DoubleClickShortCut *lineEdit);
    void lineEditFocusOutSlot(DoubleClickShortCut* lineEdit,
                              ClickFixLabel *fixLabel);
    void lineEditFocusOutSlot(DoubleClickLineEdit* lineEdit,
                              ClickFixLabel *fixLabel);
    void lineEditChangeSlot(DoubleClickShortCut* lineEdit,
                            ClickFixLabel *fixLabel,
                            QString key);
    void customLineEditStrChangeSlot(DoubleClickLineEdit* lineEdit,
                                     ClickFixLabel *fixLabel,
                                     KeyEntry* keyEntry);
    void customLineEditChangeSlot(DoubleClickShortCut* lineEdit,
                                  ClickFixLabel *fixLabel,
                                  KeyEntry* keyEntry);
    void lineEditConflictSlot(DoubleClickShortCut* lineEdit,
                              ClickFixLabel *fixLabel,
                              QString key);
    void customLineEditConflictSlot(DoubleClickShortCut* lineEdit,
                                    ClickFixLabel *fixLabel,
                                    KeyEntry* keyEntry);
    void lineEditOccupySlot(DoubleClickShortCut* lineEdit,
                            ClickFixLabel* fixLabel);
    void customAddDialogSlot(AddShortcutDialog *addDialog,
                             DoubleClickLineEdit *lineEdit,
                             DoubleClickShortCut *bingdLineEdit,
                             ClickFixLabel* fixLabeL,
                             ClickFixLabel* bindFixLabel,
                             KeyEntry* keyEntry,
                             QString name, QString exec, QString key);
private:
    QVBoxLayout *uiLayout     = nullptr;
    QVBoxLayout *systemLayout = nullptr;
    QVBoxLayout *customLayout = nullptr;
    QVBoxLayout *m_windowLayout = nullptr;
    QHBoxLayout *m_defaultLayout = nullptr;

private:
    TitleLabel   *systemTitle         = nullptr;
    SettingGroup *systemShortcutGroup = nullptr;

    TitleLabel   *m_windowTitle         = nullptr;
    SettingGroup *m_windowShortcutGroup = nullptr;
    QPushButton* m_defaultButton = nullptr;

private:
    TitleLabel   *customTitle         = nullptr;
    SettingGroup *customShortcutGroup = nullptr;
    AddButton    *addButton           = nullptr;

    QList<KeyEntry>              mSystemEntryList;
    QList<KeyEntry>              mCustomEntryList;
    QList<KeyEntry>              m_windowEntryList;

    QList<DoubleClickShortCut *> mGeneralLineEdit;
    QList<ClickFixLabel *>       mGeneralName;
    QList<DoubleClickShortCut *> mCustomLineEdit;
    QList<ClickFixLabel *>       mCustomName;
    QList<DoubleClickShortCut *> m_windowLineEdit;
    QList<ClickFixLabel *>       m_windowName;

    QMap<QString, QString>       mAllKeyI18nName;
    bool m_windowShortcutValid = true;
    QDBusInterface* m_shortcutInterface = nullptr;
    QList<QString> m_defaultShortcutList;

Q_SIGNALS:
    void deleteShortcut(const QString &path);
    void toCreateShortcut(QString path, QString name, QString exec, QString key, bool buildFlag = true, bool convertFlag = true, DoubleClickShortCut *shortCut = nullptr);
    void addButtonClicked();
    void updateSystemShortcut(const QString &key, const QString &value);
    void updateCustomShortcut(const QByteArray &path, const QString &key, const QString &value);
};

#endif // SHORTCUTUI_H
