#include "droneWeaponWidget.h"
#include "doubleChargeSlider.h"
#include "AllWeaponsWidget.h"
#include <cstdlib>
#include "softProj\Main\INotifyUpdate.h"
#include "softProj\settings.h"

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

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

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

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

DroneWeaponWidget::DroneWeaponWidget(QWidget * parent) : BasicWeaponWidget (parent) {
	ui_.setupUi(this);
	
	bool connected = true;

	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_.fireSlider->setEnabled(false);

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

    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;
	actualLaunch_ = 0;

	ui_.chargingLabel->setProperty("charging", true);
	ui_.chargingLabel->setStyleSheet(ui_.chargingLabel->styleSheet());

	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 DroneWeaponWidget::switchOnButtonClickedSlot(bool checked) {
	if (isActive_) {
		deactivateDevice();
	}
	else {
		activateDevice();
	}
	
	emit settingsChangedSignal(getState());
}

void DroneWeaponWidget::activateDevice() {
	isActive_ = true;
	ui_.switchOnButton->setText("Nenabíjet");
	ui_.fireSlider->setValue(actualLaunch_);
	ui_.switchOnButton->setIcon(notokIcon);

	refreshDesignByActivity();
}

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

	refreshDesignByActivity();
}

void DroneWeaponWidget::refreshDesignByActivity() {
	if (isActive_) {
		ui_.chargingLabel->setText(QString::fromUtf16(L"Zbraň se nabíjí"));
		ui_.chargingLabel->setProperty("charging", true);
		ui_.chargingLabel->setStyleSheet(ui_.chargingLabel->styleSheet());
	}
	else {
		ui_.chargingLabel->setText(QString::fromUtf16(L"Zbraň se nenabíjí"));
		ui_.chargingLabel->setProperty("charging", false);
		ui_.chargingLabel->setStyleSheet(ui_.chargingLabel->styleSheet());
	}
}

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

void DroneWeaponWidget::setState(const drone_missile_launcher_status & state, unsigned long long actualLaunch) {
	this->state_ = state;
	this->actualLaunch_ = actualLaunch;

	setIndex(state.index);

	std::wstringstream ss;
	ss << L"Torpédomet " << state.index;

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

	setDeviceName(ss.str());

	if (actualLaunch <= state.actualAccumulator) {
		setMaximumCharge(actualLaunch);
		setCurrentCharge(state.energy);			
		setChargeToFire(state.targetEnergy);
		ui_.switchOnButton->setVisible(true);
	}
	else {
		setMaximumCharge(state.actualAccumulator);
		setCurrentCharge(state.energy);			
		setChargeToFire(0);
		ui_.switchOnButton->setVisible(false);
	}

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

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

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

void DroneWeaponWidget::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_.chargingLabel->setGeometry(5, 72, 190, 56);
	ui_.fireSlider->setAsBig();
	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);

	isSmall_ = false;
}

void DroneWeaponWidget::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_.chargingLabel->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);

	isSmall_ = true;
}

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

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

		ui_.nameButton->setText(QString::fromUtf16(L"Torpédomet č. ") + QString::number(state_.index));

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

		ui_.fireSlider->setVisible(false);
		ui_.chargingLabel->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 DroneWeaponWidget::notifyUpdate(double difference) {
	ui_.fireSlider->setCurrent(NotifyWidget::getUpdatedValue(state_.energy, state_.gradient, difference));
}