#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent) :
		QMainWindow(parent),
		ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	adloader = new Loader(this);
	adloader->load("data2.xml");
	alModel = new AlienModel(adloader->aliens.values());
	ui->lvAliens->setModel((QAbstractItemModel * )alModel);
	connect(ui->lvAliens->selectionModel(),SIGNAL(currentChanged(QModelIndex, QModelIndex)),this,SLOT(lvAliens_select(QModelIndex, QModelIndex)));
	ui->lvAliens->selectionModel()->setCurrentIndex(ui->lvAliens->model()->index(0,0),QItemSelectionModel::Select);
}


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


void MainWindow::changeEvent(QEvent *e)
{
	QMainWindow::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		ui->retranslateUi(this);
		break;
	default:
		break;
	}
}


void MainWindow::on_cbAlArmor_toggled(bool checked)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	ui->sbAlArmor->setEnabled(checked);
	if (checked) adloader->aliens[id].armor = ui->sbAlArmor->value();
	else adloader->aliens[id].armor = 0;
}


void MainWindow::on_cbAlRegeneration_toggled(bool checked)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	ui->sbAlregeneration->setEnabled(checked);
	if (checked) adloader->aliens[id].regeneration = ui->sbAlregeneration->value();
	else adloader->aliens[id].regeneration = 0;
}


void MainWindow::lvAliens_select(QModelIndex index, QModelIndex)
{
	tbAlien al = adloader->aliens.values().at(index.row());
	ui->leAlName->setText(al.name);
	ui->sbAlHealth->setValue(al.health);
	ui->sbAlArmor->setValue(al.armor);
	if (al.armor > 0) ui->cbAlArmor->setChecked(true);
	else ui->cbAlArmor->setChecked(false);
	ui->sbAlSpeed->setValue(al.speed);
	ui->sbAlPrise->setValue(al.prise);
	ui->sbAlregeneration->setValue(al.regeneration);
	if (al.regeneration > 0) ui->cbAlRegeneration->setChecked(true);
	else ui->cbAlRegeneration->setChecked(false);
	ui->sbAlScore->setValue(al.score);
	ui->cbAlIsFly->setChecked(al.isFlying);
}


void MainWindow::on_pbSave_clicked()
{
	adloader->save("data2.xml");
	adloader->load("data2.xml");
	alModel->refresh(adloader->aliens.values());
}

void MainWindow::on_sbAlHealth_valueChanged(double value)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	adloader->aliens[id].health = value;
}

void MainWindow::on_sbAlArmor_valueChanged(double value)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	adloader->aliens[id].armor = value;
}

void MainWindow::on_sbAlregeneration_valueChanged(double value)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	adloader->aliens[id].regeneration = value;
}

void MainWindow::on_sbAlSpeed_valueChanged(double value)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	adloader->aliens[id].speed = value;
}

void MainWindow::on_sbAlPrise_valueChanged(int val)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	adloader->aliens[id].prise = val;
}

void MainWindow::on_sbAlScore_valueChanged(int val)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	adloader->aliens[id].score = val;
}

void MainWindow::on_cbAlIsFly_clicked(bool checked)
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	adloader->aliens[id].isFlying = checked;
}

void MainWindow::on_leAlName_editingFinished()
{
	int id = adloader->aliens.values().at(ui->lvAliens->currentIndex().row()).id;
	adloader->aliens[id].name = ui->leAlName->text();
	alModel->refresh(adloader->aliens.values());
	ui->lvAliens->setModel((QAbstractItemModel * )alModel);
}

void MainWindow::on_pbAlAdd_clicked()
{
	int i = 1;
	while (adloader->aliens.contains(i)) i++;
	tbAlien al;
	al.id = i;
	adloader->aliens.insert(al.id,al);
	int index = adloader->aliens.keys().indexOf(i);
	alModel->refresh(adloader->aliens.values());
	ui->lvAliens->setModel((QAbstractItemModel * )alModel);
	ui->lvAliens->reset();
	ui->lvAliens->selectionModel()->setCurrentIndex(ui->lvAliens->model()->index(index,0),QItemSelectionModel::Select);
}

void MainWindow::on_pbAlDel_clicked()
{
	QMessageBox msgBox;
	int id = adloader->aliens.keys().at(ui->lvAliens->currentIndex().row());
	msgBox.setText(tr("Delete alien %1 id=%2").arg(adloader->aliens.value(id).name).arg(id));
	msgBox.setInformativeText(tr("Are you sure?"));
	msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
	msgBox.setDefaultButton(QMessageBox::Ok);
	int ret = msgBox.exec();
	if (ret == QMessageBox::Ok)
	{
		adloader->aliens.remove(id);
		alModel->refresh(adloader->aliens.values());
		ui->lvAliens->setModel((QAbstractItemModel * )alModel);
		ui->lvAliens->reset();
		ui->lvAliens->selectionModel()->setCurrentIndex(ui->lvAliens->model()->index(0,0),QItemSelectionModel::Select);
	}
}
