#include "shieldsWidget.h"
#include "softProj\settings.h"

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

	ui_.shieldsSection->setName(L"Štíty");

	ui_.nameButton->setText(QString::fromUtf16(L"Energie a nabíjení"));
	ui_.energyLabel->setText(QString::fromUtf16(L"Aktuální energie a akumulátor"));
	ui_.energySlider->setUnit(L"TJ");
	ui_.energySlider->setEnabled(false);

	ui_.leakLabel->setText(QString::fromUtf16(L"Samovolný unik"));
	leakUnit_ = " MW";

	ui_.chargingSpeedLabel->setText(QString::fromUtf16(L"Rychlost nabíjení"));
	chargingSpeedUnit_ = " TW";

	ui_.consumptionLabel->setText(QString::fromUtf16(L"Spotřeba"));
	consumptionUnit_ =  " GW";

	ui_.damageStatusLabel->setText(QString::fromUtf16(L"Stav zařízení"));
	ui_.damageStatusSlider->setLimits(0, 100);
	ui_.damageStatusSlider->setEnabled(false);
	ui_.damageStatusSlider->setUnit(L"%");

	availableButtonStyle_ = Settings::instance().getStyle(STYLE_AVAILABLE_DEVICE_BUTTON);
	unavailableButtonStyle_ = Settings::instance().getStyle(STYLE_UNAVAILABLE_DEVICE_BUTTON	);
	availableDamageStyle_ = Settings::instance().getStyle(STYLE_AVAILABLE_DEVICE_DAMAGE_LABEL);
	unavailableDamageStyle_ = Settings::instance().getStyle(STYLE_UNAVAILABLE_DEVICE_DAMAGE_LABEL);

	bool connected = true;
	connected = connected && connect(ui_.unmountButton, SIGNAL(clicked(bool)), this, SLOT(unmountButtonClickedSlot(bool)));
	assert(connected && "Signals not connected");

	ui_.energySlider->setFontColor(QColor::fromRgb(40, 40, 40));
}

void ShieldsWidget::unmountButtonClickedSlot(bool checked) {
	emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_DEFLECTOR_SHIELDS);
}

void ShieldsWidget::setState(const ShieldsDetails & details) {
	this->rememberUpdateTime();
	this->state_ = details;

	ui_.solidicityValue->setText(Settings::instance().formatNumber(details.solidityActual));	

	ui_.energySlider->setLimits(0, details.accumulatorActual);
	ui_.energySlider->setValue(details.energyActual);			
		
	ui_.chargingSpeedValue->setText(Settings::instance().formatNumber(details.chargingSpeedActual) + chargingSpeedUnit_);	
		
	ui_.damageStatusSlider->setValue(details.damageStatus * 100);
		
	ui_.consumptionValue->setText(Settings::instance().formatNumber((unsigned long) (details.energyConsumptionActual * 100)) + consumptionUnit_);
	ui_.leakValue->setText(Settings::instance().formatNumber(details.energyLeakActual) + leakUnit_);

	setAvailable(details.avalDeflectorShields);	
}

void ShieldsWidget::setAvailable(bool available) {
	if (available) {
		ui_.nameButton->setStyleSheet(availableButtonStyle_);
		ui_.damageStatusLabel->setStyleSheet(availableDamageStyle_);
		ui_.damageStatusLabel->setText(QString::fromUtf16(L"Stav zařízení"));
		ui_.damageStatusSlider->setVisible(true);
		ui_.valuesFrame->setVisible(true);
		ui_.unmountButton->setEnabled(true);
		ui_.unmountButton->setText(QString::fromUtf16(L"Odmontovat"));
		ui_.shieldsAccumulatorFrame->setVisible(true);
	}
	else {
		ui_.nameButton->setStyleSheet(unavailableButtonStyle_);
		ui_.damageStatusLabel->setStyleSheet(unavailableDamageStyle_);
		ui_.damageStatusLabel->setText(QString::fromUtf16(L"Zařízení není k dispozici"));
		ui_.damageStatusSlider->setVisible(false);
		ui_.valuesFrame->setVisible(false);
		ui_.unmountButton->setEnabled(false);
		ui_.unmountButton->setText(QString::fromUtf16(L"Nenamontováno"));
		ui_.shieldsAccumulatorFrame->setVisible(false);
	}
}

void ShieldsWidget::notifyUpdate() {
	double difference = getDifferenceFromUpdateTime();

	ui_.energySlider->setValue(getUpdatedValue(state_.energyActual, state_.gradient, difference));
}

ShieldsWidget::~ShieldsWidget() {
}