#include "textbox.h"
#include "graphics.hpp"
#include <iostream>

using namespace genv;
using namespace std;

int textBox::cnt;

/// ---------------------------------------- Konstruktorok ----------------------------------------------------------
void textBox::textInit()
{
    background = true;
    backgroundColor = White;
    tabStop = true;
    pressedKey = 0;
    pressedTime = 0;
    cursorOn = true;
    trimEnd = true;
}

textBox::textBox(int x0, int y0, unsigned int width0, unsigned int height0) : label(x0, y0, width0, height0)
{
    textInit();
    setText("TextBox");
}

textBox::textBox(int x0, int y0, unsigned int width0, unsigned int height0, string txt) : label(x0, y0, width0, height0, txt)
{
    textInit();
    setText(txt);
}
/// ----------------------------------------------------------------------------------------------------------------


/// ------------------------------------------- Események ----------------------------------------------------------
void textBox::onKeyPress(char keyCode)
{
    if (key_space <= keyCode && keyCode <= 255)
    {
        setText(Text + keyCode);
        //pressedKey = keyCode;
    }
    if (keyCode == key_backspace)
    {
        setText(Text.substr(0,Text.length()-1));
        //pressedKey = key_backspace;
    }
}

void textBox::onKeyUp(char keyCode)
{
    pressedKey = 0;
    pressedTime = 0;
}

void textBox::onTick()
{
    if (pressedKey !=0 ) pressedTime++;
    if (pressedTime > pressInterval / t)
    {
        if (pressedKey == key_backspace) setText(Text.substr(0,Text.length()-1));
        else setText(Text + pressedKey);
    }

    cursorTime++;
    if (cursorTime > cursorInterval / t) cursorOn = true;
        else cursorOn = false;
    if (cursorTime > cursorInterval / t * 2) cursorTime = 0;
}
/// ----------------------------------------------------------------------------------------------------------------


/// --------------------------------- Működéshez szükséges függvények ------------------------------------------------

void textBox::draw() const
{
    if (!visible) return;

    if (X < x + width) std::cerr << "Object is out of window! (X < x + width) - textBox" + convert(cnt) + " - textBox::draw" << std::endl;
    else if (Y < y + height) std::cerr << "Object is out of window! (Y < y + height) - textBox" + convert(cnt) + " - textBox::draw" << std::endl;

    labelDraw();

    rectange(x, y, width, height);

    if (focus && cursorOn)
    {
        colorize(Black);
        if (align == align_right)
            gout << move_to(x + width - marginSide, y + marginTop) << line(0, gout.cascent() + gout.cdescent());
        else if (align == align_center)
            gout << move_to(x + (width + gout.twidth(dispText))/2, y + marginTop) << line(0, gout.cascent() + gout.cdescent());
        else
            gout << move_to(x + marginSide + gout.twidth(dispText), y + marginTop) << line(0, gout.cascent() + gout.cdescent());
    }
}

/// ----------------------------------------------------------------------------------------------------------------
