#ifndef GQT_H
#define GQT_H
#include <QLayout>
#include <QThread>
#include <QComboBox>

inline long long steady_milli() {
    return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
inline long long system_milli() {
    return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
inline long long steady_micro() {
    return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
inline long long system_micro() {
    return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

template<class T>
class Wrp {
public:
    T *obj;
    Wrp(T *obj = nullptr){
        this->obj = obj;
    };
    inline Wrp& operator()(T *obj){
        this->obj = obj;
        return *this;
    }
    inline Wrp& operator()(T *obj, QLayout *layout){
        this->obj = obj;
        layout->addWidget(obj);
        return *this;
    }
    inline Wrp& addTo(QLayout *layout){
        layout->addWidget(obj);
        return *this;
    }
    inline Wrp& margin(int a){
        obj->setMargin(a);
        return *this;
    }
    inline Wrp& font(const QFont &font){
        obj->setFont(font);
        return *this;
    }
    inline Wrp& font(int size){
        QFont font = obj->font();
        font.setPointSize(size);
        obj->setFont(font);
        return *this;
    }

    inline Wrp& width(int w){
        obj->setFixedWidth(w);
        return *this;
    }
    inline Wrp& height(int h){
        obj->setFixedHeight(h);
        return *this;
    }
    inline Wrp& padding(int wAdd, int hAdd, int minW = 32, int minH = 16){
        wAdd+=8;
        hAdd+=8;
        QSize size = obj->fontMetrics().size(Qt::TextShowMnemonic, obj->text());
        int &rwidth = size.rwidth();
        rwidth += wAdd;
        if(rwidth < minW) rwidth = minW;
        int &rheight = size.rheight();
        rheight += hAdd;
        if(rheight < minH) rheight = minH;
        obj->setFixedSize(size);
        return *this;
    }

    inline Wrp& alignC(){
        obj->setAlignment(Qt::AlignCenter);
        return *this;
    }
    inline Wrp& alignR(){
        obj->setAlignment(Qt::AlignRight);
        return *this;
    }

    inline Wrp& connStart(){
        QObject::connect(obj, &QThread::finished, obj, &QThread::deleteLater);
        obj->start();
        return *this;
    }
};

class VBox : public QBoxLayout {
public:
    inline VBox(QWidget *parent=nullptr) : QBoxLayout(TopToBottom, parent) {}
    inline VBox(QBoxLayout *parent) : QBoxLayout(TopToBottom) {
        parent->addLayout(this);
    };
};
class HBox : public QBoxLayout {
public:
    inline HBox(QWidget *parent=nullptr) : QBoxLayout(LeftToRight, parent) {}
    inline HBox(QBoxLayout *parent) : QBoxLayout(LeftToRight) {
        parent->addLayout(this);
    };
};

template <typename Func>
inline QThread *ThreadStart(Func &&f) {
    QThread* thread = QThread::create(f);
    QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
    thread->start();
    return thread;
}

inline void select(QComboBox *combo, const QVariant &data) {
    int idx = combo->findData(data);
    if(idx!=-1) combo->setCurrentIndex(idx);
}


#endif
