#ifndef __ADD_PRINTER_WINDOW_H__
#define __ADD_PRINTER_WINDOW_H__

#include <QThread>
#include <QStandardItemModel>
#include <QMap>

#include <kysdk/applications/kdialog.h>
#include <kysdk/applications/ktabbar.h>

#include "device_information.h"

class QTableView;
class QComboBox;
class QPoint;
class QStackedWidget;
class QLineEdit;
class AutoSearchResultModel;
class CustomPushButton;
class CustomLabel;


class getUsbPrinterThread:public QObject{
    Q_OBJECT
public:
    explicit getUsbPrinterThread(QObject *parent = nullptr);
    ~getUsbPrinterThread(){}
public:
    void startWorker(){
        if(m_thread!=nullptr)
            m_thread->start();
    }
private:
    QThread *m_thread = nullptr;
    void startSearch();
signals:
    void finished();
    void recvUsbPrinter(DeviceInformation&);
};

QT_BEGIN_NAMESPACE
namespace Ui { class AddPrinterWindow; }
QT_END_NAMESPACE

class AddPrinterWindow : public kdk::KDialog
{
    Q_OBJECT
public:
    explicit AddPrinterWindow(QPoint pos, QWidget *parent = nullptr);
    ~AddPrinterWindow();
    void manualInstall(DeviceInformation &info);
    void setQSSFontSize(QFont curFont);
private:
    void initUi();
    //开关按钮
    void initSwitchArea();
    //添加设备区域
    void initCentralWidget();
    //自动添加设备区域
    void initAutoWidget();
    //手动添加设备区域
    void initManualWidget();
    //设备信息区域
    void initDeviceComponents();
    //确定、取消
    void initBottomArea();
    //清除设备区域信息，把添加按钮置灰
    void clearDeviceArea();
    void clearDeviceComponent();
    void clearDrvComboBox();
    //检测打印机名是否合法，由信号触发形参选true，带messagebox提示，直接调用选false
    bool checkPrinterNameIsLegal(bool hasMessageBox);
private:
    //---UI控件---start
    Ui::AddPrinterWindow *ui = nullptr;
    kdk::KTabBar *bar = nullptr;

    //父对象中心位置
    QPoint m_parentCenterPos;
    //添加打印机区域控件，一个整体
    QStackedWidget *m_centralWidget = nullptr;
    //手动添加打印机区域
    QWidget *m_manualWidget = nullptr;
    CustomLabel *m_manualLabel = nullptr;
    QLineEdit* m_addrEdit = nullptr;
    CustomPushButton* m_searchBtn = nullptr;
    CustomLabel* m_protocolLabel = nullptr;
    QComboBox* m_protocolComboBox = nullptr;
    QLabel* m_printerStatus = nullptr;

    // 自动添加打印机区域
    QPushButton* m_refreshBtn = nullptr;
    QWidget* m_autoWidget = nullptr;
    QTableView *m_autoTableView = nullptr;
    AutoSearchResultModel *m_autoSearchResultModel;
    CustomLabel* m_devList = nullptr;
    QLabel* m_loadingPic = nullptr;

    //设备参数区域，名称、位置、驱动
    CustomLabel *m_nameLabel = nullptr;
    CustomLabel *m_netLabel = nullptr;
    CustomLabel *m_locationLabel = nullptr;
    CustomLabel *m_drvLabel = nullptr;

    QLineEdit* m_nameEdit = nullptr;
    QLineEdit* m_locationEdit = nullptr;
    QComboBox* m_drvComboBox = nullptr;
    //确认，取消
    CustomPushButton *m_forwardBtn = nullptr;
    //---UI控件---End

    //页面内部变量,带添加设备的信息,在点击可用设备按钮或者点击搜索按钮的时候设置
    DeviceInformation m_deviceInfo = DeviceInformation();
    //安装状态，0：安装成功，1：安装失败，2：已经安装
    int m_installOk = -1;
    QRect m_parentRect = QRect();
    QString m_retPpdName = QString();

signals:
    void showThis(QPoint pos);
    //发送给主界面，通知主界面需要更新打印机列表，在窗口关闭前发送
    void needUpdateDests(QString);
    void installEnd();
    void receivePPD();
private slots:
    //按键响应
    void onForwardClicked();
    void onShow(QPoint pos);
    void onSearchClicked();
    void handleDeviceFromIP(DeviceInformation &info);
    void onChooseClicked();
    void onAutoClicked();
    void onManualClicked();
    void onDeviceUpdated(DeviceInformation& info);
    void onAddrChanged(const QString&);
    void onAutoDeviceAdd(DeviceInformation& info);
    //异步化，分成两部分处理
    //点击启动线程，线程中发送信号给主线程
    void onRefreshClicked();
    void onNameEditChanged();

protected:
    bool eventFilter(QObject *watched, QEvent *event) override;
};

class AutoSearchResultModel : public QStandardItemModel
{
    Q_OBJECT
public:
    explicit AutoSearchResultModel(QStandardItemModel *parent = nullptr);
    ~AutoSearchResultModel();
    void addPrinter(const DeviceInformation &info);
    DeviceInformation getInfo(const QModelIndex &index);
    void clearUsbPrinter();
    void clearAllPrinter();
private:
    QMap<QString, DeviceInformation> *m_devInfoList;
};

#endif //__ADD_PRINTER_WINDOW_H__
