#include "fightWidgetRowWidget.h"

void FightWidgetRowWidget::initIcons() {
	buildIcon.addFile(QString::fromUtf8(":/pictures/images/others/hangary.png"), QSize(), QIcon::Normal, QIcon::Off);
	notokIcon.addFile(QString::fromUtf8(":/pictures/images/trade/notok.png"), QSize(), QIcon::Normal, QIcon::Off);
	shipIcon.addFile(QString::fromUtf8(":/pictures/images/others/shuttle-icon.png"), QSize(), QIcon::Normal, QIcon::Off);
	weaponIcon.addFile(QString::fromUtf8(":/pictures/images/details/zbrane.png"), QSize(), QIcon::Normal, QIcon::Off);
	shieldIcon.addFile(QString::fromUtf8(":/pictures/images/details/stity.png"), QSize(), QIcon::Normal, QIcon::Off);
	weaponShieldIcon.addFile(QString::fromUtf8(":/pictures/images/fight/zbrane-stity.png"), QSize(), QIcon::Normal, QIcon::Off);
	attackOnMeIcon.addFile(QString::fromUtf8(":/pictures/images/fight/attack.png"), QSize(), QIcon::Normal, QIcon::Off);
	baseIcon.addFile(QString::fromUtf8(":/pictures/images/details/modelZakladna.png"), QSize(), QIcon::Normal, QIcon::Off);	
	shellIcon.addFile(QString::fromUtf8(":/pictures/images/trade/hazel-nut-icon.png"), QSize(), QIcon::Normal, QIcon::Off);	
	attackOnMeShieldIcon.addFile(QString::fromUtf8(":/pictures/images/fight/cross-stity.png"), QSize(), QIcon::Normal, QIcon::Off);
}

void FightWidgetRowWidget::itemClickedSlot(bool checked) {
	emit clickedSignal(itemId_);
}

void FightWidgetRowWidget::itemDoubleClickedSlot() {
	emit doubleClickedSignal(itemId_);
}

FightWidgetRowWidget::FightWidgetRowWidget(QWidget * parent, EFortressType itemType) : QWidget(parent) {
	ui_.setupUi(this);

	bool connected = true;
	connected = connected && connect(ui_.nameButton, SIGNAL(clicked(bool)), this, SLOT(itemClickedSlot(bool)));
	connected = connected && connect(ui_.imageButton, SIGNAL(clicked(bool)), this, SLOT(itemClickedSlot(bool)));		

	connected = connected && connect(ui_.nameButton, SIGNAL(doubleClickedSignal()), this, SLOT(itemDoubleClickedSlot()));
	connected = connected && connect(ui_.imageButton, SIGNAL(doubleClickedSignal()), this, SLOT(itemDoubleClickedSlot()));		
	assert(connected && "Signals not connected");

	initIcons();

	if (itemType == FORTRESS_TYPE_SHIP) {
		ui_.nameButton->setIcon(shipIcon);
	}
	else if (itemType == FORTRESS_TYPE_BASE) {
		ui_.nameButton->setIcon(baseIcon);
	}
}

void FightWidgetRowWidget::setItemName(const QString & itemName) {
	ui_.nameButton->setText(itemName);
}

void FightWidgetRowWidget::setItemId(id_t id) {
	this->itemId_ = id;
}

id_t FightWidgetRowWidget::getItemId() {
	return itemId_;
}

void FightWidgetRowWidget::setAsGreen() {
	backgroundProperty_ = "owned";
	refreshBackground();
}

void FightWidgetRowWidget::setAsBlue() {
	backgroundProperty_ = "enemy";
	refreshBackground();
}

void FightWidgetRowWidget::setAsBuildShell(bool isShell) {
	if (isShell) {
		ui_.imageButton->setIcon(shellIcon);
	}
	else {
		ui_.imageButton->setIcon(QIcon());
	}
}

void FightWidgetRowWidget::setAsFight(bool isAttacking, bool isAttackingOnSelf, bool isUnderAttack) {
	if (isAttackingOnSelf && isUnderAttack) {
		ui_.imageButton->setIcon(attackOnMeShieldIcon);
		ui_.imageButton->setIconSize(QSize(38, 20));
	}	
	else if (isAttacking && isUnderAttack) {
		ui_.imageButton->setIcon(weaponShieldIcon);
		ui_.imageButton->setIconSize(QSize(38, 20));
	}
	else if (isAttackingOnSelf) {
		ui_.imageButton->setIcon(attackOnMeIcon);
		ui_.imageButton->setIconSize(QSize(20, 20));
	}
	else if (isAttacking) {
		ui_.imageButton->setIcon(weaponIcon);
		ui_.imageButton->setIconSize(QSize(20, 20));
	}
	else if (isUnderAttack) {
		ui_.imageButton->setIcon(shieldIcon);
		ui_.imageButton->setIconSize(QSize(20, 20));
	}
	else {
		ui_.imageButton->setIcon(QIcon());
	}
}

void FightWidgetRowWidget::refreshBackground() {
	ui_.nameButton->setProperty("partner", backgroundProperty_);
	ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());

	ui_.frame->setProperty("partner", backgroundProperty_);
	ui_.frame->setStyleSheet(ui_.frame->styleSheet());
}

void FightWidgetRowWidget::setAsCanBuild(bool canBuild) {
	if (canBuild) {
		ui_.imageButton->setVisible(true);
		ui_.imageButton->setIcon(buildIcon);
		ui_.imageButton->setIconSize(QSize(20, 20));
	}
	else {
		ui_.imageButton->setVisible(true);
		ui_.imageButton->setIcon(notokIcon);
		ui_.imageButton->setIconSize(QSize(20, 20));
	}
}

void FightWidgetRowWidget::setAsClicked(bool clicked) {
	if (clicked) {
		ui_.nameButton->setProperty("partner", "attackingEnemy");		
		ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());

		ui_.frame->setProperty("partner", "attackingEnemy");
		ui_.frame->setStyleSheet(ui_.frame->styleSheet());
	}
	else {
		refreshBackground();
	}
}

void FightWidgetRowWidget::mouseDoubleClickEvent(QMouseEvent * mouseEvent) {
	emit doubleClickedSignal(itemId_);
}