#ifndef SECTION_HEADER_WIDGET_H
#define SECTION_HEADER_WIDGET_H

#include <QtGui>
#include "ui_sectionHeaderWidget.h"

class SectionHeaderWidget : public QWidget {
	Q_OBJECT

private:
	Ui::SectionHeaderWidget ui_;
	QString name_;
	bool isInChangedState_;
public:
	QPushButton * submitButton;
	QPushButton * cancelButton;

	SectionHeaderWidget(QWidget * parent) : QWidget(parent) {
		ui_.setupUi(this);	

		isInChangedState_ = false;

		submitButton = ui_.submitButton;
		cancelButton = ui_.cancelButton;
	}

	void setName(const std::wstring & name) {
		name_ = QString::fromUtf16(name.c_str());
		
		refreshAppearance();
	}

	void setAsChanged() {
		this->isInChangedState_ = true;
		refreshAppearance();
	}

	void setAsNormal() {
		this->isInChangedState_ = false;
		refreshAppearance();
	}

	void setButtonIcon(const std::string & path, int width, int height) {
        QIcon icon;
		icon.addFile(QString::fromUtf8(path.c_str()), QSize(), QIcon::Normal, QIcon::Off);

		ui_.sectionNameButton->setIcon(icon);
        ui_.sectionNameButton->setIconSize(QSize(width, height));
	}

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

	virtual void setGeometry(int ax, int ay, int awidth, int aheight) {
		QWidget::setGeometry(ax, ay, awidth, aheight);

		ui_.sectionBorderFrame->setGeometry(0, 0, this->width(), this->height());

		ui_.sectionNameButton->setGeometry(3, 3, this->width() - 180 - 10, this->height() - 6);
		ui_.sectionButtonsFrame->setGeometry(ui_.sectionNameButton->x() + ui_.sectionNameButton->width() + 4, 3, 180, this->height() - 6);
	}

	void setAsSmall() {
		resize(435, 30);

		ui_.sectionBorderFrame->setGeometry(0, 0, 435, 30);
		ui_.sectionBorderFrame->setStyleSheet(
			"#sectionBorderFrame {" 
			"	background-color: rgb(35, 35, 35, 140);"
			"	color: #ffffff;"
			"	padding: 5px;"
			"	border: 1px solid rgb(40, 40, 40);"
			"}"

			"#sectionBorderFrame[changed=true] {" 
			"background-color: rgb(100, 25, 25);"
			);

		ui_.sectionNameButton->setGeometry(3, 3, 245, 24);
		ui_.sectionNameButton->setStyleSheet(
			"#sectionNameButton {"
			"	background-color: rgb(35, 35, 35, 140);"
			"	color: rgb(255, 255, 255);"
			"	padding: 0px;"
			"	font-weight: bold;"
			"	border: 0;"
			"	}"
			);

		ui_.sectionButtonsFrame->setGeometry(250, 3, 180, 24);
		ui_.sectionButtonsFrame->setStyleSheet(
			"#sectionButtonsFrame {"
			"	background-color: rgb(35, 35, 35, 140);"
			"	color:  rgb(75, 120, 175);"
			"	padding: 0px;"
			"	font-weight: bold;"
			"	border: 0;"
			"}"
			);
		}
private:
	void refreshAppearance() {
		if (isInChangedState_) {
			ui_.sectionNameButton->setText(name_ + QString::fromUtf16(L" (Změněno)"));

			ui_.sectionNameButton->setProperty("changed", true);
			ui_.sectionNameButton->setStyleSheet(ui_.sectionNameButton->styleSheet());

			ui_.sectionButtonsFrame->setProperty("changed", true);
			ui_.sectionButtonsFrame->setStyleSheet(ui_.sectionButtonsFrame->styleSheet());

			ui_.sectionBorderFrame->setProperty("changed", true);
			ui_.sectionBorderFrame->setStyleSheet(ui_.sectionBorderFrame->styleSheet());

			ui_.submitButton->setEnabled(true);
			ui_.cancelButton->setText(QString::fromUtf16(L"Zrušit změny"));
			ui_.cancelButton->setEnabled(true);
		}
		else {
			ui_.sectionNameButton->setText(name_);

			// Set property as invalid QVariant to unset it
			ui_.sectionNameButton->setProperty("changed", QVariant());
			ui_.sectionNameButton->setStyleSheet(ui_.sectionNameButton->styleSheet());

			ui_.sectionButtonsFrame->setProperty("changed", QVariant());
			ui_.sectionButtonsFrame->setStyleSheet(ui_.sectionButtonsFrame->styleSheet());

			ui_.sectionBorderFrame->setProperty("changed", QVariant());
			ui_.sectionBorderFrame->setStyleSheet(ui_.sectionBorderFrame->styleSheet());

			ui_.submitButton->setEnabled(false);
			ui_.cancelButton->setText(QString::fromUtf16(L"Aktualizovat"));
			ui_.cancelButton->setEnabled(true);
		}
	}
};

#endif