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

void MiningWidget::changeQuarkGluonPlasmaQuarryStateSlot(bool isQuarrying) {
	emit changeQuarkGluonPlasmaQuarryStateSignal(fortressId_, isQuarrying);
}
	
void MiningWidget::changeHeavyWaterQuarryStateSlot(bool isQuarrying) {
	emit changeHeavyWaterQuarryStateSignal(fortressId_, isQuarrying);
}

void MiningWidget::changeZirconiumCrystalQuarryStateSlot(bool isQuarrying) {
	emit changeZirconiumCrystalQuarryStateSignal(fortressId_, isQuarrying);
}

void MiningWidget::changeIlmeniteQuarryStateSlot(bool isQuarrying) {
	emit changeIlmeniteQuarryStateSignal(fortressId_, isQuarrying);
}

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

	bool connected = true;
		
	ui_.miningSection->setName(L"Těžební stroje");
	ui_.resourcesSection->setName(L"Suroviny, které lze těžit");		

	/* Magnetical collectors of plasma */

	ui_.magneticalCollectorsOfPlasma->setName(L"Magnetické\nkolektory plazmy");
	ui_.magneticalCollectorsOfPlasma->setDescription(L"Slouží k těžbě quark-gluonové plazmy z neutronových hvězd");
	ui_.magneticalCollectorsOfPlasma->setSpeedUnit(L"mg / hs");
	ui_.magneticalCollectorsOfPlasma->setRangeUnit(L"km");
	connected = connected && connect(ui_.magneticalCollectorsOfPlasma, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(magneticalCollectorsOfPlasmaUnmountButtonClickedSlot(bool)));

	/* Submarine minning machine */

	ui_.submarineMiningMachine->setName(L"Podmořské těžební\nzařízení");
	ui_.submarineMiningMachine->setDescription(L"Slouží k těžbě zirkonových krystalů na mořských dnech");
	ui_.submarineMiningMachine->setSpeedUnit(L"g / hs");
	ui_.submarineMiningMachine->setRangeUnit(L"km");
	connected = connected && connect(ui_.submarineMiningMachine, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(submarineMiningMachineUnmountButtonClickedSlot(bool)));

	/* Submersible filtration tank */
	ui_.sumbersibleFiltrationTank->setDescription(L"Slouží k těžbě těžké vody z moře");
	ui_.sumbersibleFiltrationTank->setSpeedUnit(L"kg / hs");
	ui_.sumbersibleFiltrationTank->setRangeUnit(L"km");
	ui_.sumbersibleFiltrationTank->setName(L"Ponorné filtrační\nnádrže");

	connected = connected && connect(ui_.sumbersibleFiltrationTank, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(sumbersibleFiltrationTankUnmountButtonClickedSlot(bool)));

	/* Underground drilling machine */

	ui_.undergroundDrillingMachine->setName(L"Důlní vrtací\nstroje");
	ui_.undergroundDrillingMachine->setDescription(L"Slouží k těžbě ilmenitu v dolech na planetách");
	ui_.undergroundDrillingMachine->setSpeedUnit(L"g / hs");
	ui_.undergroundDrillingMachine->setRangeUnit(L"km");
	connected = connected && connect(ui_.undergroundDrillingMachine, SIGNAL(unmountButtonClickedSignal(bool)), this, SLOT(undergroundDrillingMachineUnmountButtonClickedSlot(bool)));

	/* Quark-gluon plasma */
	ui_.quarkGluonPlasma->setName(L"Quark-gluonová plazma");
	ui_.quarkGluonPlasma->setIcon(":/pictures/images/others/plazma.png");
	ui_.quarkGluonPlasma->setSpeedUnit(L"kg / hs");

	bool ok = connect(ui_.quarkGluonPlasma, SIGNAL(changeQuarryStateSignal(bool)),
		this, SLOT(changeQuarkGluonPlasmaQuarryStateSlot(bool)));

	/* Heavy water */
	ui_.heavyWater->setName(L"Těžká voda");
	ui_.heavyWater->setIcon(":/pictures/images/others/tezkaVoda.png");
	ui_.heavyWater->setSpeedUnit(L"kg / hs");

	ok = ok && connect(ui_.heavyWater, SIGNAL(changeQuarryStateSignal(bool)),
		this, SLOT(changeHeavyWaterQuarryStateSlot(bool)));

	/* Zirconium crystals */
	ui_.zirconiumCrystal->setName(L"Zirconiové krystaly");
	ui_.zirconiumCrystal->setIcon(":/pictures/images/others/krystaly.png");
	ui_.zirconiumCrystal->setSpeedUnit(L"kg / hs");

	ok = ok && connect(ui_.zirconiumCrystal, SIGNAL(changeQuarryStateSignal(bool)),
		this, SLOT(changeZirconiumCrystalQuarryStateSlot(bool)));

	/* Ilmenite */
	ui_.ilmenite->setName(L"Ilmenit");
	ui_.ilmenite->setIcon(":/pictures/images/others/ilmenit.png");
	ui_.ilmenite->setSpeedUnit(L"ks / hs");

	ok = ok && connect(ui_.ilmenite, SIGNAL(changeQuarryStateSignal(bool)),
		this, SLOT(changeIlmeniteQuarryStateSlot(bool)));

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

void MiningWidget::setState(id_t fortressId, quarry_status & state) {		
	this->fortressId_ = fortressId;

	this->state_ = state;
	this->rememberUpdateTime();

	/* Magnetical collectors of plasma */

	ui_.magneticalCollectorsOfPlasma->setSpeed(state.QuarkGluonPlasmaQuarryingActualMaximalSpeed);
	ui_.magneticalCollectorsOfPlasma->setRange(state.QuarkGluonPlasmaQuarryingActualRange);
	ui_.magneticalCollectorsOfPlasma->setTotalCount(state.QuarkGluonPlasmaQuarryingDamaged
		+ state.QuarkGluonPlasmaQuarryingWorking);
	ui_.magneticalCollectorsOfPlasma->setDamageStatus(state.QuarkGluonPlasmaQuarryingWorking, state.QuarkGluonPlasmaQuarryingDamaged);
	ui_.magneticalCollectorsOfPlasma->setAvailable(state.avalMagneticalCollectorsOfPlasma);

	/* Submarine minning machine */

	ui_.submarineMiningMachine->setSpeed(state.ZirconiumCrystalQuarryingActualMaximalSpeed);
	ui_.submarineMiningMachine->setRange(state.ZirconiumCrystalQuarryingActualRange);
	ui_.submarineMiningMachine->setTotalCount(state.ZirconiumCrystalQuarryingDamaged
		+ state.ZirconiumCrystalQuarryingWorking);
	ui_.submarineMiningMachine->setDamageStatus(state.ZirconiumCrystalQuarryingWorking, state.ZirconiumCrystalQuarryingDamaged);
	ui_.submarineMiningMachine->setAvailable(state.avalSubmarineMiningMachine);

	/* Submersible filtration tank */

	ui_.sumbersibleFiltrationTank->setSpeed(state.HeavyWaterQuarryingActualMaximalSpeed);
	ui_.sumbersibleFiltrationTank->setRange(state.HeavyWaterQuarryingActualRange);
	ui_.sumbersibleFiltrationTank->setTotalCount(state.HeavyWaterQuarryingDamaged
		+ state.HeavyWaterQuarryingWorking);
	ui_.sumbersibleFiltrationTank->setDamageStatus(state.HeavyWaterQuarryingWorking, state.HeavyWaterQuarryingDamaged);
	ui_.sumbersibleFiltrationTank->setAvailable(state.avalSubmersibleFiltrationTank);

	/* Underground drilling machine */

	ui_.undergroundDrillingMachine->setSpeed(state.IlmeniteQuarryingActualMaximalSpeed);
	ui_.undergroundDrillingMachine->setRange(state.IlmeniteQuarryingActualRange);
	ui_.undergroundDrillingMachine->setTotalCount(state.IlmeniteQuarryingDamaged
		+ state.IlmeniteQuarryingWorking);
	ui_.undergroundDrillingMachine->setDamageStatus(state.IlmeniteQuarryingWorking, state.IlmeniteQuarryingDamaged);
	ui_.undergroundDrillingMachine->setAvailable(state.avalUndergroundDrillingMachine);

	/* Quark-gluon plasma */

	ui_.quarkGluonPlasma->setSpeed((signed long long)state.QuarkGluonPlasmaQuarryingSpeed);

	if (state.isActuallyQuarryingQuarkGluonPlasma) {
		ui_.quarkGluonPlasma->setAsActuallyQuarrying();
	}
	else if (state.isPossibleToQuarryQuarkGluonPlasma) {
		ui_.quarkGluonPlasma->setAsQuarryingEnabled();
	}
	else {
		ui_.quarkGluonPlasma->setAsQuarryingDisabled();
	}		

	/* Heavy water */

	ui_.heavyWater->setSpeed((signed long long)state.HeavyWaterQuarryingSpeed);

	if (state.isActuallyQuarryingHeavyWater) {
		ui_.heavyWater->setAsActuallyQuarrying();
	}
	else if (state.isPossibleToQuarryHeavyWater) {
		ui_.heavyWater->setAsQuarryingEnabled();
	}
	else {
		ui_.heavyWater->setAsQuarryingDisabled();
	}		

	/* Zirconium crystals */

	ui_.zirconiumCrystal->setSpeed((signed long long)state.ZirconiumCrystalQuarryingSpeed);

	if (state.isActuallyQuarryingZirconiumCrystals) {
		ui_.zirconiumCrystal->setAsActuallyQuarrying();
	}
	else if (state.isPossibleToQuarryZirconiumCrystals) {
		ui_.zirconiumCrystal->setAsQuarryingEnabled();
	}
	else {
		ui_.zirconiumCrystal->setAsQuarryingDisabled();
	}

	/* Ilmenite */

	ui_.ilmenite->setSpeed((signed long long)state.IlmeniteQuarryingSpeed);

	if (state.isActuallyQuarryingIlmenite) {
		ui_.ilmenite->setAsActuallyQuarrying();
	}
	else if (state.isPossibleToQuarryIlmenite) {
		ui_.ilmenite->setAsQuarryingEnabled();
	}
	else {
		ui_.ilmenite->setAsQuarryingDisabled();
	}			
} 

void MiningWidget::magneticalCollectorsOfPlasmaUnmountButtonClickedSlot(bool checked) {
	emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_MAGNETICAL_COLLECTORS_OF_PLASMA, state_.QuarkGluonPlasmaQuarryingWorking, state_.QuarkGluonPlasmaQuarryingDamaged);
}

void MiningWidget::submarineMiningMachineUnmountButtonClickedSlot(bool checked) {
	emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_SUBMARINE_MINING_MACHINE, state_.ZirconiumCrystalQuarryingWorking, state_.ZirconiumCrystalQuarryingDamaged);
}

void MiningWidget::sumbersibleFiltrationTankUnmountButtonClickedSlot(bool checked) { 
	emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_SUBMERSIBLE_FILTRATION_TANK, state_.HeavyWaterQuarryingWorking, state_.HeavyWaterQuarryingDamaged);
}

void MiningWidget::undergroundDrillingMachineUnmountButtonClickedSlot(bool checked) {
	emit unmountSignal(PROTOCOL_HANGARS_PATTERN_ID_UNDERGROUND_DRILLIG_MACHINE, state_.IlmeniteQuarryingWorking, state_.IlmeniteQuarryingDamaged);
}


MiningWidget::~MiningWidget() {
}

void MiningWidget::notifyUpdate() {
}