/*
 * Copyright (C) 2019  Max Regan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "Application/Screens/BigDigitalTimeScreen.h"
#include "Application/SystemFonts.h"
#include "Bsp/Drivers/RtcDriver.h"
#include "Bsp/SystemTime.h"
#include "Bsp/Drivers/LowPower.h"

using BSP::ReturnCode;
using BSP::Time;
using BSP::Schedule::NextTime;
using Color = BSP::DisplayDriver::Color;

BigDigitalTimeScreen::BigDigitalTimeScreen(BSP::DisplayDriver &driver,
                                     ScreenManager &manager,
                                     Screen &menu_screen)
    : m_driver(driver)
    , m_last_time()
    , m_manager(manager)
    , m_menu_screen(menu_screen)
    , m_display_seconds(true)
{}

static char get_char_for_digit(uint8_t bcd_digit)
{
    if (bcd_digit > 9) {
        return '0';
    }
    return bcd_digit + '0';
}

ReturnCode BigDigitalTimeScreen::init()
{
    return ReturnCode::OK;
}

void BigDigitalTimeScreen::display_number(uint32_t *x, uint32_t y, uint32_t tens, uint32_t ones, const struct font &f)
{
    char time_str[3] = { 0 };

    time_str[0] = get_char_for_digit(tens);
    time_str[1] = get_char_for_digit(ones);
    time_str[2] = '\0';

    m_driver.string_at(x, y, time_str, &f, Color::BLACK);
}

void BigDigitalTimeScreen::display_time()
{
    BSP::WallClockTime time;
    BSP::RtcDriver::get_time(time);
    const struct font &font = font_large_digits;
    const uint32_t y_space = (m_driver.get_height() - (2 * font.height)) / 3;
    const uint32_t x_space = (m_driver.get_width() - (2 * font.width)) / 2;

    uint32_t x = 0;

    m_driver.clear(Color::WHITE);

    x = x_space;
    display_number(&x, y_space,
                   time.get_hours_12_tens(), time.get_hours_12_ones(), font);

    x = x_space;
    display_number(&x, y_space * 2 + font.height,
                   time.get_minutes_tens(), time.get_minutes_ones(), font);

    m_last_time = time;
    m_driver.refresh();
}

NextTime BigDigitalTimeScreen::execute()
{
    display_time();

    BSP::time_t now;
    BSP::SystemTimer::get_time(now);
    if (m_display_seconds) {
        return NextTime::in(Time::seconds(1));
    } else {

        BSP::WallClockTime wall_time;
        BSP::RtcDriver::get_time(wall_time);
        return NextTime::in(Time::seconds(61 - wall_time.get_seconds()));
    }
}


void BigDigitalTimeScreen::enable() {
    m_last_time = {};
    display_time();
}

void BigDigitalTimeScreen::disable() {
}

void BigDigitalTimeScreen::notify_up_button() {
    /* TODO: This should open a menu first */
    m_manager.push_screen(m_menu_screen);
}

void BigDigitalTimeScreen::notify_middle_button() {

}

void BigDigitalTimeScreen::notify_down_button() {

}
