#include "graphics.hpp"
#include "label.h"
#include <iostream>

using namespace genv;
using namespace std;

int label::cnt;

/// ---------------------------------------- Konstruktorok ----------------------------------------------------------
void label::labelInit()
{
    cnt++;
    marginSide = 10; marginTop = 7;
    visible = true;
    background = false;
    fontColor = Black;
    backgroundColor = "#ffffff";
    align = align_left;
    tabStop = false;
    autoSize = false;
    trimEnd = false;
}

label::label(int x0, int y0, unsigned int width0, unsigned int height0) : widget(x0, y0, width0, height0)
{
    labelInit();
    setText("Label" + convert(cnt));
}

label::label(int x0, int y0, unsigned int width0, unsigned int height0, string txt) : widget(x0, y0, width0, height0)
{
    labelInit();
    setText(txt);
}
/// ----------------------------------------------------------------------------------------------------------------


/// --------------------------------------------- Get/Set ----------------------------------------------------------
void label::setText(string txt)
{
    Text = txt;
    dispText = txt;
    if (autoSize)
    {
        width = marginSide * 2 + gout.twidth(txt);
    }
    else if (width < marginSide * 2 + gout.twidth(txt))
    {
        // annak keresése, hogy a szövegnek mekkora része fér ki
        if(trimEnd)
        {
            for (unsigned int i = txt.length()-1; i > 0 ; i--)
            {
                dispText = "..." + txt.substr(i, txt.length());
                if (width < marginSide * 2 + gout.twidth(dispText) + 4) break;
            }
        }
        else
        {
            for (unsigned int i = txt.length()-1; i > 0 ; i--)
            {
                dispText = txt.substr(0,i) + "...";
                if (width > marginSide + gout.twidth(dispText) + 4) break;
            }
        }

    }
}
string label::getText() const { return Text; }

void label::setMargin(int mS, int mT)
{
    marginSide = mS;
    marginTop = mT;
}
/// ----------------------------------------------------------------------------------------------------------------


/// --------------------------------- Mûködéshez szükséges függvények ------------------------------------------------
void label::labelDraw() const
{
    if (X < x + width) std::cerr << "Object is out of window! (X < x + width) : label" + convert(cnt) + " - label::labelDraw" << std::endl;
    else if (Y < y + height) std::cerr << "Object is out of window! (Y < y + height) - label" + convert(cnt) + " - label::labelDraw" << std::endl;

    if (background)
    {
        colorize(backgroundColor);
        gout << move_to(x,y) << box(width, height);
    }
    colorize(fontColor);
    if (align == align_right)
    {
        gout << move_to(x + width - gout.twidth(dispText)  - marginSide, y + marginTop + gout.cascent()) << text(dispText);
    }
    else if (align == align_center)
    {
        gout << move_to(x + (width - gout.twidth(dispText))/2, y + marginTop + gout.cascent()) << text(dispText);
    }
    else
    {
        gout << move_to(x + marginSide, y + marginTop + gout.cascent()) << text(dispText);
    }
}

void label::draw() const
{
    if (visible) labelDraw();
}
/// ----------------------------------------------------------------------------------------------------------------


