#include "centralInfoWidget.h"
#include "softProj\settings.h"
#include "..\helpers.h"
#include <cstdlib>

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

	ui_.centralInfoSection->setName(L"Centrální informace");

	/* Zirconium crystals */

	ui_.crystalResourceWidget->setName(L"Zirkonové krystaly");
	ui_.crystalResourceWidget->setIcon(":/pictures/images/others/krystaly.png");
	ui_.crystalResourceWidget->setValueUnit(L"kg");
	ui_.crystalResourceWidget->setCapacityUnit(L"m^3");

	/* Deuterium */

	ui_.deuteriumResourceWidget->setName(L"Deuterium");
	ui_.deuteriumResourceWidget->setIcon(":/pictures/images/others/deuterium.png");
	ui_.deuteriumResourceWidget->setValueUnit(L"kg");
	ui_.deuteriumResourceWidget->setCapacityUnit(L"m^3");

	/* Energy */

	ui_.energyResourceWidget->setName(L"Energie");
	ui_.energyResourceWidget->setIcon(":/pictures/images/others/energie.png");
	ui_.energyResourceWidget->setValueUnit(L"TJ");
	ui_.energyResourceWidget->setValueLabel(L"Množství");
	ui_.energyResourceWidget->setSize(L"-");
	ui_.energyResourceWidget->setCapacityUnit(L"m^3");

	/* Heavy water */

	ui_.heavyWaterResourceWidget->setName(L"Těžká voda");
	ui_.heavyWaterResourceWidget->setIcon(":/pictures/images/others/tezkaVoda.png");
	ui_.heavyWaterResourceWidget->setValueUnit(L"kg");
	ui_.heavyWaterResourceWidget->setCapacityUnit(L"m^3");

	/* Ilmenite */

	ui_.ilmenitResourceWidget->setName(L"Ilmenit");
	ui_.ilmenitResourceWidget->setIcon(":/pictures/images/others/ilmenit.png");
	ui_.ilmenitResourceWidget->setValueUnit(L"kg");
	ui_.ilmenitResourceWidget->setCapacityUnit(L"m^3");
		
	/* Quark-gluon plasma */

	ui_.plazmaResourceWidget->setName(L"Quark-gluonové plazma");
	ui_.plazmaResourceWidget->setIcon(":/pictures/images/others/plazma.png");
	ui_.plazmaResourceWidget->setValueUnit(L"mg");
	ui_.plazmaResourceWidget->setCapacityUnit(L"m^3");

	/* Titan */

	ui_.titanResourceWidget->setName(L"Titan");	
	ui_.titanResourceWidget->setIcon(":/pictures/images/others/titan.png");
	ui_.titanResourceWidget->setValueUnit(L"mg");
	ui_.titanResourceWidget->setCapacityUnit(L"m^3");

	/* Ulrea */

	ui_.ulreaResourceWidget->setName(L"Ulrea");
	ui_.ulreaResourceWidget->setIcon(":/pictures/images/others/ulrea.png");
	ui_.ulreaResourceWidget->setValueUnit(L"kusů");
	ui_.ulreaResourceWidget->setSize(L"-");
	ui_.ulreaResourceWidget->setValueLabel(L"Počet");
	ui_.ulreaResourceWidget->setFreeCapacity(L"Neomezeno");		
	ui_.ulreaResourceWidget->setCapacityUnit(L"m^3");
}

void CentralInfoWidget::setState(CentralInfoDetails & details) {	
	this->state_ = details;	
	rememberUpdateTime();

	ui_.integritySlider->setLimits(0, details.structuralIntegrityTotal);
	ui_.integritySlider->setValue(details.structuralIntegrityActual);	
		
	ui_.agilityValue->setText(Settings::instance().formatNumber(details.manoeuvrability));	
		
	ui_.widthValue->setText(Settings::instance().formatNumber(details.width) + " m");	
	ui_.lengthValue->setText(Settings::instance().formatNumber(details.length) + " m");	
	ui_.heightValue->setText(Settings::instance().formatNumber(details.height) + " m");	

	ui_.cleanWeightValue->setText(Settings::instance().formatNumber(details.weight) + " kg");	
	ui_.dirtyWeightValue->setText(Settings::instance().formatNumber(details.weightWithCargo) + " kg");
	ui_.fortressVolumeValue->setText(Settings::instance().formatNumber(details.loadingCapacityLimit) + " m^3");

	ui_.fortressLoadingValue->setText(Settings::instance().formatNumber(details.loadingWeightLimit) + " kg");
	ui_.cargoWeightValue->setText(Settings::instance().formatNumber(details.actualTotalCargoWeight) + " kg");
	ui_.cargoVolumeValue->setText(Settings::instance().formatNumber(details.actualSizeOfCargoInWarehouse) + " m^3");

	ui_.freeFortressLoadingValue->setText(Settings::instance().formatNumber(details.freeWeight) + " kg");
	ui_.freeFortressVolumeValue->setText(Settings::instance().formatNumber(details.freeCapacity) + " m^3");

	ui_.crystalResourceWidget->setSize(details.zirconiumCrystalsSize);
	ui_.crystalResourceWidget->setValue(details.zirconiumCrystalsActual);		
	ui_.crystalResourceWidget->setGradient((signed long long) ceil(details.zirconiumCrystalGradient));
	ui_.crystalResourceWidget->setFreeCapacity(details.zirconiumCrystalsFreeCapacity);

	ui_.deuteriumResourceWidget->setSize(details.deuteriumSize);
	ui_.deuteriumResourceWidget->setValue(details.deuteriumActual);		
	ui_.deuteriumResourceWidget->setGradient((signed long long) ceil(details.deuteriumGradient));
	ui_.deuteriumResourceWidget->setFreeCapacity(details.deuteriumFreeCapacity);
	
	ui_.energyResourceWidget->setValue(details.energyActual);		
	ui_.energyResourceWidget->setGradient((signed long long) ceil(details.energyGradient));
	ui_.energyResourceWidget->setFreeCapacity(details.energyFreeCapacity);

	ui_.heavyWaterResourceWidget->setSize(details.heavyWaterSize);
	ui_.heavyWaterResourceWidget->setValue(details.heavyWaterActual);		
	ui_.heavyWaterResourceWidget->setGradient((signed long long) ceil(details.heavyWaterGradient));
	ui_.heavyWaterResourceWidget->setFreeCapacity(details.heavyWaterFreeCapacity);

	ui_.ilmenitResourceWidget->setSize(details.ilmeniteSize);
	ui_.ilmenitResourceWidget->setValue(details.ilmeniteActual);		
	ui_.ilmenitResourceWidget->setGradient((signed long long) ceil(details.ilmeniteGradient));
	ui_.ilmenitResourceWidget->setFreeCapacity(details.ilmeniteFreeCapacity);

	ui_.plazmaResourceWidget->setSize(L"-");
	ui_.plazmaResourceWidget->setValue(details.quarkGluonPlasmaActual);		
	ui_.plazmaResourceWidget->setGradient((signed long long) ceil(details.quarkGluonPlasmaGradient));
	ui_.plazmaResourceWidget->setFreeCapacity(details.quarkGluonPlasmaFreeCapacity);

	ui_.titanResourceWidget->setSize(details.titaniumSize);
	ui_.titanResourceWidget->setValue(details.titaniumActual);		
	ui_.titanResourceWidget->setGradient((signed long long) ceil(details.titaniumGradient));
	ui_.titanResourceWidget->setFreeCapacity(details.titaniumFreeCapacity);
	
	ui_.ulreaResourceWidget->setValue(details.ulreaActual);		
	ui_.ulreaResourceWidget->setGradient((signed long long) ceil(details.ulreaGradient));
}

void CentralInfoWidget::notifyUpdate() {
	double difference = getDifferenceFromUpdateTime();

	double zirconiumCrystal = getUpdatedValue(state_.zirconiumCrystalsActual, state_.zirconiumCrystalGradient, difference);
	double deuterium = getUpdatedValue(state_.deuteriumActual, state_.deuteriumGradient, difference);
	double energy = getUpdatedValue(state_.energyActual, state_.energyGradient, difference);
	double heavyWater = getUpdatedValue(state_.heavyWaterActual, state_.heavyWaterGradient, difference);
	double ilmenite = getUpdatedValue(state_.ilmeniteActual, state_.ilmeniteGradient, difference);
	double plasma = getUpdatedValue(state_.quarkGluonPlasmaActual, state_.quarkGluonPlasmaGradient, difference);
	double titanium = getUpdatedValue(state_.titaniumActual, state_.titaniumGradient, difference);
	double ulrea = getUpdatedValue(state_.ulreaActual, state_.ulreaGradient, difference);

	double zirconiumCrystalsFreeCapacity = getUpdatedValue((double) state_.zirconiumCrystalsFreeCapacity, -state_.zirconiumCrystalGradient, difference);
	double deuteriumFreeCapacity = getUpdatedValue((double) state_.deuteriumFreeCapacity, -state_.deuteriumGradient, difference);
	double energyFreeCapacity = getUpdatedValue((double) state_.energyFreeCapacity, -state_.energyGradient, difference);
	double heavyWaterFreeCapacity = getUpdatedValue((double) state_.heavyWaterFreeCapacity, -state_.heavyWaterGradient, difference);
	double ilmeniteFreeCapacity = getUpdatedValue((double) state_.ilmeniteFreeCapacity, -state_.ilmeniteGradient, difference);
	double quarkGluonPlasmaFreeCapacity = getUpdatedValue((double) state_.quarkGluonPlasmaFreeCapacity, -state_.quarkGluonPlasmaGradient, difference);
	double titaniumFreeCapacity = getUpdatedValue((double) state_.titaniumFreeCapacity, -state_.titaniumGradient, difference);

	zirconiumCrystalsFreeCapacity = max(0, zirconiumCrystalsFreeCapacity);
	deuteriumFreeCapacity = max(0, deuteriumFreeCapacity);
	energyFreeCapacity = max(0, energyFreeCapacity);
	heavyWaterFreeCapacity = max(0, heavyWaterFreeCapacity);
	ilmeniteFreeCapacity = max(0, ilmeniteFreeCapacity);
	quarkGluonPlasmaFreeCapacity = max(0, quarkGluonPlasmaFreeCapacity);
	titaniumFreeCapacity = max(0, titaniumFreeCapacity);

	heavyWater = max(0, heavyWater);
	deuterium = max(0, deuterium);
	zirconiumCrystal = max(0, zirconiumCrystal);
	ilmenite = max(0, ilmenite);
	titanium = max(0, titanium);

	double totalWeight = 
		heavyWater +
		deuterium +
		zirconiumCrystal +
		ilmenite +
		titanium;

	double totalCapacity    = 
		heavyWater / (double) state_.heavyWaterDensity +
		deuterium / (double) state_.deuteriumDensity +
		zirconiumCrystal / (double) state_.zirconiumCrystalDensity +
		ilmenite / (double) state_.ilmeniteDensity+
		titanium / (double) state_.titaniumDensity;

	if ((state_.freeResWeight   < totalWeight  ) || (state_.commonRawMaterialCapacityLimit < totalCapacity))
	{
		double weightBalanceRatio = 0;

		if (totalWeight != 0) {
			weightBalanceRatio = (double)(state_.freeResWeight) / (double)(totalWeight);
		}

		double capacityBalanceRatio = 0;

		if (totalCapacity != 0) {
			capacityBalanceRatio  = (double)(state_.commonRawMaterialCapacityLimit) / (double)(totalCapacity);
		}

		double balanceRatio = min(weightBalanceRatio, capacityBalanceRatio);

		heavyWater           *= balanceRatio;
		deuterium            *= balanceRatio;
		zirconiumCrystal     *= balanceRatio;
		ilmenite             *= balanceRatio;
		titanium             *= balanceRatio;
	}

	ui_.crystalResourceWidget->setValue(zirconiumCrystal);
	ui_.crystalResourceWidget->setFreeCapacity(zirconiumCrystalsFreeCapacity);

	ui_.deuteriumResourceWidget->setValue(deuterium);
	ui_.deuteriumResourceWidget->setFreeCapacity(deuteriumFreeCapacity);

	ui_.energyResourceWidget->setValue(energy);
	ui_.energyResourceWidget->setFreeCapacity(energyFreeCapacity);

	ui_.heavyWaterResourceWidget->setValue(heavyWater);
	ui_.heavyWaterResourceWidget->setFreeCapacity(heavyWaterFreeCapacity);

	ui_.ilmenitResourceWidget->setValue(ilmenite);
	ui_.ilmenitResourceWidget->setFreeCapacity(ilmeniteFreeCapacity);

	ui_.plazmaResourceWidget->setValue(plasma);
	ui_.plazmaResourceWidget->setFreeCapacity(quarkGluonPlasmaFreeCapacity);

	ui_.titanResourceWidget->setValue(titanium);
	ui_.titanResourceWidget->setFreeCapacity(titaniumFreeCapacity);

	ui_.ulreaResourceWidget->setValue(ulrea);

	ui_.integritySlider->setValue(getUpdatedValue(state_.structuralIntegrityActual, state_.structuralIntegrityGradient, difference));
}