#include "sensorsWidget.h"
#include "softProj\settings.h"
#include <cassert>
#include "softProj\NavigationWidget\unitConvertor.h"

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

	ui_.sectionHeader->setName(L"Senzory");

	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(unmountHRSensorsButtonClickedSlot(bool)));
	connected = connected && connect(ui_.lrSensorsUnmountButton, SIGNAL(clicked(bool)), this, SLOT(unmountLRSensorsButtonClickedSlot(bool)));
	assert(connected && "Signals not connected");

	ui_.damageSlider->setMinimum(0);
	ui_.damageSlider->setMaximum(100);
	ui_.damageSlider->setUnit(L"%");

	ui_.lrSensorsDamageSlider->setMinimum(0);
	ui_.lrSensorsDamageSlider->setMaximum(100);
	ui_.lrSensorsDamageSlider->setUnit(L"%");

	rangeUnit_ = " km";
	weightUnit_ = " kg";
	consumptionUnit_ = " GW";
}

void SensorsWidget::unmountHRSensorsButtonClickedSlot(bool checked) {
	emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_HIGH_RESOLUTION_SENSORS);
}

void SensorsWidget::unmountLRSensorsButtonClickedSlot(bool checked) {
	emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_LONG_RANGE_SENSORS);
}

void SensorsWidget::setState(const sensors_data & hrSensorsData, const sensors_data & lrSensorsData) {
	this->rememberUpdateTime();

	ui_.consumptionValue->setText(Settings::instance().formatNumber((unsigned long) (hrSensorsData.consumption * 1000)) + consumptionUnit_);	
	ui_.levelValue->setText(Settings::instance().formatNumber(hrSensorsData.level));	
	ui_.rangeValue->setText(QString::fromUtf16(formatDistance(hrSensorsData.range, 4).c_str()));
	ui_.weightValue->setText(Settings::instance().formatNumber(hrSensorsData.weight) + weightUnit_);	
		
	ui_.damageSlider->setValue(hrSensorsData.status * 100);		
	setHrSensorsAvailable(hrSensorsData.available);

	ui_.lrSensorsConsumptionValue->setText(Settings::instance().formatNumber((unsigned long) (lrSensorsData.consumption * 1000)) + consumptionUnit_);	
	ui_.lrSensorsLevelValue->setText(Settings::instance().formatNumber(lrSensorsData.level));	
	ui_.lrSensorsRangeValue->setText(Settings::instance().formatNumber((unsigned long long) lrSensorsData.range) + rangeUnit_);	
	ui_.lrSensorsRangeValue->setText(QString::fromUtf16(formatDistance(lrSensorsData.range, 4).c_str()));
	ui_.lrSensorsWeightValue->setText(Settings::instance().formatNumber(lrSensorsData.weight) + weightUnit_);	
		
	ui_.lrSensorsDamageSlider->setValue(lrSensorsData.status * 100);		
	setLRSensorsAvailable(lrSensorsData.available);

	this->hrSensorsState_ = hrSensorsData;
	this->lrSensorsState_ = lrSensorsData;
}

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

void SensorsWidget::setLRSensorsAvailable(bool available) {
	if (available) {
		ui_.lrSensorsNameButton->setStyleSheet(availableButtonStyle_);
		ui_.lrSensorsDamageLabel->setStyleSheet(availableDamageStyle_);
		ui_.lrSensorsDamageLabel->setText(QString::fromUtf16(L"Stav zařízení"));
		ui_.lrSensorsDamageSlider->setVisible(true);
		ui_.lrSensorsValueFrame->setVisible(true);
		ui_.lrSensorsUnmountButton->setEnabled(true);
		ui_.lrSensorsUnmountButton->setText(QString::fromUtf16(L"Odmontovat"));
	}
	else {
		ui_.lrSensorsNameButton->setStyleSheet(unavailableButtonStyle_);
		ui_.lrSensorsDamageLabel->setStyleSheet(unavailableDamageStyle_);
		ui_.lrSensorsDamageLabel->setText(QString::fromUtf16(L"Zařízení není k dispozici"));
		ui_.lrSensorsDamageSlider->setVisible(false);
		ui_.lrSensorsValueFrame->setVisible(false);
		ui_.lrSensorsUnmountButton->setEnabled(false);
		ui_.lrSensorsUnmountButton->setText(QString::fromUtf16(L"Nenamontováno"));
	}
}

void SensorsWidget::notifyUpdate() {
}

SensorsWidget::~SensorsWidget() {
}