#pragma once

#include <vector>
#include <map>
#include <string>
#include <common.h>

#include "font.h"

namespace render::text{

class text
{
public:
    vec2f rect;

    text(Font* = 0);
    text(const std::string&, Font* = 0);
    text(const std::wstring&, Font* = 0);

    void make(const std::string&);
    void make(const std::wstring&);

    void addf(const std::string&, ...);
    void addf(const std::wstring&, ...);
    void format(const std::string&, ...);
    void format(const std::wstring&, ...);

    void push_back(const std::string&);
    void push_back(const std::wstring&);

    void push_back(wchar_t);

    void insert(size_t, const std::string&, size_t = 0, int = -1);
    void insert(size_t, const std::wstring&, size_t = 0, int = -1);
    void insert(size_t, wchar_t);

    void pop_back();
    void clear();
    void erase(size_t = 0, size_t = -1);

    size_t length();
    size_t size();
    bool empty();

    void draw(vec2f = vec2f());

    void setFont(Font*);

    const std::wstring &value();

    void operator+=(const std::string &str){push_back(str);}
    void operator+=(const std::wstring &str){push_back(str);}

    void operator+=(wchar_t str){push_back(str);}

    void operator=(const std::string &str){make(str);}
    void operator=(const std::wstring &str){make(str);}
protected:
    std::wstring _value;

    void _remath();
    bool _mathChar(wchar_t, uint32_t*);
public:
    vec4f color;
    float scale;
    bool justifyAlign;
    enum{
        align_left = 0,
        align_center,
        align_right
    }align;
    enum{
        valign_top = 0,
        valign_middle,
        valign_bottom
    }valign;

    std::map<std::string, std::string> customValues;

    static std::wstring UTF8toWide(const std::string&);
    static std::string WidetoUTF8(const std::wstring&);
private:

    struct _symbolInfo{
        vec3f color;
        uint32_t code;
        bool useColor;

        _symbolInfo(uint32_t d)
        {
            code = d;
            useColor = false;
        }
    };

    std::vector<_symbolInfo> _data;
    Font *_font;
    Font::drawGlyphInfo *_glyphs;
};

}
