#include "accommodationsWidget.h"
#include "softProj\helpers.h"
#include <cassert>

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

	weightUnit_ = " kg";
	sizeUnit_ = " m^3";

	addRoomDialog_ = NULL;

	ui_.luxuryAccomodations->setName(L"Luxusní apartmá");
	ui_.luxuryAccomodations->setIcon(":/pictures/images/others/luxusniApartma.png");
	ui_.luxuryAccomodations->setQualityClass(L"1.\n\ntřída");

	ui_.comfortableRooms->setName(L"Komfortní pokoj");
	ui_.comfortableRooms->setIcon(":/pictures/images/others/komfortniPokoj.png");
	ui_.comfortableRooms->setQualityClass(L"2.\n\ntřída");

	ui_.commonCabins->setName(L"Standardní kajuta");
	ui_.commonCabins->setIcon(":/pictures/images/others/standardniKajuta.png");
	ui_.commonCabins->setQualityClass(L"3.\n\ntřída");

	ui_.warehouses->setName(L"Sklad s podporou života");
	ui_.warehouses->setIcon(":/pictures/images/others/skladSPodporouZivota.png");
	ui_.warehouses->setQualityClass(L"4.\n\ntřída");

	ui_.accomodationsSection->setName(L"Ubikace");

	bool connected = true;
	connected = connected && connect(ui_.luxuryAccomodations, SIGNAL(addRoomSignal()), this, SLOT(addLuxuryAccommodationsSlot()));
	connected = connected && connect(ui_.comfortableRooms, SIGNAL(addRoomSignal()), this, SLOT(addComfortableRoomsSlot()));
	connected = connected && connect(ui_.commonCabins, SIGNAL(addRoomSignal()), this, SLOT(addCommonCabinsSlot()));
	connected = connected && connect(ui_.warehouses, SIGNAL(addRoomSignal()), this, SLOT(addWarehousesSlot()));
	
	assert(connected && "Signals not connected");
}

void AccommodationsWidget::addLuxuryAccommodationsSlot() {
	showAddRoomDialog(ACCOMMODATION_LUXURY_APPARTEMENT);
}

void AccommodationsWidget::addComfortableRoomsSlot() {
	showAddRoomDialog(ACCOMMODATION_COMFORTABLE_ROOM);
}

void AccommodationsWidget::addCommonCabinsSlot() {
	showAddRoomDialog(ACCOMMODATION_COMMON_CABIN);
}

void AccommodationsWidget::addWarehousesSlot() {
	showAddRoomDialog(ACCOMMODATION_WAREHOUSE_WITH_LIFE_SUPPORT);
}

void AccommodationsWidget::setState(accommodation_status & state) {

	// TODO Free rooms?

	ui_.comfortableRooms->setRoomCount(state.comfortableRoomsCount);
	ui_.comfortableRooms->setAvailableCount(state.freeComfortableRooms);
	ui_.comfortableRooms->setEngagedCount(state.comfortableRoomsCount - state.freeComfortableRooms);
	ui_.comfortableRooms->setPrice(state.comfortableRoomsPrice);
	ui_.commonCabins->setRoomCount(state.commonCabinsCount);
	ui_.commonCabins->setAvailableCount(state.freeCommonCabins);
	ui_.commonCabins->setEngagedCount(state.commonCabinsCount - state.freeCommonCabins);
	ui_.commonCabins->setPrice(state.commonCabinsPrice);
	ui_.luxuryAccomodations->setRoomCount(state.luxuryAccommodationsCount);
	ui_.luxuryAccomodations->setAvailableCount(state.freeLuxuryAccommodations);
	ui_.luxuryAccomodations->setEngagedCount(state.luxuryAccommodationsCount - state.freeLuxuryAccommodations);
	ui_.luxuryAccomodations->setPrice(state.luxuryAccommodationsPrice);
	ui_.warehouses->setRoomCount(state.warehousesWithLifeSupportCount);
	ui_.warehouses->setAvailableCount(state.freeWarehousesWithLifeSupport);
	ui_.warehouses->setEngagedCount(state.warehousesWithLifeSupportCount - state.freeWarehousesWithLifeSupport);
	ui_.warehouses->setPrice(state.warehousesWithLifeSupportPrice);

	ui_.weightValue->setText(Settings::instance().formatNumber(state.accommodationsWeight) + weightUnit_);
	ui_.sizeValue->setText(Settings::instance().formatNumber(state.accommodationsSize) + sizeUnit_);

	ui_.freeConstructionPointsValue->setText(Settings::instance().formatNumber(state.totalPoints - state.consumedPoints));
	ui_.totalConstructionPointsValue->setText(Settings::instance().formatNumber(state.totalPoints));
	ui_.usedConstructionPointsValue->setText(Settings::instance().formatNumber(state.consumedPoints));

	this->state_ = state;
}

void AccommodationsWidget::createAddRoomDialog() {
	addRoomDialog_ = new AddRoomDialog(this);
		
	bool connected = true;		
	connected = connected && connect(addRoomDialog_, SIGNAL(submitSignal()), this, SLOT(submitAddRoomDialogSlot()));
	connected = connected && connect(addRoomDialog_, SIGNAL(cancelSignal()), this, SLOT(cancelAddRoomDialogSlot()));
	assert(connected && "Signals not connected");

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

void AccommodationsWidget::submitAddRoomDialogSlot() {
	emit addAccommodationsSignal(addRoomDialog_->getRoomCount(), addRoomDialog_->getAccommodationType());

	addRoomDialog_->close();
}

void AccommodationsWidget::cancelAddRoomDialogSlot() {
	addRoomDialog_->close();
	emit hideDialogSignal();
}

void AccommodationsWidget::showAddRoomDialog(accommodation_type accommodationType) {
	if (addRoomDialog_ == NULL) {
		createAddRoomDialog();
	}

	emit showDialogSignal();

	addRoomDialog_->setDialog(state_, accommodationType);

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