#include "screenchecker.h"

#include <QLabel>
#include <QBitmap>
#include <QToolButton>
#include <QPainter>

ScreenChecker::ScreenChecker(QWidget *mainWidget,DataParser *dataParser, QHBoxLayout *layout, QObject *parent) :
    QObject(parent)
{
    screenCount = 0;
    this->mainWidget = mainWidget;
    this->layout = layout;
    this->dataParser = dataParser;
}

void ScreenChecker::check()
{
    screens = QGuiApplication::screens();

    QPixmap checkedPixmap(":resource/Icons/checked.png");
    checkedPixmap = checkedPixmap.scaled(20,20);

    QPainter painter;
    QColor color(51,153,0,255);

    painter.begin(&checkedPixmap);
    painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    painter.fillRect(checkedPixmap.rect(),color);
    painter.end();

    QIcon checkedIcon;
    checkedIcon.addPixmap(checkedPixmap,QIcon::Disabled,QIcon::On);

    qint8 sizeVertical = 90;
    quint16 sizeHorizontal = 150;

    if(screens.length() > 3)
    {
        sizeVertical = 80;
        sizeHorizontal = 120;
    }

    for (int i = 0; i < screens.length() ;i++)
    {
        QScreen *display = screens[i];
        screenCount++;

        QString sizeText = QString::number(display->size().width()) + "x" + QString::number(display->size().height());
        sizeText.append("\n");
        sizeText.append(QString::number(display->refreshRate()) + "Hz");

        QToolButton *button = new QToolButton();
        button->setText(sizeText);
        button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        button->setObjectName("displayView");
        buttons.append(button);

        layout->addWidget(button);
        float width = display->size().width();
        float height = display->size().height();
        float ratio =  width / height;

        QSize *sizeResult;
        QPushButton *checkedLabel = new QPushButton;

        checkedLabel->setParent(button);
        checkedLabel->setIcon(checkedIcon);
        checkedLabel->setObjectName("checkedLabelButton");
        checkedLabel->setGeometry(3,3,30,30);

        if(height > width)
        {
            sizeResult = new QSize(sizeVertical,sizeVertical / ratio);
        }
        else
        {
            sizeResult = new QSize(sizeHorizontal,sizeHorizontal/ ratio);
        }

        button->setMinimumSize(*sizeResult);
        button->setMaximumSize(*sizeResult);
        button->setCheckable(true);


        if(i == 0)
        {
            button->setChecked(true);
            button->setEnabled(false);
        }

        labels.append(checkedLabel);
        connect(button,&QToolButton::clicked,this,&ScreenChecker::updateDisplayData);

    }

    updateDisplayData();
}

void ScreenChecker::updateDisplayData()
{
    QList<int> *displays = new QList<int>;

    for (int i = 0; i <buttons.count();i++)
    {
        QToolButton *button = buttons[i];
        if(button->isChecked())
        {
            displays->append(i);
            labels[i]->show();
        }
        else
        {
            labels[i]->hide();
        }
    }

    dataParser->addRunData(*displays);
}

QString ScreenChecker::getScreenCount() const
{
    return QString::number(screenCount);
}

ScreenChecker::~ScreenChecker()
{
    for (int i = 0; i < buttons.size(); i++)
    {
        delete buttons[i];
    }
}
