#include "cameradisplay.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QToolButton>
#include <QStyleOption>
#include <QPainter>

CameraDisplay::CameraDisplay(QWidget *parent)
    : QWidget(parent)
{
    mMainVLayout = new QVBoxLayout;
//    mMainVLayout->setSpacing(0);
//    mMainVLayout->setMargin(0);
    mMainVLayout->setContentsMargins(0, 0, 0, 0);

    mTopWidget = new QWidget;
    mTopWidget->setObjectName("CameraDisplayTopWidget");
    mTopWidget->setFixedHeight(32);
    mTopHLayout = new QHBoxLayout;
    mTopHLayout->setContentsMargins(0, 0, 0, 0);
    mTopHLayout->setSpacing(0);
//    mIndexLabel = new QLabel;
//    mIndexLabel->setObjectName("CameraDisplayIndexLabel");
//    mIndexLabel->setFixedSize(50, 40);
//    mIndexLabel->setAlignment(Qt::AlignCenter);
    mIndexButton = new QToolButton;
    mIndexButton->setFixedSize(50, 32);
    mIndexButton->setObjectName("CameraDisplayIndexButton");

    mTitleLabel = new QLabel;
    mTitleLabel->setObjectName("CameraDisplayTitleLabel");
    mTitleLabel->setAlignment(Qt::AlignCenter);
    mTitleLabel->setFixedSize(50, 32);
    mStateLabel = new QLabel;
    mStateLabel->setObjectName("CameraDisplayStateLabel");
    mStateLabel->setAlignment(Qt::AlignCenter);
    mStateLabel->setFixedHeight(32);
    mTopHLayout->addWidget(mIndexButton);
    mTopHLayout->addWidget(mStateLabel);
    mTopHLayout->addWidget(mTitleLabel);
    mTopWidget->setLayout(mTopHLayout);

    mContentLabel = new QLabel;
    mContentLabel->setAlignment(Qt::AlignCenter);

    mMainVLayout->addWidget(mTopWidget);
    mMainVLayout->addWidget(mContentLabel);

    this->setLayout(mMainVLayout);

    connect(mIndexButton, &QPushButton::clicked, this , [=](){
        QString index = mIndexButton->text();
        QString title = mTitleLabel->text();
        QString state = mStateLabel->text();
        emit clickIndex(index, title, state);
    });
}

void CameraDisplay::setIndex(const QString &index)
{
    mIndexButton->setText(index);
}

void CameraDisplay::setTitle(const QString &title)
{
    mTitleLabel->setText(title);
}

void CameraDisplay::setState(CameraState state)
{
    if(state == CameraState::NG)
    {
        mStateLabel->setText("NG");
    }
    else if(state == CameraState::OK)
    {
        mStateLabel->setText("OK");
    }
    else
    {
        mStateLabel->setText("UNKNOW");
    }

}

void CameraDisplay::setContent(const QString &content)
{
    mContentLabel->setText(content);
}

void CameraDisplay::paintEvent(QPaintEvent *e)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
