#include "adpainter.h"


adpainter::adpainter(int width, int height, int adpcellsize)
{
    QString str;
    QPixmap tmpAlPixmap;
    int i=1;
    cellsize = adpcellsize;
    buff = new QPixmap(width,height);
    background = new QPixmap(width,height);
    pen = new QPen();
    brush = new QBrush();
    str.setNum(i);
    while (i != 0) {
        str.setNum(i);
        tmpAlPixmap.load("./images/Aliens/Al_0_" + str + ".png");
        if (!tmpAlPixmap.isNull()) {
            AlienPix.push_back(tmpAlPixmap);
            tmpAlPixmap = 0;
            i++;
        }
        else i = 0;
    }
    qDebug() << "pictures:" << AlienPix.size();
    AnimAlien = 0;
    StepAlien = 0;
}


void adpainter::clear()
{
    painter.begin(background);
    pen->setColor(QColor::fromRgb(qRgb(150,150,150)));
    painter.setPen(*pen);
    painter.fillRect(buff->rect(),QColor::fromRgb(qRgb(150,150,150)));
    painter.end();
}


void adpainter::drawgrid()
{
    int i;
    painter.begin(background);
    pen->setColor(QColor::fromRgb(qRgb(100,100,100)));
    painter.setPen(*pen);
    for (i = 0;i<buff->width()/cellsize;i++)
    {
        painter.drawLine(i*cellsize,0,i*cellsize,background->height());
    }
    for (i = 0;i<buff->height()/cellsize;i++)
    {
        painter.drawLine(0,i*cellsize,background->width(),i*cellsize);
    }
    painter.end();
}


void adpainter::drawAliens(Alien * al,AlienImages * pixmaps)
{
    //float tmpdx,tmpdy,arctg,tmpdx1,tmpdy1;
    //arctg = 0;
    painter.begin(buff);
    pen->setColor(QColor::fromRgb(qRgb(80,200,100)));
    brush->setColor(QColor::fromRgb(qRgb(230,10,10)));
    brush->setStyle(Qt::SolidPattern);
    painter.setPen(*pen);
    painter.setBrush(*brush);
    painter.drawPixmap(0,0,*background);
    for (int i = 0; i < al->path.size(); i++){
        painter.drawEllipse(al->path[i].x() * cellsize +1, al->path[i].y() * cellsize +1, cellsize - 2, cellsize - 2);
    }
    //tmpdx = position->pnt.x() - path.at(StepAlien).x()*cellsize;
    //tmpdy = position->pnt.y() - path.at(StepAlien).y()*cellsize;
    //if (StepAlien + 1 < path.size()) {
    //    tmpdx1 = position->pnt.x() - path.at(StepAlien+1).x()*cellsize;
    //    tmpdy1 = position->pnt.y() - path.at(StepAlien+1).y()*cellsize;
    //}
    //arctg = atanf(tmpdx1/tmpdy1);
    //position->angle = 180*(-arctg)/3.1415;
    //if (tmpdy1 < 0) position->angle = 180 + position->angle;
    //if (AlienPix.size() > 0) {
        //qDebug() << "angle:" << position->angle;
    /*    if (qAbs(tmpdx) < 1 && qAbs(tmpdy) < 1) StepAlien++;
        else {
            if (qAbs(tmpdx) <= qAbs(tmpdy)) {
                if (tmpdy < 0) position->pnt.ry()++;
                else position->pnt.ry()--;
            }
            if (qAbs(tmpdx) >= qAbs(tmpdy)) {
                if (tmpdx < 0) position->pnt.rx()++;
                else position->pnt.rx()--;
            }
        }*/
        painter.translate(al->Position.pnt.x()+cellsize/2, al->Position.pnt.y()+cellsize/2);
        painter.rotate(al->Position.angle);
        painter.drawPixmap(-cellsize, -cellsize, cellsize*2, cellsize*2, pixmaps->pix.at(al->PicIndex));
        //AnimAlien++;
        //if (AnimAlien >= AlienPix.size()) AnimAlien = 0;
        /*if (StepAlien >= path.size()) {
            StepAlien = 0;
            position->pnt.setX(path.at(0).x()*cellsize);
            position->pnt.setY(path.at(0).y()*cellsize);
        }*/
        //qDebug() << StepAlien;
    //}
    painter.end();
}


void adpainter::drawcell(QPoint pnt)
{
    painter.begin(background);
    pen->setColor(QColor::fromRgb(qRgb(0,0,0)));
    brush->setColor(QColor::fromRgb(qRgb(0,0,0)));
    brush->setStyle(Qt::SolidPattern);
    painter.setPen(*pen);
    painter.setBrush(*brush);
    painter.drawRect(pnt.x()*cellsize+1,pnt.y()*cellsize+1,cellsize - 2, cellsize - 2);
    painter.end();
}


void adpainter::clearcell(QPoint pnt)
{
    painter.begin(background);
    pen->setColor(QColor::fromRgb(qRgb(150,150,150)));
    brush->setColor(QColor::fromRgb(qRgb(150,150,150)));
    brush->setStyle(Qt::SolidPattern);
    painter.setPen(*pen);
    painter.setBrush(*brush);
    painter.drawRect(pnt.x()*cellsize+1,pnt.y()*cellsize+1,cellsize - 2, cellsize - 2);
    painter.end();
}


QPixmap * adpainter::getPixmap()
{
    return buff;
}
