
#include <SDL\SDL_video.h>

#include "Other.h"
//====== PANIC BUTTON =========

itfzPanicB::itfzPanicB(int hor, int ver, string tit, SDL_Surface * screen) {
    posX = hor;
    posY = ver;
    scr = screen;
    value = true;
    boton = itfz_LoadIMG("pb");
    area.x = posX + 45;
    area.y = posY + 45;
    area.h = boton->h;
    area.w = boton->w;
    textotitulo = tit;
    titulo = itfz_DrawText(futurab14, GRIS, textotitulo.c_str());
    bg = itfz_LoadIMG("bgx1");
    paint();

}

void itfzPanicB::paint(void) {
    itfz_PosIMG(bg, posX, posY, scr);
    itfz_PosIMG(titulo, posX + 25, posY + 10, scr);
    itfz_PosIMG(boton, posX + 45, posY + 45, scr);
}

void itfzPanicB::clickhandler(int, int) {
    SDL_mutexP(itfzWriteLock);
    SDL_mutexP(itfzRXBuffLock);
    SDL_mutexP(itfzTXBuffLock);
    MZnetBufferTx = "aph\n";
    MZnetQueuedSending = 1;
    SDL_mutexV(itfzTXBuffLock);
    while (MZnetQueuedSending > 0) {
        SDL_Delay(1000);
    }
    SDL_mutexV(itfzRXBuffLock);
    SDL_mutexV(itfzWriteLock);
}

//====== ACEL =========

itfzAcel::itfzAcel(int hor, int ver, string tit, SDL_Surface * screen) {
    posX = hor;
    posY = ver;
    posYtmp = posY + 245;
    scr = screen;
    value = 0;

    palanca = itfz_LoadIMG("ac");
    bg = itfz_LoadIMG("bga");
    textotitulo = tit;
    titulo = itfz_DrawText(futurab14, GRIS, textotitulo.c_str());
    textovalor = itfz_DrawText(futurab24, NEGRO, "0%");

    area.x = posX;
    area.y = posY;
    area.h = bg->h;
    area.w = bg->w;

    apal.x = posX + 52;
    apal.y = posY + 45;
    apal.h = 225;
    apal.w = palanca->w;



    clicked = false;


    rescale();
    paint();

}

void itfzAcel::rescale(void) {
    char txtvalor[5];

    if (clicked) {
        if (MOUSEy <= (posY + 255) && MOUSEy >= (posY + 55))
            value = ((posY + 255) - MOUSEy) / 2;
        else if (MOUSEy >= (posY + 255))
            value = 0;
        else if (MOUSEy <= (posY + 55))
            value = 100;
    }
    posYtmp = posY + (245 - (2 * value));

    sprintf(txtvalor, "%d%%", value);
    textovalor = itfz_DrawText(futurab24, NEGRO, txtvalor);
    marca = itfz_DrawText(futurab10, ROJO, "IGNITION -");
}

void itfzAcel::paint(void) {
    itfz_PosIMG(bg, posX, posY, scr);
    itfz_PosIMG(titulo, posX + 25, posY + 10, scr);
    itfz_PosIMG(marca, posX + 8, posY + 165, scr);
    rescale();
    itfz_PosIMG(palanca, posX + 52, posYtmp, scr);
    itfz_PosIMG(textovalor, posX + 120, posYtmp + 15, scr);
}

void itfzAcel::clickhandler(int x, int y) {
    if (mzsinside(apal, x, y))
        clicked = true;
}

void itfzAcel::unclickhandler(void) {
    clicked = false;
    stringstream val;
    val << (62 * value / 100) + 34;
   // MZnetSend("acel" + val.str());
    MZnetSend("acel" + val.str());
}

//======= TEXT BOX ===========

itfzTxtBox::itfzTxtBox(int hor, int ver, string tit, string values, bool updtabl, string id, SDL_Surface * screen) {
    posX = hor;
    posY = ver;
    scr = screen;
    value = values;
    box = itfz_LoadIMG("tb");
    textotitulo = tit;
    updateable = updtabl;
    ID = id;
    textovalor = NULL;
    titulo = itfz_DrawText(futurab14, GRIS, textotitulo.c_str());
    paint();

}

void itfzTxtBox::paint(void) {
    itfz_PosIMG(box, posX, posY, scr);
    itfz_PosIMG(titulo, posX - 130, posY + 3, scr);
    SDL_FreeSurface(textovalor);
    textovalor = itfz_DrawText(futurab10, NEGRO, value.c_str());
    itfz_PosIMG(textovalor, posX + 5, posY + 3, scr);
}
//======= BG ===========

itfzBG::itfzBG(int hor, int ver, SDL_Surface * screen) {
    posX = hor;
    posY = ver;
    scr = screen;
    bg = itfz_LoadIMG("bg");
    bgx3 = itfz_LoadIMG("bgx3");
    bgx4 = itfz_LoadIMG("bgx4");
    logo = itfz_LoadIMG("logo");
    lng = itfz_LoadIMG("lng");
    identificacion = itfz_DrawText(futurab14, GRIS, "IDENTIFICACION");
    presion = itfz_DrawText(futurab14, GRIS, "PRESION");
    temperatura = itfz_DrawText(futurab14, GRIS, "TEMPERATURA");
    rendimiento = itfz_DrawText(futurab14, GRIS, "RENDIMIENTO");
    historico = itfz_DrawText(futurab16, GRIS, "HISTORICO");
    graficos = itfz_DrawText(futurab16, GRIS, "GRAFICOS");
    paint();
}

void itfzBG::paint(void) {
    SDL_FillRect(scr, NULL, 0xffffff); // Fondo blanco
    itfz_PosIMG(bg, 18, 55, scr); // Carga el fondo
    itfz_PosIMG(bgx3, 40, 75, scr); //  Barra de 3x
    itfz_PosIMG(bgx3, 40, 230, scr); // Barra de 3x
    itfz_PosIMG(bgx3, 40, 385, scr); // Barra de 3x
    itfz_PosIMG(bgx4, 40, 540, scr); // Barra de 4x
    itfz_PosIMG(logo, 25, 10, scr); // Logo
    itfz_PosIMG(lng, 660, 20, scr); // Lengueta
    itfz_PosIMG(lng, 833, 20, scr); // Lengueta
    itfz_PosIMG(identificacion, 58, 85, scr);
    itfz_PosIMG(presion, 62, 240, scr);
    itfz_PosIMG(temperatura, 60, 395, scr);
    itfz_PosIMG(rendimiento, 60, 549, scr);
    itfz_PosIMG(historico, 700, 35, scr);
    itfz_PosIMG(graficos, 875, 35, scr);
}
//====== TEXT =========

itfzTXT::itfzTXT(int hor, int ver, SDL_Surface * screen) {
    posX = hor;
    posY = ver;
    scr = screen;
    text = "";
    paint();

}

void itfzTXT::paint(void) {
    //itfz_PosIMG(drawtext(loadfont("fonts/futurab.ttf",10),NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "HOLA MUNDO!", solid),500,500,scr);
}
