#ifndef TRADE_PARTNER_BUTTON_H
#define TRADE_PARTNER_BUTTON_H

#include "ui_tradePartnerButton.h"
#include "tradeItem.h"
#include "softProj\helpers.h"
#include <QtGui>

class TradePartnerButton : public QWidget {
	Q_OBJECT
private:
	bool isBothButtonVisible_;

	Ui::TradePartnerButton ui_;

	QIcon shipIcon_;
	QIcon baseIcon_;
	QIcon userIcon_;
	QIcon notokIcon_;
	QIcon spaceIcon_;

	bool isOwned_;
	bool isShip_;

	id_t fortressId_;
	std::string fortressName_;
	std::string realmName_;
public:
	TradePartnerButton(QWidget * parent) : QWidget(parent) {
		ui_.setupUi(this);

		shipIcon_ = getIcon("../images/others/shuttle-icon.png");
		baseIcon_ = getIcon("../images/details/tovarny.png");
		userIcon_ = getIcon("../images/others/binIcon.png");
		notokIcon_ = getIcon("../images/trade/notok.png");
		spaceIcon_ = getIcon("../images/details/zakladna.png");

		isBothButtonVisible_ = false;
		
		setNone();
	}

	void setPartner(id_t fortressId, const std::string & fortressName, const std::string & realmName, bool isOwned, bool isShip) {
		isBothButtonVisible_ = true;

		QString ownedString = isOwned ? "owned" : "enemy";
		QIcon fortressIcon = isShip ? shipIcon_ : baseIcon_;

		int button1Width = (ui_.frame->width() - 2) / 2;
		int button2Width = button1Width;

		if ((ui_.frame->width()) % 2 != 0) {
			button2Width += 1;
		}

		int buttonHeight = ui_.frame->height() - 2;

		ui_.nameButton->setGeometry(1, 1, button1Width, buttonHeight);
		ui_.nameButton->setProperty("partner", ownedString);
		ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());
		ui_.nameButton->setIcon(shipIcon_);
		ui_.nameButton->setText(QString::fromUtf8(fortressName.c_str()));
		ui_.nameButton->setVisible(true);		

		ui_.userButton->setGeometry(1 + button2Width, 1, button2Width, buttonHeight);
		ui_.userButton->setProperty("partner", ownedString);
		ui_.userButton->setStyleSheet(ui_.userButton->styleSheet());
		ui_.userButton->setIcon(userIcon_);
		ui_.nameButton->setIconSize(QSize(20, 20));
		ui_.userButton->setText(QString::fromUtf8(realmName.c_str()));
		ui_.userButton->setVisible(true);

		this->fortressId_ = fortressId;		
		this->fortressName_ = fortressName;
		this->realmName_ = realmName;

		this->isOwned_ = isOwned;
		this->isShip_ = isShip;

		if (isShip) {
			ui_.nameButton->setIcon(shipIcon_);
		}
		else {
			ui_.nameButton->setIcon(baseIcon_);
		}
	}

	void setNone() {
		isBothButtonVisible_ = false;

		int buttonWidth = ui_.frame->width() - 2;
		int buttonHeight = ui_.frame->height() - 2;

		ui_.nameButton->setGeometry(1, 1, buttonWidth, buttonHeight);
		ui_.nameButton->setProperty("partner", "none");
		ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());
		ui_.nameButton->setIcon(notokIcon_);
		ui_.nameButton->setIconSize(QSize(14, 14));
		ui_.nameButton->setText(QString::fromUtf16(L"Nevybral jsi žádnou pevnost"));
		ui_.nameButton->setVisible(true);	

		ui_.userButton->setVisible(false);

		this->fortressId_ = 0;		
		this->fortressName_ = "";
		this->realmName_ = "";

		this->isOwned_ = false;
		this->isShip_ = false;
	}

	void setNone(const std::wstring & text) {
		isBothButtonVisible_ = false;

		int buttonWidth = ui_.frame->width() - 2;
		int buttonHeight = ui_.frame->height() - 2;

		ui_.nameButton->setGeometry(1, 1, buttonWidth, buttonHeight);
		ui_.nameButton->setProperty("partner", "none");
		ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());
		ui_.nameButton->setIcon(notokIcon_);
		ui_.nameButton->setIconSize(QSize(14, 14));
		ui_.nameButton->setText(QString::fromUtf16(text.c_str()));
		ui_.nameButton->setVisible(true);	

		ui_.userButton->setVisible(false);

		this->fortressId_ = 0;		
		this->fortressName_ = "";
		this->realmName_ = "";

		this->isOwned_ = false;
		this->isShip_ = false;
	}

	void setSpace() {
		isBothButtonVisible_ = false;

		int buttonWidth = ui_.frame->width() - 2;
		int buttonHeight = ui_.frame->height() - 2;

		ui_.nameButton->setGeometry(1, 1, buttonWidth, buttonHeight);
		ui_.nameButton->setProperty("partner", "space");
		ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());
		ui_.nameButton->setIcon(spaceIcon_);
		ui_.nameButton->setIconSize(QSize(20, 20));
		ui_.nameButton->setText(QString::fromUtf16(L"Vyhazování předmětů do vesmíru"));
		ui_.nameButton->setVisible(true);	

		ui_.userButton->setVisible(false);

		this->fortressId_ = 0;		
		this->fortressName_ = "";
		this->realmName_ = "";

		this->isOwned_ = false;
		this->isShip_ = false;
	}

	void setFortressClass(id_t fortressClassId, const std::string & fortressClassName, bool isShip) {
		isBothButtonVisible_ = false;

		int buttonWidth = ui_.frame->width() - 2;
		int buttonHeight = ui_.frame->height() - 2;

		ui_.nameButton->setGeometry(1, 1, buttonWidth, buttonHeight);
		ui_.nameButton->setProperty("partner", "enemy");
		ui_.nameButton->setStyleSheet(ui_.nameButton->styleSheet());
		ui_.nameButton->setIcon(baseIcon_);
		ui_.nameButton->setIconSize(QSize(18, 18));
		ui_.nameButton->setText(QString::fromUtf8(fortressClassName.c_str()));
		ui_.nameButton->setVisible(true);	

		ui_.userButton->setVisible(false);

		this->fortressId_ = fortressClassId;		
		this->fortressName_ = fortressClassName;
		this->realmName_ = "";
		this->isShip_ = isShip;

		this->isOwned_ = false;

		if (isShip) {
			ui_.nameButton->setIcon(shipIcon_);
		}
		else {
			ui_.nameButton->setIcon(baseIcon_);
		}
	}

	id_t getFortressId() const {
		return fortressId_;
	}

	std::string getFortressName() const {
		return fortressName_;
	}

	std::string getRealmName() const {
		return realmName_;
	}

	bool isOwned() const {
		return isOwned_;
	}

	bool isShip() const {
		return isShip_;
	}

	virtual void setGeometry(int x, int y, int width, int height) {
		QWidget::setGeometry(x, y, width, height);

		if (isBothButtonVisible_) {
			int button1Width = (ui_.frame->width() - 2) / 2;
			int button2Width = button1Width;

			if ((ui_.frame->width()) % 2 != 0) {
				button2Width += 1;
			}

			int buttonHeight = ui_.frame->height() - 2;

			ui_.frame->setGeometry(0, 0, width, height);
			ui_.nameButton->setGeometry(1, 1, button1Width, buttonHeight);
			ui_.nameButton->setGeometry(button1Width + 1, 1, button2Width, buttonHeight);
		}
		else {
			int buttonWidth = ui_.frame->width() - 2;
			int buttonHeight = ui_.frame->height() - 2;

			ui_.frame->setGeometry(0, 0, width, height);
			ui_.nameButton->setGeometry(1, 1, buttonWidth, buttonHeight);
		}
	}

	virtual void setGeometry(const QRect & rect) {
		QWidget::setGeometry(rect);
		setGeometry(rect.x(), rect.y(), rect.width(), rect.height());
	}
};

#endif