#include "checkbox.h"
#include "graphics.hpp"
#include <iostream>

using namespace genv;

int checkBox::cnt;

/// ------------------------------------------ Események ----------------------------------------------------------
void checkBox::onClick(int posX, int posY, char button)
{
    if ((x+boxX) < posX && posX < (x+boxX+boxW) && (y+boxY) < posY && posY < (y+boxY+boxH))
        checked = !checked;
}

void checkBox::onKeyPress(char keyCode)
{
    if (keyCode == key_space) checked = ! checked;
}
/// ----------------------------------------------------------------------------------------------------------------


/// ---------------------------------------- Konstruktorok ----------------------------------------------------------
checkBox::checkBox(int x0, int y0, unsigned int width0, unsigned int height0) : widget(x0, y0, width0, height0)
{
    cnt++;
    checked = false;
    myLabel = new label(x0, y0, width0, height0);
    myLabel->setMargin(boxX*2 + boxW, boxY + 2);
    myLabel->setText("CheckBox" + convert(cnt));
    tabStop = true;
}
/// ----------------------------------------------------------------------------------------------------------------


/// --------------------------------------------- Get/Set ----------------------------------------------------------
void checkBox::setText(std::string txt)
{
    myLabel->setText(txt);
}

std::string checkBox::getText() const
{
    return myLabel->getText();
}
/// ----------------------------------------------------------------------------------------------------------------


/// --------------------------------- Működéshez szükséges függvények ------------------------------------------------
void checkBox::draw() const
{
    if (!visible) return;

    if (X < x + width) std::cerr << "Object is out of window! (X < x + width) : checkBox" + convert(cnt) + " - checkBox::draw" << std::endl;
    else if (Y < y + height) std::cerr << "Object is out of window! (Y < y + height) - checkBox" + convert(cnt) + " - checkBox::draw" << std::endl;

    myLabel->draw();

    if (focus) colorize(ActiveColor);
    else colorize(White);
    gout << move_to(x+boxX, y+boxY) << box(boxW,boxH);

    rectange(x+boxX, y+boxY, boxW,boxH);

    if (checked)
    {
        colorize(Black);
        gout << move_to(x+boxX+5, y+boxY+5) << line(boxW-2*boxX,boxH-2*boxY);
        gout << move_to(x+boxX+5, y-boxY+boxH+5) << line(boxW-2*boxX,-boxH+2*boxX);
    }
}
/// ----------------------------------------------------------------------------------------------------------------

