
#include <stdbool.h>
#include <stdint.h>
#include "boards.h"
#include "bsp.h"
#include "bsp_nfc.h"
#include "app_timer.h"
#include "nordic_common.h"
#include "app_error.h"
#include "nrf_drv_clock.h"
#include "nrf_pwr_mgmt.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#if NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
#include "app_scheduler.h"
#define APP_SCHED_MAX_EVENT_SIZE    0   /**< Maximum size of scheduler events. */
#define APP_SCHED_QUEUE_SIZE        4   /**< Maximum number of events in the scheduler queue. */
#endif // NRF_PWR_MGMT_CONFIG_USE_SCHEDULER

#define BTN_ID                20



/**@brief Handler for shutdown preparation.
 */
bool shutdown_handler(nrf_pwr_mgmt_evt_t event)
{

    uint32_t err_code;

    switch (event)
    {
        case NRF_PWR_MGMT_EVT_PREPARE_WAKEUP:
            NRF_LOG_INFO("NRF_PWR_MGMT_EVT_PREPARE_WAKEUP");
            err_code = bsp_buttons_disable();
            // Suppress NRF_ERROR_NOT_SUPPORTED return code.
            UNUSED_VARIABLE(err_code);

            err_code = bsp_wakeup_button_enable(BTN_ID);
            // Suppress NRF_ERROR_NOT_SUPPORTED return code.
            UNUSED_VARIABLE(err_code);

            break;
    }

    err_code = app_timer_stop_all();
    APP_ERROR_CHECK(err_code);

    return true;
}





/**@brief Register application shutdown handler with priority 0. */
NRF_PWR_MGMT_HANDLER_REGISTER(shutdown_handler, 0);






/**@brief Function for handling BSP events.
 */
static void bsp_evt_handler(bsp_event_t evt)
{
#if NRF_PWR_MGMT_CONFIG_STANDBY_TIMEOUT_ENABLED
    nrf_pwr_mgmt_feed();
    NRF_LOG_INFO("Power management fed");
#endif // NRF_PWR_MGMT_CONFIG_STANDBY_TIMEOUT_ENABLED

    switch (evt)
    {
        
        case BSP_EVENT_DEFAULT:
            
            break;

        case BSP_EVENT_SYSOFF:

                // Application will prepare the wakeup mechanism: NRF_PWR_MGMT_EVT_PREPARE_WAKEUP
                /**@brief Function for shutting down the system.
                 * @param[in] shutdown_type     Type of operation.
                 * @details All callbacks will be executed prior to shutdown.
                 */
                nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
                // NRF_LOG_INFO("2) System OFF is entered\r\n");
                // NRF_POWER->SYSTEMOFF = 1;
            
            break;


        default:
            return; // no implementation needed
    }
}








/**@brief Function for initializing low-frequency clock.
 */
static void lfclk_config(void)
{
    ret_code_t err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_clock_lfclk_request(NULL);
}






/**@brief Function for initializing the BSP module.
 */
static void bsp_configuration()
{
    NRF_LOG_INFO("3.0")
    uint32_t err_code;

    err_code = bsp_init(BSP_INIT_BUTTONS, bsp_evt_handler);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("3.1");
    err_code = bsp_event_to_button_action_assign(BTN_ID,
                                                 BSP_BUTTON_ACTION_LONG_PUSH,
                                                 BSP_EVENT_DEFAULT);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("3.2")

    err_code = bsp_event_to_button_action_assign(BTN_ID,
                                                 BSP_BUTTON_ACTION_RELEASE,
                                                 BSP_EVENT_SYSOFF);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("3.3")
}






/**
 * @brief Function for application main entry.
 */
int main(void)
{
    
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("Power Management example");

    lfclk_config();
    NRF_LOG_INFO("1");

    uint32_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("2");

#if NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
    APP_SCHED_INIT(APP_SCHED_MAX_EVENT_SIZE, APP_SCHED_QUEUE_SIZE);
#endif // NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
    NRF_LOG_INFO("3");
    bsp_configuration();
    NRF_LOG_INFO("4");

    ret_code_t ret_code = nrf_pwr_mgmt_init();
    APP_ERROR_CHECK(ret_code);
    NRF_LOG_INFO("5");

    while (true)
    {
#if NRF_PWR_MGMT_CONFIG_USE_SCHEDULER
        app_sched_execute();
#endif // NRF_PWR_MGMT_CONFIG_USE_SCHEDULER

        if (NRF_LOG_PROCESS() == false)
        {
            nrf_pwr_mgmt_run();
        }
    }
}



