#pragma once

#include <QLabel>
#include <QWidget>
#include <QMouseEvent>

class QCLED : public QLabel
{
    Q_OBJECT

public:
    explicit QCLED(QWidget *parent = nullptr,
                   Qt::WindowFlags f = Qt::WindowFlags());
    ~QCLED() override;

    volatile int iledNum;
    volatile int iledColor;
    volatile int tglLED;

    void setLED(int i) { iledNum = i; }
    void ext_ctrl(int i);

public slots:
    void wht() { applyColor("#cccccc"); }
    void red() { applyColor("#dd2222"); }
    void grn() { applyColor("#22cc44"); }
    void yel() { applyColor("#ddcc00"); }

    void OnOffLED(int i)
    {
        tglLED = i;
        tglLED ? red() : wht();
    }

    void toggleLED()
    {
        tglLED ^= 1;
        tglLED ? red() : wht();
    }

signals:
    void clicked();
    void onoff_switch(int);

protected:
    void mousePressEvent(QMouseEvent *event) override;

private:
    void applyColor(const char *color)
    {
        setStyleSheet(QString(s_base) + "background: " + color + ";");
    }
    static constexpr const char *s_base =
        "border-radius: 8px; min-width: 16px; max-width: 16px;"
        "min-height: 16px; max-height: 16px; border: 1px solid #555; ";
};
