#include <qwidget.h>
#include <qobject.h>
#include <qframe.h>
#include <exception>
#include <map>
#include <vector>

#include "ScienceNode.h"
#include "ui_ScienceTreeWidget.h"
#include "ScienceTree.h"
#include "ListDetailsViewWidget.h"

ListDetailsViewWidget::CompositeItemsRow::CompositeItemsRow(QString & style)
{
	item_ = NULL;
	m_isVisible = false;

	nameLabel.setStyleSheet(style);
	pointsLabel.setStyleSheet(style);
	percentsBox.setStyleSheet(style);

	nameLabel.setGeometry(nameLabel.x(), nameLabel.y(), nameLabel.width(), 20);
	pointsLabel.setGeometry(pointsLabel.x(), pointsLabel.y(), pointsLabel.width(), 20);
	percentsBox.setGeometry(percentsBox.x(), percentsBox.y(), percentsBox.width(), 20);

	nameLabel.setMaximumHeight(20);
	pointsLabel.setMaximumHeight(20);
	percentsBox.setMaximumHeight(20);
}

void ListDetailsViewWidget::CompositeItemsRow::hide()
{
	nameLabel.hide();			
	percentsBox.hide();
	pointsLabel.hide();

	m_isVisible = false;
}

bool ListDetailsViewWidget::CompositeItemsRow::isVisible()
{
	return m_isVisible;
}

void ListDetailsViewWidget::CompositeItemsRow::show()
{
	nameLabel.show();			
	percentsBox.show();
	pointsLabel.show();

	m_isVisible = true;
}

void ListDetailsViewWidget::CompositeItemsRow::adjustMaximum(int freePercents)
{
	if (item_)
	{
		percentsBox.setMaximum(item_->getAssignedPercents() + freePercents);
	}
}

void ListDetailsViewWidget::CompositeItemsRow::setup(CompositeItem * item, int freePercents)
{
	show();

	item_ = item;

	nameLabel.setText(item->getGraphicalName());
	adjustMaximum(freePercents);
	percentsBox.setValue(item->getAssignedPercents());			
	pointsLabel.setText(QString::number(item->getCreatedPoints()));

	QObject::connect(&percentsBox, SIGNAL(valueChanged(int)), item, SLOT(assignedPercentsChanged(int)));			
}

void ListDetailsViewWidget::CompositeItemsRow::setupBack()
{
	QObject::disconnect(&percentsBox, SIGNAL(valueChanged(int)), item_, SLOT(assignedPercentsChanged(int)));	

	item_ = NULL;
}

ListDetailsViewWidget::ListDetailsViewWidget(QGridLayout * layout, int maxRows)
{
	layout_ = layout;
	freePercents_ = 0;

	itemsRowsCount_ = maxRows;
	itemsRows_ = new CompositeItemsRow*[itemsRowsCount_];

	QString style = QString::fromUtf8(
		"QLabel {"
		"	background-color: rgb(35, 35, 35, 140);"
		"	padding: 3px;"
		"	color: #ffffff;"
		"}"
		
		"QSpinBox {"
		"	color: rgb(255, 255, 255);"
		"	border: 1px solid rgb(40, 40, 40);"
		"	background-color: rgb(35, 35, 35, 140);"
		"	padding: 2px;"
		"}"

		"QSpinBox::up-arrow:disabled, QSpinBox::up-arrow:off {"
		"   color: rgb(252, 252, 252);"
		"}"

		"QSpinBox::down-arrow:disabled, QSpinBox::down-arrow:off {"
		"   color: rgb(252, 252, 252);"
		"}"

		"QSpinBox:disabled {"
		"	color: rgb(255, 255, 255);"
		"	border: 1px solid rgb(40, 40, 40)"
		"	background-color: rgb(100, 0, 0);"
		"	padding: 2px;"
		"}");

	for (int i = 0; i < itemsRowsCount_; ++i)
	{
		CompositeItemsRow * addedRow = new CompositeItemsRow(style);
		itemsRows_[i] = addedRow;

		layout_->addWidget(&addedRow->nameLabel, 2 * i + 1, 0, 1, 3);
		layout_->addWidget(&addedRow->pointsLabel, 2 * i + 2, 0, 1, 2);
		layout_->addWidget(&addedRow->percentsBox, 2 * i + 2, 2);		
	
		layout_->setRowStretch(2 * i + 1, 1);
		layout_->setRowStretch(2 * i + 2, 1);
	}

	layout_->setRowStretch(0, 1);
}

void ListDetailsViewWidget::setupRow(CompositeItem * item, int index)
{
	itemsRows_[index]->setup(item, freePercents_);
}

void ListDetailsViewWidget::setupRowBack(int index)
{
	itemsRows_[index]->setupBack();
}	

void ListDetailsViewWidget::hideAllRowsFrom(int index)
{
	for (int i = index; i < itemsRowsCount_; ++i)
	{
		itemsRows_[i]->hide();
	}
}

void ListDetailsViewWidget::hide()
{
	hideAllRowsFrom(0);		
	QWidget::hide();
}

void ListDetailsViewWidget::showContent()
{
}

void ListDetailsViewWidget::setFreePercents(int value)
{
	freePercents_ = value;

	for (int i = 0; i < itemsRowsCount_; ++i)
	{
		if (itemsRows_[i])
		{
			if (itemsRows_[i]->isVisible())
			{
				itemsRows_[i]->adjustMaximum(value);
			}
		}
	}
}

ListDetailsViewWidget::~ListDetailsViewWidget()
{
	for (int i = 0; i < itemsRowsCount_; ++i)
	{
		if (itemsRows_[i])
		{
			delete itemsRows_[i];
		}
	}

	delete [] itemsRows_;
}