#ifndef JOB_MANAGER_WINDOW_H
#define JOB_MANAGER_WINDOW_H

#include <QWidget>
#include <QStandardItemModel>
#include <QMenu>
#include <QTableWidget>
#include <kysdk/applications/kdialog.h>
#include "ukui_printer.h"
#include "device_information.h"
#include "custom_list_view.h"

/*
|----------------------------------------------------------------|
| status     | printer can do                                    |
|----------------------------------------------------------------|
| -          | cancel | delete | hold | release | move | repaint |
| pending    | Y      |        | Y    |         | Y    |         |
| held       | Y      |        |      | Y       |      |         |
| processing | Y      |        |      |         |      |         |
| canceled   |        | Y      |      |         |      |         |
| completed  | Y      |        |      |         |      | Y       |
|----------------------------------------------------------------|
*/

// about jobStatusStrList, u can see ipp_jstate_e in ipp.h for help
#ifndef JOB_FLAG
#define JOB_FLAG                                   \
QStringList jobList = {                            \
    QObject::tr("id"),                             \
    QObject::tr("user"),                           \
    QObject::tr("title"),                          \
    QObject::tr("printer name"),                   \
    QObject::tr("size"),                           \
    QObject::tr("create time"),                    \
    QObject::tr("job state"),                      \
};                                                 \
QStringList jobStatusStrList = {                   \
    "",                                            \
    "",                                            \
    "",                                            \
    QObject::tr("Job is waiting to be printed"),   \
    QObject::tr("Job is held for printing"),       \
    QObject::tr("Job is currently printing"),      \
    QObject::tr("Job has been stopped"),           \
    QObject::tr("Job has been canceled"),          \
    QObject::tr("Job has aborted due to error"),   \
    QObject::tr("Job has completed successfully"), \
};
#endif

class JobManagerModel : public QStandardItemModel
{
    Q_OBJECT
private:
    JOB_FLAG
public:

    explicit JobManagerModel(QStandardItemModel *parent = nullptr);
    ~JobManagerModel();
    bool addJob(const PrintJob &job);
    void resetFont(QFont font);
    QString getJobTitleTranslation(const QString &title);
};

class JobMenu : public QMenu
{
    Q_OBJECT
private:
    
public:
    explicit JobMenu(QWidget *parent = nullptr);
    ~JobMenu();
    QAction *m_actionCancel;
    QAction *m_actionDelete;
    QAction *m_actionHold;
    QAction *m_actionRelease;
    QAction *m_actionRepint;
    QAction *m_actionMove;
    
    QMenu *m_moveMenu;
};


class JobManagerWindow : public kdk::KDialog
{
    Q_OBJECT
private:
    JOB_FLAG
    QTimer *m_timer;
    QString m_printerName;
    UkuiQueueStatus m_status;
    QList<PrintJob> m_jobs;
    std::unique_ptr<CustomListView> m_view{nullptr};
    JobManagerModel *m_model;
    QModelIndexList m_selectList;
    QList<DeviceInformation> m_printerList;
    QPoint m_mainWidCenterPos;
    void getJobs(QList<PrintJob> &jobls, const QString &printerName, const UkuiQueueStatus &status);
    bool getJob(PrintJob &job, const int &id) const;
    bool compareJobs(const QList<PrintJob> &a, const QList<PrintJob> &b) const;
    void initView();
    void initTimer();
    void initMenu();
    void initUi();
    void initConnect();
    void ShowContextMenu(const QPoint &pos);
    void triggerMenu(QAction *action);
    void updateModel(bool needCompare);
    void setMenuActionEnabled(JobMenu &menu, const int &status);
    explicit JobManagerWindow(QString printerName = QString(),
                              UkuiQueueStatus status = UkuiQueueStatus::UKUI_PRINTER_QUEUE_ALL,
                              QWidget *parent = nullptr);
public:
    static JobManagerWindow *getInstance(QString printerName = QString(),
                                        UkuiQueueStatus status = UkuiQueueStatus::UKUI_PRINTER_QUEUE_ALL,
                                        QWidget *parent = nullptr);
    void showThis();
    void hideThis();
    ~JobManagerWindow();
};


#endif // JOB_MANAGER_WINDOW_H
