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

#include "stm32l0xx.h"

namespace BSP {

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

ButtonManager *ButtonManager::m_instance = nullptr;

ReturnCode ButtonManager::init()
{
    for (auto &btn: m_buttons) {
        /** Enable pin for input (pulled up) */
        CLR(GPIOA->MODER, 3u << (2 * btn.m_gpio_idx));
        CLR(GPIOA->OTYPER, 1u << btn.m_gpio_idx);
        SET_TO(GPIOA->PUPDR, 3u << (2 * btn.m_gpio_idx), 1u << (2 * btn.m_gpio_idx));

        // Unmask this interrupt
        SET(EXTI->IMR, 1u << btn.m_gpio_idx);
        // Enable this interrupt
        SET(EXTI->EMR, 1u << btn.m_gpio_idx);
        // Enable interrupt for rising edge
        SET(EXTI->RTSR, 1u << btn.m_gpio_idx);
        // Enable interrupt for falling edge
        SET(EXTI->FTSR, 1u << btn.m_gpio_idx);
    }

    CLR(SYSCFG->EXTICR[0],
        SYSCFG_EXTICR1_EXTI3 | SYSCFG_EXTICR1_EXTI2 |
        SYSCFG_EXTICR1_EXTI1 | SYSCFG_EXTICR1_EXTI0);

    NVIC_EnableIRQ(EXTI0_1_IRQn);
    NVIC_EnableIRQ(EXTI2_3_IRQn);
    NVIC_EnableIRQ(EXTI4_15_IRQn);

    NVIC_SetPriority(EXTI0_1_IRQn, 1);
    NVIC_SetPriority(EXTI2_3_IRQn, 1);
    NVIC_SetPriority(EXTI4_15_IRQn, 1);

    return ReturnCode::OK;
}

NextTime ButtonManager::execute()
{
    BSP::time_t systime;
    BSP::time_t endtime = 0;
    BSP::SystemTimer::get_time(systime);


    for (auto &btn: m_buttons) {
        // Has the state changed?
        if (btn.m_prev_call_state != btn.m_state) {
            // Have we 'debounced' this state?
            if (btn.m_state_change_ts + btn.m_debounce_time < systime) {
                // It's tiiiiime
                if (btn.m_callback != nullptr) {
                    btn.m_callback(btn.m_state);
                }
                btn.m_prev_call_state = btn.m_state;
                continue;
            } else {

                // It's not time yet. Use this to figure out the next time we should check
                if (endtime == 0 || btn.m_state_change_ts + btn.m_debounce_time < endtime) {
                    endtime = btn.m_state_change_ts + btn.m_debounce_time;
                }
            }
        }
    }

    if (endtime == 0) {
        return NextTime::never();
    } else {
        return NextTime::at(endtime);
    }
}

void ButtonManager::set_callback(Button btn, ChangeCallback callback)
{
    if (btn == Button::Count) {
        return;
    }

    m_buttons[btn].m_callback = callback;
}

void ButtonManager::remove_callback(Button btn)
{
    if (btn == Button::Count) {
        return;
    }

    m_buttons[btn].m_callback = nop_callback;
}

void ButtonManager::irq()
{
    uint32_t idr = GPIOA->IDR;
    static BSP::time_t systime;

    // TODO: is this call too expensive for an interrupt handler?
    BSP::SystemTimer::get_time(systime);

    if (!m_instance) {
        return;
    }

    for (auto &btn: m_instance->m_buttons) {
        bool is_pressed = !(idr & (1 << btn.m_gpio_idx));

        // Check if the button state has changed, and timestamp it
        if (is_pressed && (btn.m_state == ButtonState::NOT_PRESSED)) {
            btn.m_state = ButtonState::PRESSED;
            btn.m_state_change_ts = systime;
        } else if (!is_pressed && (btn.m_state == ButtonState::PRESSED)) {
            btn.m_state = ButtonState::NOT_PRESSED;
            btn.m_state_change_ts = systime;
        }

        // Clear the event
        SET(EXTI->PR, 1u << btn.m_gpio_idx);
    }

    if (m_instance != nullptr) {
        m_instance->m_scheduler.add_task(*m_instance, NextTime::asap());
    }
}


extern "C" void EXTI_1_0_IRQHandler() {
    static uint32_t irq_count = 0;
    ButtonManager::irq();
    irq_count++;
}

extern "C" void EXTI_3_2_IRQHandler() {
    static uint32_t irq_count = 0;
    ButtonManager::irq();
    irq_count++;
}

extern "C" void EXTI_15_4_IRQHandler() {
    static uint32_t irq_count = 0;
    ButtonManager::irq();
    irq_count++;
}

}
