#include "graphics.hpp"
#include <iostream>
#include <vector>

#include "widget.h"
#include "focus.h"
#include "color.h"
#include "widget.h"
#include "label.h"
#include "checkbox.h"

using namespace genv;
using namespace std;

/// ---- Vezérlők --------------------
std::vector<widget*> widgets;
focus Focus(&widgets);

label* label1 = new label(50,50,150,30);
checkBox* checkBox1 = new checkBox(50,100,150,30);
checkBox* checkBox2 = new checkBox(50,150,150,30);
checkBox* checkBox3 = new checkBox(50,200,150,30);

/// ---- Konstansok ------------------
// rajzvászon mérete
const unsigned int X = 500;
const unsigned int Y = 500;

const unsigned int t = 40;  // timer idõköze

/// ---- Függvények ------------------
// Ablak törlése
inline void scrClear()
{
    colorize(backgroundColor);
    gout << move_to(0,0) << box(X,Y);
}

// Általános billentyűvezérlés, gyorsbillentyűk
bool keyEvents(event &ev)
{
    if (ev.keycode == key_tab)
    {
        Focus.next();
        return true;
    }
    return false;
}

/// ---- Main ------------------------
int main ()
{
    gout.open(X,Y);

    widgets.push_back(label1);
    widgets.push_back(checkBox1);
    widgets.push_back(checkBox2);
    widgets.push_back(checkBox3);

    Focus.setFocus(1);

    event ev;
    gin.timer(t);
    while (gin >> ev && ev.keycode != key_escape)
    {
        scrClear();

        if (ev.type == ev_key && keyEvents(ev)) continue;

        // Vezérlők kirajzolása
        for (unsigned int i = 0; i < widgets.size(); i++)
            widgets[i]->draw();

        // Ha több elemhez is eljutna az esemény, akkor csak a legfelső kapja meg, és az kerüljön fókuszba
        for (int i = widgets.size()-1; i >= 0; i--)
            if (widgets[i]->event(ev)) Focus.setFocus(i);

        if (checkBox1->checked) label1->setText("This text has changed.");
        else label1->setText("Hello World!");

        gout << refresh;

    }

    return 0;
}
