#include "form.h"
#include "ui_form.h"


Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    buildTowerId = -1;
    ui->setupUi(this);
    ui->pbSell->hide();
    graphics = new AD_Graphics(&core);
    ui->widget->layout()->addWidget(graphics);
    tbf = new TouchButtFrame();
    tbf->setOrientation(Qt::Vertical);
    morphtbf = new TouchButtFrame();
    morphtbf->setOrientation(Qt::Vertical);
    //tbf->addButton(tr("delete"));
    for(int j=0; j<core.addata->players.at(0)->srcTowers.size(); ++j)
    {
        srcTowerType i = core.addata->players.at(0)->srcTowers.at(j);
        if (i.isRoot)
        {
            QPixmap pix(32,32);
            pix.fill(Qt::transparent);
            QPainter p;
            p.begin(&pix);
            //p.fillRect(pix.rect(),Qt::transparent);
            p.setRenderHints(QPainter::SmoothPixmapTransform);
            p.drawImage(pix.rect(),*(i.image));
            p.end();
            cursors.append(pix);
            tbf->addButton(i.name+tr("\n %1 $").arg(i.cost));
            tbf->Button(cursors.size()-1)->setIcon(QIcon(pix));
            buildTowers.append(j);
        }
    }
    ui->gbTowers->layout()->addWidget(tbf);
    ui->verticalLayout_2->addWidget(morphtbf);
    connect(tbf,SIGNAL(click(int)),this,SLOT(towerBuild(int)));
    connect(tbf,SIGNAL(click(int)),tbf,SLOT(set_green(int)));
    connect(morphtbf,SIGNAL(click(int)),this,SLOT(towerMorph(int)));
    connect(graphics,SIGNAL(add_tow(QPoint,bool)),this,SLOT(add_tow(QPoint,bool)));
    //    connect(core.adtowers,SIGNAL(tower_killed(QPoint)),graphics,SLOT(towerKilled(QPoint)));
    connect(core.addata->aliens,SIGNAL(alien_killed(int,bool)),graphics,SLOT(alienKilled(int,bool)));
    connect(graphics,SIGNAL(cancel()),this,SLOT(cancel()));
    connect(graphics,SIGNAL(selected_tower_changed(QPoint)),this,SLOT(selectedTowerChanged(QPoint)));
    this->startTimer(50);
}


Form::~Form()
{
    delete ui;
}


void Form::on_pbNextWave_clicked()
{
    core.nextWave();
}


void Form::towerBuild(int index)
{
    buildTowerId = buildTowers.at(index);
    graphics->setBuilding(true);
    setCursor(QCursor(cursors.at(index)));
}


void Form::add_tow(QPoint pnt, bool shift)
{
    if (buildTowerId == -1) return;
    //    if (buildTowerId == -9)
    //    {
    //        graphics->towerKilled(pnt);
    //        core.adtowers->delTower(pnt);
    //        return;
    //    }
    core.adtowers->addTower(0,buildTowerId,pnt);
    if (!shift) cancel();
}


void Form::cancel()
{
    buildTowerId = -1;
    graphics->setBuilding(false);
    setCursor(Qt::ArrowCursor);
}


void Form::timerEvent(QTimerEvent *)
{
    for (int i=0; i<tbf->buttCount(); ++i)
    {
        bool b = core.adtowers->canBuild(buildTowers.at(i),0);
        tbf->Button(i)->setEnabled(b);
        if (b) tbf->set_green(i);
        else tbf->set_red(i);
    }
    QString twstr;
    QString alstr;
    if (!graphics->selectedTower().isNull())
    {
        TowerType &tw = core.addata->curTowers[graphics->selectedTower()];
        twstr = "\n\n  " + tw.src->name;
        if (tw.isBilding) twstr += tr("\n  build %1/%2").arg(tw.build).arg(tw.src->buildTime);
        twstr += tr("\n  player %1\n  exp %2").arg(tw.PlayerId).arg(tw.experience);
        twstr += tr("\n  reload %1/%2\n  alien %3").arg(tw.reload).arg(tw.src->reload).arg(tw.aim);
        twstr += tr("\n  cost %1\n  radius %2").arg(tw.src->cost).arg(tw.src->radius-1);
        if (!tw.src->triggers.isEmpty())
            twstr += tr("\n  damage %1").arg(tw.src->triggers.at(0).damage);
        for (int i=0; i<tw.src->morphs.size(); ++i)
        {
            bool m = core.adtowers->canMorph(tw.pos,i);
            morphtbf->Button(i)->setEnabled(m);
            if (m) morphtbf->set_green(i);
            else morphtbf->set_red(i);
        }
    }
    if (graphics->selectedAlien() != -1)
    {
        AlienType al = core.addata->aliens->curAliens.value(graphics->selectedAlien());
        alstr = "\n\n  " + al.src->name;
        if (al.src->isFlying) alstr += tr("\n  flying");
        else alstr += tr("\n  ground");
        alstr += tr("\n  health %1/%2").arg(al.health).arg(al.src->health);
        alstr += tr("\n  speed %1/%2").arg(al.speed).arg(al.src->speed);
        alstr += tr("\n  armor %1\n  regeneration %2").arg(al.src->armor).arg(al.src->regeneration);
        alstr += tr("\n  prise %1").arg(al.src->prise);
    }
    ui->lbInfo->setText(tr(" money %1 \n missing %2").arg(core.addata->players.at(0)->money).arg(core.addata->aliens->missingAliens())
                        + tr("\n wave %1").arg(core.addata->curWave+1)
                        + tr("\n  aliens %1").arg(core.addata->aliens->curAliens.size())
                        + tr("\n  towers %1").arg(core.addata->curTowers.size())
                        + tr("\n  splashes %1").arg(core.addata->curSplashes.size())
                        + twstr + alstr);

}


void Form::on_sbSpeed_valueChanged(double val)
{
    core.setSpeed(val);
}


void Form::selectedTowerChanged(QPoint id)
{
    morphtbf->deleteButtons();
    if (id.isNull())
    {
        ui->pbSell->hide();
        return;
    }
    ui->pbSell->show();
    srcTowerType * t = core.addata->curTowers[id].src;
    piForeachCA(i, t->morphs)
            morphtbf->addButton(i.morph->name+tr("\n %1 $, %2 exp").arg(i.cost).arg(i.expRequired));
}


void Form::towerMorph(int index)
{
    QPoint id(graphics->selectedTower());
    graphics->towerKilled(id);
    core.adtowers->morphTower(id,index);
    graphics->selectTower(core.addata->curTowers[id].item);
    selectedTowerChanged(graphics->selectedTower());
}


void Form::on_pbSell_clicked()
{
    QPoint pnt = graphics->selectedTower();
    if (!pnt.isNull())
    {
        graphics->towerKilled(pnt);
        core.adtowers->delTower(pnt);
        return;
    }
}
