/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/core/text/sgxtext.cpp

*/

#include "sgx/core/core.hpp"

sgxHashMap<tUINT32, SGXStringEntry *> *SGXText::byHash;
sgxHashMap<tUINT32, SGXStringEntry *> *SGXText::byID;

void
SGXText::initialize() {
	SGXText::byHash = new sgxHashMap<tUINT32, SGXStringEntry *>;
	SGXText::byID = new sgxHashMap<tUINT32, SGXStringEntry *>;
}

void 
SGXText::addStringEntry(const tUINT32 id, SGXStringEntry *pEntry) {
	(*byID)[id] = pEntry;
}

void 
SGXText::addStringEntry(const sgxString &hash, SGXStringEntry *pEntry) {
	 (*byHash)[sgxGetCRC32(hash)] = pEntry;
}

tBOOL SGXText::eraseStrings(const tUINT32 fromStringID, const tUINT32 toStringIDExclusive) {
	for(tUINT32 i=fromStringID;i<toStringIDExclusive;++i) {
		delete (*byID)[i];
		(*byID)[i] = NULL;
	}
	return TRUE;
}


//
// Variant retrieval methods
//
const char *SGXText::getText(const char *textString, const tINT32 number /*= 0*/) {
	 tINT32 hash = sgxGetCRC32(textString);
	 SGXStringEntry *pEntry = (*byHash)[hash];
	 if (pEntry) {
		 return (const char *)getText(pEntry, number).c_str();
	}
	 return textString;
}

const sgxString &SGXText::getText(const tUINT32 keyID, const tINT32 number /*= 0*/) {
	SGXStringEntry *pEntry = (*byID)[keyID];

	if (pEntry) {
		return getText(pEntry, number);
	}

#ifdef SGX_DEBUG
	 static sgxString empty("*No String*");
#else
	 static sgxString empty;
#endif
	 return empty;
}

const char *SGXText::getTextString(const tUINT32 keyID, const tINT32 number /*= 0*/) {
	return getText(keyID, number).c_str();
}

const sgxString &SGXText::getText(const SGXStringEntry *pEntry, const tINT32 number /* = 0 */) {
	sgxAssert(pEntry);

	sgxHashMap<tINT32, sgxString>::const_iterator cit = pEntry->vOthers.find(number);

	if (cit != pEntry->vOthers.end()) {
		// We have a specific numeral for this string, so use it
		return (*cit).second;
	} else if (number == 1 || pEntry->vPlural == "") {
		// Use the singular version when there's no plural, or it's obviously a single (i.e. 1)
		return pEntry->vSingle;
	} else {
		return pEntry->vPlural;
	}
}




