/* DO NOT EDIT THIS FILE */
/* This file is autogenerated by the text-database code generator */

#include <stdarg.h>
#include <touchgfx/Texts.hpp>
#include <touchgfx/hal/HAL.hpp>
#include <touchgfx/TypedText.hpp>
#include <texts/TypedTextDatabase.hpp>
#include <touchgfx/lcd/LCD.hpp>
#include <touchgfx/TextProvider.hpp>

<% if is_rtl %>
touchgfx::Font::StringWidthFunctionPointer touchgfx::Font::getStringWidthFunction = &touchgfx::Font::getStringWidthRTL;
touchgfx::LCD::DrawStringFunctionPointer touchgfx::LCD::drawStringFunction = &touchgfx::LCD::drawStringRTL;
touchgfx::TextProvider::UnicodeConverterInitFunctionPointer touchgfx::TextProvider::unicodeConverterInitFunction = &touchgfx::TextProvider::unicodeConverterInit;
touchgfx::TextProvider::UnicodeConverterFunctionPointer touchgfx::TextProvider::unicodeConverterFunction = &touchgfx::TextProvider::unicodeConverter;
<% else %>
touchgfx::Font::StringWidthFunctionPointer touchgfx::Font::getStringWidthFunction = &touchgfx::Font::getStringWidthLTR;
touchgfx::LCD::DrawStringFunctionPointer touchgfx::LCD::drawStringFunction = &touchgfx::LCD::drawStringLTR;
touchgfx::TextProvider::UnicodeConverterInitFunctionPointer touchgfx::TextProvider::unicodeConverterInitFunction = static_cast<touchgfx::TextProvider::UnicodeConverterInitFunctionPointer>(0);
touchgfx::TextProvider::UnicodeConverterFunctionPointer touchgfx::TextProvider::unicodeConverterFunction = static_cast<touchgfx::TextProvider::UnicodeConverterFunctionPointer>(0);
<% end %>

//Default typed text database
extern const touchgfx::TypedText::TypedTextData* const typedTextDatabaseArray[];

<% if generate_binary_files? %>
extern const touchgfx::Unicode::UnicodeChar EmptyLanguageTexts[];
extern uint32_t const EmptyLanguageIndices[];
<% else %>
<% if remap_strings? %>
TEXT_LOCATION_FLASH_PRAGMA
KEEP extern const touchgfx::Unicode::UnicodeChar texts_all_languages[] TEXT_LOCATION_FLASH_ATTRIBUTE =
{
    <%= all_unicodes %>
};
<% end %>
<% countries.each do |lang| %>
extern uint32_t const indices<%= lang %>[];
<% if not remap_strings? %>
extern const touchgfx::Unicode::UnicodeChar texts<%= lang %>[];
<% end %>
<% end %>
<% end %>

//array holding dynamically installed languages
struct TranslationHeader
{
    uint32_t offset_to_texts;
    uint32_t offset_to_indices;
    uint32_t offset_to_typedtext;
};
static const TranslationHeader* languagesArray[<%= countries.length > 0 ? countries.length : 1 %>] = { 0 };

//Compiled and linked in languages
static const uint32_t* const staticLanguageIndices[] =
{
<% if generate_binary_files? %>
    EmptyLanguageIndices
<% else %>
<% if countries.empty? %>
    0
<% end %>
<% countries.each_with_index do |lang, index| %>
    indices<%= lang %><%= (index==countries.length-1) ? '': ',' %>
<% end %>
<% end %>
};
<% if generate_binary_files? %>
static const touchgfx::Unicode::UnicodeChar* const staticLanguageTexts[] = {
    EmptyLanguageTexts
};
<% else %>
<% if not remap_strings? %>
static const touchgfx::Unicode::UnicodeChar* const staticLanguageTexts[] = {
<% if countries.empty? %>
    0
<% end %>
<% countries.each_with_index do |lang, index| %>
    texts<%= lang %><%= (index==countries.length-1) ? '': ',' %>
<% end %>
};
<% end %>
<% end %>

touchgfx::LanguageId touchgfx::Texts::currentLanguage = static_cast<touchgfx::LanguageId>(0);
static const touchgfx::Unicode::UnicodeChar* currentLanguagePtr = 0;
static const uint32_t* currentLanguageIndices = 0;

void touchgfx::Texts::setLanguage(touchgfx::LanguageId id)
{
    const touchgfx::TypedText::TypedTextData* currentLanguageTypedText = 0;
    if (id < <%= countries.length > 0 ? countries.length : 1 %>)
    {
        if (languagesArray[id] != 0)
        {
            //dynamic translation is added
            const TranslationHeader* translation = languagesArray[id];
            currentLanguagePtr = (const touchgfx::Unicode::UnicodeChar*)(((const uint8_t*)translation) + translation->offset_to_texts);
            currentLanguageIndices = (const uint32_t*)(((const uint8_t*)translation) + translation->offset_to_indices);
            currentLanguageTypedText = (const touchgfx::TypedText::TypedTextData*)(((const uint8_t*)translation) + translation->offset_to_typedtext);
        }
<% if generate_binary_files? %>
        else
        {
            //compiled and linked empty texts and indices in typedTextData
            currentLanguagePtr = staticLanguageTexts[0];
            currentLanguageIndices = staticLanguageIndices[0];
            currentLanguageTypedText = typedTextDatabaseArray[0];
        }
<% else %>
        else
        {
            //compiled and linked in languages
<% if remap_strings? %>
            currentLanguagePtr = texts_all_languages;
            currentLanguageIndices = staticLanguageIndices[id];
<% else %>
            currentLanguagePtr = staticLanguageTexts[id];
            currentLanguageIndices = staticLanguageIndices[id];
<% end %>
            currentLanguageTypedText = typedTextDatabaseArray[id];
        }
<% end %>
    }

    if (currentLanguageTypedText)
    {
        currentLanguage = id;
        touchgfx::TypedText::registerTypedTextDatabase(currentLanguageTypedText,
                                                       TypedTextDatabase::getFonts(), TypedTextDatabase::getInstanceSize());
    }
}

void touchgfx::Texts::setTranslation(touchgfx::LanguageId id, const void* translation)
{
    languagesArray[id] = (const TranslationHeader*)translation;
}

const touchgfx::Unicode::UnicodeChar* touchgfx::Texts::getText(TypedTextId id) const
{
    return &currentLanguagePtr[currentLanguageIndices[id]];
}

