/*
    PIP - Platform Independent Primitives
	Translation support
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pitranslator.h"

#include "pidir.h"
#include "pifile.h"
#include "piliterals_string.h"
#include "pitranslator_p.h"
#include "pivaluetree_conversions.h"


//! \class PITranslator pitranslator.h
//! \details
//! \~english \section PITranslator_sec0 Synopsis
//! \~russian \section PITranslator_sec0 Краткий обзор
//! \~english
//!
//! \~russian
//!


PRIVATE_DEFINITION_START(PITranslator)
	PITranslatorPrivate::Translation content;
PRIVATE_DEFINITION_END(PITranslator)


PIString PITranslator::tr(const PIString & in, const PIString & context) {
	return instance()->PRIVATEWB->content.translate(in, context);
}


void PITranslator::clear() {
	instance()->PRIVATEWB->content.clear();
}


void PITranslator::loadLang(const PIString & short_lang, PIString dir) {
	if (dir.isEmpty()) dir = PIDir::current().absolute("lang");
	clear();
	auto files = PIDir(dir).entries();
	for (const auto & f: files) {
		if (!f.baseName().endsWith(short_lang)) continue;
		loadFile(f.path);
	}
	piCout << "Loaded %1 string for lang \"%2\""_a.arg(instance()->PRIVATEWB->content.count()).arg(short_lang);
	/*auto s    = instance();
	auto vt   = PIValueTreeConversions::fromText(getBuiltinConfig());
	auto lang = vt.child(short_lang.toLowerCase().trim());
	for (const auto & cn: lang.children()) {
		auto c = s->PRIVATEWB->content.createContext(cn.name());
		for (const auto & s: cn.children())
			c->add(s.name(), s.value().toString());
	}*/
}


void PITranslator::loadConfig(const PIString & content) {
	auto s    = instance();
	auto lang = PIValueTreeConversions::fromText(content);
	for (const auto & cn: lang.children()) {
		auto c = s->PRIVATEWB->content.createContext(cn.name());
		for (const auto & s: cn.children())
			c->add(s.name(), s.value().toString());
	}
	auto c = s->PRIVATEWB->content.createContext("");
	for (const auto & s: lang.children()) {
		if (s.hasChildren()) continue;
		c->add(s.name(), s.value().toString());
	}
}


bool PITranslator::load(const PIByteArray & content) {
	return instance()->PRIVATEWB->content.load(content);
}


bool PITranslator::loadFile(const PIString & path) {
	PIFile f(path, PIIODevice::ReadOnly);
	if (f.isClosed()) return false;
	if (!PITranslatorPrivate::checkHeader(&f)) return false;
	auto data = f.readAll();
	data.remove(0, PITranslatorPrivate::headerSize());
	return load(data);
}


PITranslator::PITranslator() {}


PITranslator::~PITranslator() {
	PRIVATE->content.clear();
}


PITranslator * PITranslator::instance() {
	static PITranslator ret;
	return &ret;
}
