#include "aux_font.h"
#include "jgame.h"

namespace font
{
    static draw::surface *font1 = nullptr;
    static draw::surface *font2 = nullptr;
    static int mode = font::type::normal;

    void init()
    {
        font::font1 = draw::loadSurface("fuente1.gif");
        font::font2 = draw::loadSurface("fuente2.gif");
    }

    void selectFont(const int which)
    {
        font::mode = which;
    }

    void setColor(const int color)
    {
        switch (color) {
            case font::color::white: draw::setPaletteEntry(63, 255, 255, 255); break;
            case font::color::red: draw::setPaletteEntry(63, 255, 0, 0); break;
            case font::color::green: draw::setPaletteEntry(63, 0, 255, 0); break;
            case font::color::blue: draw::setPaletteEntry(63, 0, 0, 255); break;
        }

    }

    void print(const int x, const int y, const std::string text)
    {
        draw::setSource(font::mode == font::type::colored ? font::font2 : font::font1);
        draw::setTrans(0);

        if (font::mode == font::type::fade) for (int i=64;i<=67;++i) draw::swapcol(i, i+4);

        const int len = text.length();
        for (int i=0;i<len;++i)
        {
            char chr = text[i];
            if (font::mode == font::type::colored)
                draw::draw(x+i*7, y, 6, 6, (int(chr)-32)*7, 0);
            else
                draw::draw(x+i*7, y, 5, 5, (int(chr)-32)*7, 0);
        }

        for (int i=64;i<=67;++i) draw::restorecol(i);

        draw::setTrans(255);
    }

    void print(const int x, const int y, const int num)
    {
        const char txt[3] = { char(num/10 + 48), char(num%10 + 48), 0 };
        print(x, y, txt);
    }
}