#ifndef CHANNELWIDGET_H
#define CHANNELWIDGET_H

#include <QWidget>
#include <QCheckBox>
#include <QLabel>
#include <QHBoxLayout>
#include <QComboBox>
#include <eismultiplexer.h>
#include <memory>

class ChannelWidget : public QWidget
{
        Q_OBJECT

public:
        explicit ChannelWidget(uint16_t deviceSerial, uint16_t channelNumber,
                               std::shared_ptr<struct eismultiplexer> multiplexer,
                               QWidget *parent = nullptr);
        ~ChannelWidget() override;

        uint16_t getDeviceSerial() const;
        uint16_t getChannelNumber() const;
        bool isChecked() const;

        // Accessors for MainWindow to populate and access the gang combo
        QComboBox* getGangCombo() { return &gangcombo; }
        void setGangedChannel(uint16_t deviceSerial, uint16_t channelNumber);

public slots:
        void onOtherChannelStateChanged(uint16_t deviceSerial, uint16_t channelNumber, bool checked);

private slots:
        void onChannelToggled(bool checked);
        void onGangComboChanged(int index);
        void replaceMultiplexer(std::shared_ptr<struct eismultiplexer> multiplexer, int serial = -1);

signals:
        void channelAboutToBeTurnedOn(uint16_t deviceSerial, uint16_t channelNumber);
        void channelStateChanged(uint16_t deviceSerial, uint16_t channelNumber, bool checked);

private:
        void updateGangCombo();
        void updateCheckboxState();

        uint16_t deviceSerial;
        uint16_t channelNumber;
        std::shared_ptr<struct eismultiplexer> multiplexer;
        QCheckBox checkbox;
        QLabel devicelabel;
        QLabel channellabel;
        QLabel ganglabel;
        QComboBox gangcombo;
        QFrame line;
        QVBoxLayout vlayout;
        QHBoxLayout hlayout;
        QVBoxLayout labellayout;

        // Track the channel this one is ganged to (if any)
        int gangedDeviceSerial = -1;
        int gangedChannelNumber = -1;
};

#endif // CHANNELWIDGET_H

