#include "screenchecker.h"
#include <windows.h>

ScreenChecker::ScreenChecker(QObject *parent) :
    QObject(parent)
{
    screenCount = 0;
}

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);

    float sizeVertical = 90;
    float sizeHorizontal = 150;

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

    std::sort(screens.begin(), screens.end(), [](QScreen* a, QScreen* b)
    {
            return a->geometry().x() < b->geometry().x() || //левее
                    (a->geometry().x() == b->geometry().x() && a->geometry().y() < b->geometry().y()); //выше
    });

    //int i = 0; i < screens.length() ;i++
    for (auto display : screens)
    {
        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(display->name() == QGuiApplication::primaryScreen()->name())
        {
            button->setChecked(true);
            button->setEnabled(false);
        }

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

    }

    updateDisplayData();

}

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

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

    if (displaysIndexList->at(0) != 0)
        displaysIndexList->replace(0,0);

    dataParserOutput->addRunData(*displaysIndexList);
}

void ScreenChecker::initialize(DataParserOutput *dataParserOutput, QHBoxLayout *layout)
{
    this->layout = layout;
    this->dataParserOutput = dataParserOutput;
}

ScreenChecker::~ScreenChecker()
{
    for (auto & button : buttons)
    {
        delete button;
    }
}
