#include "PDB_viewer.h"
#include "ColorSchemeFromFile.h"
#include <fstream>
#include <sstream>
#include <cstdio>

ColorSchemeFromFile::ColorSchemeFromFile(std::string filepath)
{
	std::ifstream input(filepath);
	if (!input.is_open())
	{
		return;
	}
	while (input.good())
	{
		std::string line; std::getline(input, line);
		std::istringstream linestream(line);
		std::string name; int color;
		if (linestream >> name >> std::hex >> color)
		{
			std::transform(name.begin(), name.end(), name.begin(), toupper);
			colorMap[name] = hexColorToVector(color);
		}
	}
	initialized = true;
	input.close();
}

FVector ColorSchemeFromFile::getColorForElement(std::string elementName)
{
	std::transform(elementName.begin(), elementName.end(), elementName.begin(), toupper);
	auto entry = colorMap.find(elementName);
	if (entry != colorMap.end())
		return entry->second;
	return INVALID_COLOR;
}

bool ColorSchemeFromFile::isInitialized() const
{
	return initialized;
}
