#include "androidsWidget.h"
#include "androidsSectionWidget.h"
#include "softProj\DevicesWidget\combinedSlider.h"
#include "protocol.h"
#include "softProj\helpers.h"

void AndroidsSectionWidget::priorityChangedSlot(int value) {
	recolorFromPriority();

	emit settingsChangedSignal();
}

void AndroidsSectionWidget::ratioChangedSlot(int value) {
	emit settingsChangedSignal();
}

void AndroidsSectionWidget::setEnergyRatio(double value) {
	this->ui_.ratioSlider->setValue(value);
}

void AndroidsSectionWidget::setEnergyPriority(int value) {
	this->ui_.prioritySlider->setValue(value);	
}

void AndroidsSectionWidget::setBackgroundsHolder(DeviceBackgroundsHolder * backgroundsHolder) {
	backgroundsHolder_ = backgroundsHolder;	
}

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

	minimumRatio = 0;
	maximumRatio = 0;

	minimumPriority = 1;
	maximumPriority = 1;	

	ui_.ratioSlider->setName(L"Podíl");
	ui_.prioritySlider->setName(L"Priorita");

	ui_.ratioSlider->setLimits(minimumRatio, maximumRatio);
	ui_.prioritySlider->setLimits(minimumPriority, maximumPriority);	

	setEnergyRatio(minimumRatio);
	setEnergyPriority(minimumPriority);
	
	bool connected = true;
	
	connected = connected && connect(
		ui_.prioritySlider, SIGNAL(valueChanged(int)),
		this, SLOT(priorityChangedSlot(int))
	);

	connected = connected && connect(
		ui_.ratioSlider, SIGNAL(valueChanged(int)),
		this, SLOT(ratioChangedSlot(int))
	);

	ui_.ratioSlider->setFontColor(QColor::fromRgb(40, 40, 40));
	ui_.prioritySlider->setFontColor(QColor::fromRgb(40, 40, 40));

	assert(connected && "Signals not connected");
}

void AndroidsSectionWidget::recolorFromPriority() {
	ui_.deviceFrame->setAutoFillBackground(true);

	int priority = getEnergyPriority();
	
	QString backgroundStyle = backgroundsHolder_->getBackgroundFromPriority(priority, true);
	QString foregroundStyle = backgroundsHolder_->getForegroundFromPriority(priority, true);

	ui_.deviceFrame->setStyleSheet(backgroundStyle);
}


void AndroidsSectionWidget::setSectionName(const std::wstring & sectionName) {
	ui_.nameButton->setText(QString::fromUtf16(sectionName.c_str()));
}	

void AndroidsSectionWidget::setSectionIcon(const std::string & deviceIconPath) {
	ui_.nameButton->setLayoutDirection(Qt::RightToLeft);
	setButtonIconFromResources(ui_.nameButton, deviceIconPath, 20);
}

int AndroidsSectionWidget::getEnergyRatio() {
	return ui_.ratioSlider->getValue();
}

int AndroidsSectionWidget::getEnergyPriority() {
	return ui_.prioritySlider->getValue();
}

void AndroidsSectionWidget::setState(androids_assignment & state, androids_status & fullState) {
	ui_.prioritySlider->setMinimum(fullState.androidsMinPriority);
	ui_.prioritySlider->setMaximum(fullState.androidsMaxPriority);

	ui_.ratioSlider->setMinimum(fullState.androidsMinRatio);
	ui_.ratioSlider->setMaximum(fullState.androidsMaxRatio);
}

androids_assignment AndroidsSectionWidget::getState() {
	androids_assignment state;

	state.priority = ui_.prioritySlider->getValue();
	state.ratio = ui_.ratioSlider->getValue();

	return state;
}