#include "crewWidget.h"
#include <cassert>
#include "softProj\settings.h"

CrewWidget::CrewWidget(QWidget * parent) : NotifyWidget(parent) {
	ui_.setupUi(this);
		
	addCrewDialog_ = NULL;

	ui_.captain->setDescription(L"Bez kapitána je posádka neorganizovaná, jejich výkon klesá na 50%");

	ui_.doctors->setName(L"Doktoři");
	ui_.doctors->setIcon(":/pictures/images/others/doktori.png");
	ui_.doctors->setDescription(L"Doktoři zaprvé poskytují první pomoc při útoku, čímž výrazně snižují ztráty, a zadruhé léči raněné");

	ui_.engineers->setName(L"Inženýři");
	ui_.engineers->setIcon(":/pictures/images/others/inzenyri.png");
	ui_.engineers->setDescription(L"Inženýři zefektivňují práci androidů; nemohou je však plně zastoupit, pouze je posilují a zlepšují");

	ui_.officers->setName(L"Důstojníci");
	ui_.officers->setIcon(":/pictures/images/others/dustojnici.png");
	ui_.officers->setDescription(L"Důstojníci mohou nahradit libovolného člena posádky (i kapitána), avšak efektivita jejich práce na jakékoliv pozici je nižší");

	ui_.passengers->setName(L"Pasažéři");
	ui_.passengers->setIcon(":/pictures/images/others/pasazeri.png");
	ui_.passengers->setDescription(L"Pasažéři sice základně či lodi nijak prospěšní nejsou, nicméně mohou za meziplanetární či mezihvězdnou přepravu zaplatit ulrei");

	ui_.pilots->setName(L"Piloti");
	ui_.pilots->setIcon(":/pictures/images/others/piloti.png");
	ui_.pilots->setDescription(L"Piloti pomáhají počítači řídit loď, čímž zlepšují její manévrovatelnost, což zvyšuje její bojovou schopnost. Na základnách nemají efekt");

	ui_.scientists->setName(L"Vědci");
	ui_.scientists->setIcon(":/pictures/images/others/vedci.png");
	ui_.scientists->setDescription(L"Vědci pracují na výzkumu nových technologií. Díky počítačové síti spolupracují na výzkumu ze všech základen a lodí");

	ui_.crewMemberWeightValue->setText(QString::fromUtf16(L"Hmotnost jedné osoby"));

	bool connected = true;		
	connected = connected && connect(ui_.captain, SIGNAL(addCrewButtonClickedSignal()), this, SLOT(addCaptainSlot()));
	connected = connected && connect(ui_.captain, SIGNAL(removeCrewButtonClickedSignal()), this, SLOT(removeCaptainSlot()));
	connected = connected && connect(ui_.officers, SIGNAL(addCrewButtonClickedSignal()), this, SLOT(addOfficersSlot()));
	connected = connected && connect(ui_.officers, SIGNAL(removeCrewButtonClickedSignal()), this, SLOT(removeOfficersSlot()));
	connected = connected && connect(ui_.pilots, SIGNAL(addCrewButtonClickedSignal()), this, SLOT(addPilotsSlot()));
	connected = connected && connect(ui_.pilots, SIGNAL(removeCrewButtonClickedSignal()), this, SLOT(removePilotsSlot()));
	connected = connected && connect(ui_.scientists, SIGNAL(addCrewButtonClickedSignal()), this, SLOT(addScientistsSlot()));
	connected = connected && connect(ui_.scientists, SIGNAL(removeCrewButtonClickedSignal()), this, SLOT(removeScientistsSlot()));
	connected = connected && connect(ui_.doctors, SIGNAL(addCrewButtonClickedSignal()), this, SLOT(addDoctorsSlot()));
	connected = connected && connect(ui_.doctors, SIGNAL(removeCrewButtonClickedSignal()), this, SLOT(removeDoctorsSlot()));
	connected = connected && connect(ui_.engineers, SIGNAL(addCrewButtonClickedSignal()), this, SLOT(addEngineersSlot()));
	connected = connected && connect(ui_.engineers, SIGNAL(removeCrewButtonClickedSignal()), this, SLOT(removeEngineersSlot()));
	connected = connected && connect(ui_.passengers, SIGNAL(addCrewButtonClickedSignal()), this, SLOT(addPassengersSlot()));
	connected = connected && connect(ui_.passengers, SIGNAL(removeCrewButtonClickedSignal()), this, SLOT(removePassengersSlot()));
	assert(connected && "Signals not connected");

	memberWeightUnit_ = " kg";

	ui_.crewSection->setName(L"Posádka");
}

void CrewWidget::setState(crew_status & state) {
	ui_.captain->setStatus(state.ABLE_CREW_CAPITAN, state.WOUNDED_CREW_CAPITAN);

	ui_.doctors->setValues(state.ABLE_CREW_DOCTOR, state.WOUNDED_CREW_DOCTOR, state.TOTAL_CREW_DOCTOR);
	ui_.engineers->setValues(state.ABLE_CREW_ENGINEER, state.WOUNDED_CREW_ENGINEER, state.TOTAL_CREW_ENGINEER);
	ui_.officers->setValues(state.ABLE_CREW_OFFICER, state.WOUNDED_CREW_OFFICER, state.TOTAL_CREW_OFFICER);
	ui_.passengers->setValues(state.ABLE_CREW_PASSENGER, state.WOUNDED_CREW_PASSENGER, state.TOTAL_CREW_PASSENGER);
	ui_.pilots->setValues(state.ABLE_CREW_PILOT, state.WOUNDED_CREW_PILOT, state.TOTAL_CREW_PILOT);
	ui_.scientists->setValues(state.ABLE_CREW_SCIENTIST, state.WOUNDED_CREW_SCIENTIST, state.TOTAL_CREW_SCIENTIST);

	ui_.crewMemberWeightValue->setText(Settings::instance().formatNumber(state.ACT_PEOPLE_WEIGHT) + memberWeightUnit_);

	this->state_ = state;
}

void CrewWidget::notifyUpdate() {
}

void CrewWidget::addCaptainSlot() {
	showAddCrewDialog(CREW_TYPE_CAPTAIN, true);
}

void CrewWidget::addOfficersSlot() {
	showAddCrewDialog(CREW_TYPE_OFFICERS, true);
}

void CrewWidget::addPilotsSlot() {
	showAddCrewDialog(CREW_TYPE_PILOTS, true);
}

void CrewWidget::addEngineersSlot() {
	showAddCrewDialog(CREW_TYPE_ENGINEERS, true);
}

void CrewWidget::addScientistsSlot(){
	showAddCrewDialog(CREW_TYPE_SCIENTISTS, true);
}

void CrewWidget::addDoctorsSlot() {
	showAddCrewDialog(CREW_TYPE_DOCTORS, true);
}

void CrewWidget::addPassengersSlot() {
	showAddCrewDialog(CREW_TYPE_PASSENGERS, true);
}

void CrewWidget::removeCaptainSlot() {
	showAddCrewDialog(CREW_TYPE_CAPTAIN, false);
}

void CrewWidget::removeOfficersSlot() {
	showAddCrewDialog(CREW_TYPE_OFFICERS, false);
}

void CrewWidget::removePilotsSlot() {
	showAddCrewDialog(CREW_TYPE_PILOTS, false);
}

void CrewWidget::removeEngineersSlot() {
	showAddCrewDialog(CREW_TYPE_ENGINEERS, false);
}

void CrewWidget::removeScientistsSlot() {
	showAddCrewDialog(CREW_TYPE_SCIENTISTS, false);
}

void CrewWidget::removeDoctorsSlot() {
	showAddCrewDialog(CREW_TYPE_DOCTORS, false);
}

void CrewWidget::removePassengersSlot() {
	showAddCrewDialog(CREW_TYPE_PASSENGERS, false);
}
	
void CrewWidget::createAddCrewDialog() {
	addCrewDialog_ = new AddCrewDialog(this);
		
	bool connected = true;		
	connected = connected && connect(addCrewDialog_, SIGNAL(submitSignal()), this, SLOT(submitAddCrewDialogSlot()));
	connected = connected && connect(addCrewDialog_, SIGNAL(cancelSignal()), this, SLOT(cancelAddCrewDialogSlot()));
	assert(connected && "Signals not connected");

	addCrewDialog_->setWindowFlags(Qt::SplashScreen);
}

void CrewWidget::submitAddCrewDialogSlot() {
	if (addCrewDialog_->isAdding()) {
		emit addCrewSignal(addCrewDialog_->getAble(), addCrewDialog_->getWounded(), addCrewDialog_->getCrewType());
	}
	else {
		emit removeCrewSignal(addCrewDialog_->getAble(), addCrewDialog_->getWounded(), addCrewDialog_->getCrewType());
	}

	addCrewDialog_->close();
}

void CrewWidget::cancelAddCrewDialogSlot() {
	addCrewDialog_->close();
	emit hideDialogSignal();
}

void CrewWidget::showAddCrewDialog(crew_type crewType, bool isAdding) {
	if (addCrewDialog_ == NULL) {
		createAddCrewDialog();
	}

	emit showDialogSignal();

	addCrewDialog_->setDialog(state_, crewType, isAdding);

	addCrewDialog_->show();
	addCrewDialog_->raise();
	addCrewDialog_->activateWindow();
}