#include <gui/screen1_screen/Screen1View.hpp>
#include <texts/TextKeysAndLanguages.hpp>


// Screen1View constructor
Screen1View::Screen1View()
    : tickCounter(0), targetItem(0), scrollCompleted(false),
      digitalHours(0), digitalMinutes(0), digitalSeconds(0),
      scrollWheelAnimateToCallback(this,&Screen1View::scrollWheel11AnimateToHandler)
{
}

void Screen1View::setupScreen()
{
    Screen1ViewBase::setupScreen();
    scrollWheel1.setAnimateToCallback(scrollWheel1AnimateToCallback);
    tickCounter = 0;  // Reset the tick counter
    textUpdateIntervalCounter = 0; // Reset the interval counter
    targetItem = scrollWheel1.getSelectedItem(); // Initialize target item
    scrollCompleted = false; // Reset scroll completion flag
    digitalHours = digitalClock1.getCurrentHour();
    digitalMinutes = digitalClock1.getCurrentMinute();
    digitalSeconds = digitalClock1.getCurrentSecond();
}

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

void Screen1View::handleTickEvent()
{
    tickCounter++;
    textUpdateIntervalCounter++;

    // Scroll logic: Execute every 0.5 seconds (30 ticks at 60 Hz)
    if (tickCounter % ticksPerStep == 0)
    {
        if (!scrollWheel1.isAnimating() && !scrollCompleted)
        {
            targetItem = (targetItem + 1) % scrollWheel1.getNumberOfItems();
            scrollWheel1.animateToItem(targetItem, 500); // Smooth animation (500ms)
            scrollCompleted = true;
        }
        else if (!scrollWheel1.isAnimating() && scrollCompleted)
        {
            scrollCompleted = false; // Reset flag when animation finishes
        }
    }

    // Clock update logic: Execute every second
    if (tickCounter % 60 == 0)
    {
        if (++digitalSeconds >= 60)
        {
            digitalSeconds = 0;
            if (++digitalMinutes >= 60)
            {
                digitalMinutes = 0;
                if (++digitalHours >= 24)
                {
                    digitalHours = 0;
                }
            }
        }

        digitalClock1.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
    }

    // Text update logic: Execute every 2 seconds (120 ticks at 60 Hz)
}

void Screen1View::resetScroll()
{
    tickCounter = 0;            // Reset tick counter
    scrollCompleted = false;    // Allow scrolling again
}

void Screen1View::scrollWheel1UpdateItem(Scroll_container& item, char itemIndex)
    {
        item.setElements(itemIndex);
    }

void Screen1View::scrollWheel1AnimateToHandler(char item)
{
	textArea1.invalidate();
	Unicode::snprintf(textArea1Buffer, TEXTAREA1_SIZE, "%s", item);
}
