/*
 * 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 "ScreenManager.h"
#include "ButtonManager.h"

// TODO: Basically all of the error checking

using BSP::Schedule::Task;
using BSP::Schedule::NextTime;
using BSP::ReturnCode;

ScreenManager::ScreenManager(BSP::Schedule::TaskScheduler &scheduler,
                             BSP::DisplayDriver &display,
                             BSP::ButtonManager &buttons)
    : m_scheduler(scheduler)
    , m_screen_stack{nullptr}
    , m_screen_stack_depth(0)
    , m_display(display)
    , m_buttons(buttons)
{
}

ReturnCode ScreenManager::init()
{
    m_buttons.set_callback(
        BSP::ButtonManager::Button::UP,
        [this](BSP::ButtonManager::ButtonState state) {
            if (state == BSP::ButtonManager::ButtonState::PRESSED) {
                current_screen()->notify_up_button();
            }});

    m_buttons.set_callback(
        BSP::ButtonManager::Button::MID,
        [this](BSP::ButtonManager::ButtonState state) {
            if (state == BSP::ButtonManager::ButtonState::PRESSED) {
                return current_screen()->notify_middle_button();
            }});

    m_buttons.set_callback(
        BSP::ButtonManager::Button::DOWN,
        [this](BSP::ButtonManager::ButtonState state) {
            if (state == BSP::ButtonManager::ButtonState::PRESSED) {
                current_screen()->notify_down_button();
            }});

    m_display.clear();

    return ReturnCode::OK;
}

NextTime ScreenManager::execute()
{
    return current_screen()->execute();
}

ReturnCode ScreenManager::set_root_screen(Screen &screen)
{
    m_screen_stack[m_screen_stack_depth] = &screen;
    m_screen_stack_depth = 1;
    current_screen()->enable();
    m_scheduler.add_task(*this, NextTime::asap());

    return ReturnCode::OK;
}

ReturnCode ScreenManager::push_screen(Screen &screen)
{
    if (m_screen_stack_depth == MAX_SCREEN_STACK) {
        return ReturnCode::FAIL;
    }

    // TODO: "Lock" changes during this operation. Don't allow
    // disable/enable to push/pop.

    current_screen()->disable();
    m_screen_stack[m_screen_stack_depth] = &screen;
    m_screen_stack_depth++;
    current_screen()->enable();
    m_scheduler.add_task(*this, NextTime::asap());

    return ReturnCode::OK;
}

ReturnCode ScreenManager::pop_screen() {
    if (m_screen_stack_depth == 1) {
        return ReturnCode::FAIL;
    }

    current_screen()->disable();
    m_screen_stack_depth--;
    m_screen_stack[m_screen_stack_depth] = nullptr;
    current_screen()->enable();
    m_scheduler.add_task(*this, NextTime::asap());

    return ReturnCode::OK;
}

ReturnCode ScreenManager::set_screen(Screen &screen) {
    if (m_screen_stack_depth == 1) {
        return ReturnCode::FAIL;
    }

    current_screen()->disable();
    m_screen_stack[m_screen_stack_depth - 1] = &screen;
    current_screen()->enable();
    m_scheduler.add_task(*this, NextTime::asap());

    return ReturnCode::OK;
}
