#include "laserWeaponWidget.h"
#include "doubleChargeSlider.h"
#include "AllWeaponsWidget.h"
#include "softProj\Main\INotifyUpdate.h"
#include "softProj\settings.h"

void LaserWeaponWidget::chargeToFireSliderChangedSlot(int value) {
	chargeToFireChanged(value);
	emit settingsChangedSignal(getState());
}

void LaserWeaponWidget::chargeToFireChanged(unsigned long long value) {
	
	setChargeToFire(value);

	if (value == 0) {
		deactivateDevice();
	}
	else if (!isActive_) {
		activateDevice();
	}	
}

unsigned long long LaserWeaponWidget::getChargeToFire() {
	return ui_.fireSlider->value();
}

void LaserWeaponWidget::setMaximumCharge(unsigned long long value) {
	ui_.fireSlider->setLimits(0, value);
}

void LaserWeaponWidget::setCurrentCharge(unsigned long long value) {
	ui_.fireSlider->setCurrent(value);
}

void LaserWeaponWidget::setChargeToFire(unsigned long long value) {
	ui_.fireSlider->setValue(value);
}

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

	bool connected = true;

	connected = connected && (QObject::connect(
		ui_.fireSlider, SIGNAL(valueChanged(int)),
		this, SLOT(chargeToFireSliderChangedSlot(int))
	));

	connected = connected && (QObject::connect(
		ui_.switchOnButton, SIGNAL(clicked(bool)),
		this, SLOT(switchOnButtonClickedSlot(bool))
	));	

	connected = connected && (QObject::connect(
		ui_.unmountButton, SIGNAL(clicked(bool)),
		this, SLOT(unmountButtonClickedSlot())
	));	

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

	ui_.damageStatusSlider->setUnit(L"%");
	ui_.damageStatusSlider->setLimits(0, 100);

	ui_.fireSlider->setEnabled(true);

    okIcon.addFile(QString::fromUtf8(":/pictures/images/trade/ok-big.png"), QSize(), QIcon::Normal, QIcon::Off);
	notokIcon.addFile(QString::fromUtf8(":/pictures/images/trade/not-ok-big.png"), QSize(), QIcon::Normal, QIcon::Off);

	isSmall_ = false;

	availableButtonStyle_ = Settings::instance().getStyle(STYLE_AVAILABLE_DEVICE_BUTTON);
	unavailableButtonStyle_ = Settings::instance().getStyle(STYLE_UNAVAILABLE_DEVICE_BUTTON);

	availableSmallButtonStyle_ = Settings::instance().getStyle(STYLE_AVAILABLE_DEVICE_SMALL_BUTTON);
	unavailableSmallButtonStyle_ = Settings::instance().getStyle(STYLE_UNAVAILABLE_DEVICE_SMALL_BUTTON);
}

void LaserWeaponWidget::switchOnButtonClickedSlot(bool checked) {
	if (isActive_) {
		deactivateDevice();
	}
	else {
		activateDevice();
	}

	emit settingsChangedSignal(getState());
}

void LaserWeaponWidget::activateDevice() {
	isActive_ = true;
	ui_.switchOnButton->setText("Nenabíjet");
	ui_.switchOnButton->setIcon(notokIcon);
	ui_.switchOnButton->setEnabled(true);
	refreshDesignByActivity();
}

void LaserWeaponWidget::deactivateDevice() {
	isActive_ = false;
	ui_.switchOnButton->setEnabled(false);
	ui_.switchOnButton->setText("Nabíjet");
	ui_.switchOnButton->setIcon(okIcon);
	ui_.fireSlider->setValue(0);

	refreshDesignByActivity();
}

void LaserWeaponWidget::setState(const laser_gun_status & state) {
	this->state_ = state;

	setMaximumCharge(state.actualAccumulator);
	setCurrentCharge(state.energy);			
	setChargeToFire(state.targetEnergy);	
	setIndex(state.index);

	ui_.precisionValue->setText(QString::number(state.actualPrecision));
	ui_.rangeValue->setText(QString::number(state.actualRange));
	ui_.forcibilityValue->setText(QString::number(state.actualForcibility));

	std::wstringstream ss;
	ss << L"Laserové dělo " << state.index;

	ui_.damageStatusSlider->setValue(state.status * 100);

	setDeviceName(ss.str());

	if (getChargeToFire() > 0) {
		activateDevice();
	}
	else {
		deactivateDevice();
	}
}

void LaserWeaponWidget::refreshDesignByActivity() {
}

void LaserWeaponWidget::setDeviceName(const std::wstring & deviceName) {
	ui_.nameButton->setText(QString::fromUtf16(deviceName.c_str()));
}

std::pair<id_t, unsigned long long> LaserWeaponWidget::getState() {
	return std::make_pair(state_.index, getChargeToFire());
}

void LaserWeaponWidget::unmountButtonClickedSlot() {
	emit unmountSignal(state_.index);
}

void LaserWeaponWidget::setAsBig() {
	resize(200, 190);

	ui_.weaponImageFrame->setGeometry(0, 0, 200, 190);
	ui_.deviceFrame->setGeometry(1, 1, 198, 188);
	ui_.nameButton->setGeometry(5, 3, 188, 25);
	ui_.nameButton->setIconSize(QSize(20, 20));
	ui_.nameButton->setStyleSheet("QPushButton { background-color: rgb(48, 66, 35, 230); color: #ffffff; border: 0; text-align: center; font-weight: bold; font-size: 11px; }");
	ui_.fireSlider->setGeometry(5, 31, 188, 38);
	ui_.fireSlider->setAsBig();
	ui_.frame->setGeometry(5, 72, 190, 56);
	ui_.damageStatusSlider->setGeometry(5, 133, 190, 30);
	ui_.damageStatusSlider->showMaximum(true);
	ui_.damageStatusSlider->setFontSize(8, QFont::Bold);
	ui_.switchOnButton->setVisible(true);
	ui_.switchOnButton->setGeometry(5, 167, 98, 18);
	ui_.unmountButton->setVisible(true);
	ui_.unmountButton->setGeometry(105, 167, 98, 18);

	ui_.rangeLabel->setGeometry(0, 0, 60, 18);
	ui_.rangeLabel->setStyleSheet("font-weight: bold; background: transparent; font-size: 11px;");
	ui_.precisionLabel->setGeometry(0, 18, 60, 18);
	ui_.precisionLabel->setStyleSheet("font-weight: bold; background: transparent; font-size: 11px;");
	ui_.forcibilityLabel->setGeometry(0, 36, 60, 18);
	ui_.forcibilityLabel->setStyleSheet("font-weight: bold; background: transparent; font-size: 11px;");

	ui_.rangeValue->setGeometry(60, 0, 82, 18);
	ui_.rangeValue->setStyleSheet("font-weight: normal; background: transparent; font-size: 11px;");
	ui_.precisionValue->setGeometry(60, 18, 80, 18);
	ui_.precisionValue->setStyleSheet("font-weight: normal; background: transparent; font-size: 11px;");
	ui_.forcibilityValue->setGeometry(60, 36, 82, 18);
	ui_.forcibilityValue->setStyleSheet("font-weight: normal; background: transparent; font-size: 11px;");

	isSmall_ = false;
}

void LaserWeaponWidget::setAsSmall() {
	resize(115, 115);

	ui_.weaponImageFrame->setGeometry(0, 0, 115, 115);
	ui_.deviceFrame->setGeometry(1, 1, 113, 113);
	ui_.nameButton->setGeometry(2, 2, 109, 15);
	ui_.nameButton->setIconSize(QSize(12, 12));
	ui_.nameButton->setStyleSheet("QPushButton { background-color: rgb(48, 66, 35, 230); color: #ffffff; border: 0; text-align: center; font-weight: normal; font-size: 10px; }");
	ui_.fireSlider->setGeometry(2, 20, 109, 29);
	ui_.fireSlider->setAsSmall();
	ui_.frame->setGeometry(3, 50, 109, 45);
	ui_.damageStatusSlider->setGeometry(2, 97, 109, 14);
	ui_.damageStatusSlider->showMaximum(false);
	ui_.damageStatusSlider->setFontSize(7, QFont::Normal);
	ui_.switchOnButton->setVisible(false);
	ui_.unmountButton->setVisible(false);

	ui_.rangeLabel->setGeometry(0, 0, 60, 15);
	ui_.rangeLabel->setStyleSheet("background: transparent; font-size: 10px;");
	ui_.precisionLabel->setGeometry(0, 15, 60, 15);
	ui_.precisionLabel->setStyleSheet("background: transparent; font-size: 10px;");
	ui_.forcibilityLabel->setGeometry(0, 30, 60, 15);
	ui_.forcibilityLabel->setStyleSheet("background: transparent; font-size: 10px;");

	ui_.rangeValue->setGeometry(60, 0, 48, 15);
	ui_.rangeValue->setStyleSheet("background: transparent; font-size: 10px;");
	ui_.precisionValue->setGeometry(60, 15, 48, 15);
	ui_.precisionValue->setStyleSheet("background: transparent; font-size: 10px;");
	ui_.forcibilityValue->setGeometry(60, 30, 48, 15);
	ui_.forcibilityValue->setStyleSheet("background: transparent; font-size: 10px;");

	isSmall_ = true;
}

void LaserWeaponWidget::setAsMounted(bool mounted) {
	if (mounted) {
		refreshDesignByActivity();

		ui_.fireSlider->setVisible(true);
		ui_.frame->setVisible(true);
		ui_.damageStatusSlider->setVisible(true);
		ui_.switchOnButton->setVisible(true);
		ui_.unmountButton->setVisible(true);

		ui_.nameButton->setText(QString::fromUtf16(L"Laserové dělo č. ") + QString::number(state_.index));

		if (isSmall_) {
			ui_.nameButton->setStyleSheet(availableSmallButtonStyle_);
		}
		else {
			ui_.nameButton->setStyleSheet(availableButtonStyle_);
		}
	}
	else {
		refreshDesignByActivity();

		ui_.fireSlider->setVisible(false);
		ui_.frame->setVisible(false);
		ui_.damageStatusSlider->setVisible(false);
		ui_.switchOnButton->setVisible(false);
		ui_.unmountButton->setVisible(false);		

		ui_.nameButton->setText(QString::fromUtf16(L"Nepřipojeno"));

		if (isSmall_) {
			ui_.nameButton->setStyleSheet(unavailableSmallButtonStyle_);
		}
		else {
			ui_.nameButton->setStyleSheet(unavailableButtonStyle_);
		}
	}
}

void LaserWeaponWidget::notifyUpdate(double difference) {
	ui_.fireSlider->setCurrent(NotifyWidget::getUpdatedValue(state_.energy, state_.gradient, difference));
}