#include "label_store.h"
#include "../widget/logger.h"



struct LabelAndColor {
	LabelAndColor(const std::string& lab = "#", const Colorf& c = Colorf(0, 0, 0, 1))
		: label(lab)
		, color(c)
	{
	}

	std::string label;
	Colorf		color;
};


//////////////////////////////////////////////////////////////////////////


const static LabelAndColor valid_labels[NUM_VALID_LABELS] = {
	LabelAndColor(GROUND,		ground_color),		// 0
	LabelAndColor(WALL,			wall_color),		// 1

	LabelAndColor(CHAIR,		chair_color),		// 2
	LabelAndColor(TABLE,		table_color),		// 3
	LabelAndColor(VASE,			vase_color),		// 4
	LabelAndColor(KEYBOARD,		keyboard_color),	// 5
	LabelAndColor(MONITOR,		monitor_color),		// 6
	LabelAndColor(CABINET,		cabinet_color),		// 7
	LabelAndColor(BED,			bed_color),			// 8
};

//////////////////////////////////////////////////////////////////////////

bool LabelStore::initialized_ = false;

std::map<std::string, int>		LabelStore::label_to_index_;
std::map<std::string, Colorf>	LabelStore::label_to_color_;

//////////////////////////////////////////////////////////////////////////

void LabelStore::initialize() {
	for (unsigned int i=0; i<NUM_VALID_LABELS; ++i) {
		const std::string& label = valid_labels[i].label;
		label_to_index_[label] = i;
	}

	for (unsigned int i=0; i<NUM_VALID_LABELS; ++i) {
		const std::string& label = valid_labels[i].label;
		const Colorf& color = valid_labels[i].color;
		label_to_color_[label] = color;
	}

	initialized_ = true;
}


int LabelStore::num_valid_labels() {
	return NUM_VALID_LABELS;
}



int	LabelStore::what_index(const std::string& label) {
	if (initialized_ == false) {
		Logger::output(title()) << "Error: LabelStore not initialized" << Logger::endl();
	}

	if (is_label_valid(label) == false) {
		Logger::output(title()) << "Warning: \'" << label << "\' is not a valid label" << Logger::endl();
		return -1;
	} 
	else
		return label_to_index_[label];
}


Colorf LabelStore::what_color(const std::string& label) {
	if (initialized_ == false) {
		Logger::output(title()) << "Error: LabelStore not initialized" << Logger::endl();
	}

	if (is_label_valid(label) == false) {
		Logger::output(title()) << "Warning: \'" << label << "\' is not a valid label" << Logger::endl();
		return Colorf(0.0f, 0.0f, 0.0f, 1.0f);
	}
	else
		return label_to_color_[label];
}


std::string	LabelStore::what_label(int idx) {
	if (initialized_ == false) {
		Logger::output(title()) << "Error: LabelStore not initialized" << Logger::endl();
	}

	if (idx < 0 || idx >= NUM_VALID_LABELS) {
		Logger::output(title()) << "Warning: \'" << idx << "\' is not a valid index." << Logger::endl();
		return "unknown";
	}
	else
		return valid_labels[idx].label;
}


Colorf	LabelStore::what_color(int idx) {
	if (initialized_ == false) {
		Logger::output(title()) << "Error: LabelStore not initialized" << Logger::endl();
	}

	if (idx < 0 || idx >= NUM_VALID_LABELS) {
		Logger::output(title()) << "Warning: \'" << idx << "\' is not a valid index." << Logger::endl();
		return Colorf(0.0f, 0.0f, 0.0f, 1.0f);
	}
	else
		return valid_labels[idx].color;
}


bool LabelStore::is_label_valid(const std::string& label) {
	if (initialized_ == false) {
		Logger::output(title()) << "Error: LabelStore not initialized" << Logger::endl();
	}

	return (label_to_index_.find(label)) != (label_to_index_.end());
}