#include "LedStringWidget.h"
#include <QPainter>



CLedStringWidget::CLedStringWidget(int NbLed, QGraphicsItem *parent)
{
    mLEDSpacing = 5;
    mNbLED = NbLed;
    mBoundingRect.setTop(0);
    mBoundingRect.setLeft(0);
    int color = 0;

//    for(int i = 0; i < NbLed; i++)
//    {
//        QColor LedColor;
//        if(color == 0)
//        {
//            LedColor = QColor(Qt::red);
//            color++;
//        }
//        else if(color == 1)
//        {
//            LedColor = QColor(Qt::green);
//            color++;
//        }
//        else
//        {
//            LedColor = QColor(Qt::blue);
//            color = 0;
//        }
//        CRGBLedWidget *NewLED = new CRGBLedWidget(LedColor,this);
//        NewLED->setPos(i*(NewLED->boundingRect().width()+mLEDSpacing),0);
//        mLEDList.append(NewLED);
//    }

    for(int i = 0; i < 30; i++)
    {
//        for(int led = 0; led < 10; led++)
        {
            QColor LedColor = QColor(50,0,0);
            CRGBLedWidget *NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+1)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(100,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+2)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(150,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+3)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(200,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+4)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(255,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+5)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(200,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+6)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(150,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+7)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(100,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+8)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(50,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+9)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);

            LedColor = QColor(25,0,0);
            NewLED = new CRGBLedWidget(LedColor,this);
            NewLED->setPos(((i*10)+10)*(NewLED->boundingRect().width()+mLEDSpacing),0);
            mLEDList.append(NewLED);
        }
    }

}

void CLedStringWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setPen(QPen(Qt::darkGreen));
   // painter->drawRect(boundingRect());

}

QRectF CLedStringWidget::boundingRect() const
{
    if(mLEDList.size() == 0)
        return QRectF();

        //This assumes all leds are the same size...
    return QRectF(0,
                  0,
                  mLEDList.size()*(mLEDSpacing + mLEDList.at(0)->boundingRect().width()),
                  mLEDList.at(0)->boundingRect().height());

}

int CLedStringWidget::SetLedStringColors(QList<QColor> *LEDColors)
{
    if(LEDColors->size() != mLEDList.size())
    {
        return 0;
    }

    for(int i = 0; i < mLEDList.size(); i++)
    {
        mLEDList[i]->SetColor(LEDColors->at(i));
    }

    this->boundingRect();

}
