#include <gui/screen1_screen/Screen1View.hpp>
#include "BitmapDatabase.hpp"
#include <touchgfx/Color.hpp>

Screen1View::Screen1View():
    counter(0),
    buttonPressedFlag(false),
    buttonEventCallback(this, &Screen1View::handleButtonEvent)
{
}

void Screen1View::setupScreen()
{
    Screen1ViewBase::setupScreen();

    customButton1.setBitmaps(Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_MEDIUM_ID), Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_MEDIUM_PRESSED_ID));
    customButton1.setBitmapXY(0, 0);
    customButton1.setPosition(285, 200, 230, 60);

    customButton2.setBitmaps(Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_MEDIUM_ID), Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_MEDIUM_PRESSED_ID));
    customButton2.setBitmapXY(0, 0);
    customButton2.setPosition(20, 20, 230, 60);

    add(customButton1);
    add(customButton2);

    customButton1.setAction(buttonEventCallback);
    customButton2.setAction(buttonEventCallback);
}

void Screen1View::tearDownScreen()
{
    Screen1ViewBase::tearDownScreen();
}

void Screen1View::handleButtonEvent(const CustomButtonContainer& element, bool& buttonState)
{
    if(buttonState)
    {
        if(&element == &customButton1)
        {
            touchgfx_printf("pressed 1\n");
            buttonPressedFlag = true;
        }
        else if(&element == &customButton2)
        {
            touchgfx_printf("pressed 2\n");
        }
    }
    else
    {
        if(&element == &customButton1)
        {
            touchgfx_printf("released 1\n");
            buttonPressedFlag = false;
            counter = 0;
        }
        else if(&element == &customButton2)
        {
            touchgfx_printf("released 2\n");
        }
    }
}

void Screen1View::handleTickEvent()
{
   if(buttonPressedFlag)
   {
       counter++;
       if(counter%180 == 0) // 3 seconds has passed
       {
           //move to another screen
           box1.setColor(touchgfx::Color::getColorFrom24BitRGB(0, 0, 255));
           box1.invalidate();
       }
   }
}
