#include "dolphin_i.h"

#include <furi_hal.h>
#include <storage/storage.h>
#include <momentum/momentum.h>

#define TAG "Dolphin"

#define DOLPHIN_LOCK_EVENT_FLAG (0x1)
#define EVENT_QUEUE_SIZE        (8)

#define SECONDS_IN_TICKS(x)    ((x) * 1000UL)
#define MINUTES_IN_TICKS(x)    (SECONDS_IN_TICKS(x) * 60UL)
#define HOURS_IN_TICKS(x)      (MINUTES_IN_TICKS(x) * 60UL)
#define DATE_IN_TICKS(h, m, s) (HOURS_IN_TICKS(h) + MINUTES_IN_TICKS(m) + SECONDS_IN_TICKS(s))

#define FLUSH_TIMEOUT_TICKS (SECONDS_IN_TICKS(30UL))

#ifndef DOLPHIN_DEBUG
#define BUTTHURT_INCREASE_PERIOD_TICKS   (SECONDS_IN_TICKS(momentum_settings.butthurt_timer))
#define CLEAR_LIMITS_PERIOD_TICKS        (HOURS_IN_TICKS(24UL))
#define CLEAR_LIMITS_UPDATE_PERIOD_TICKS (HOURS_IN_TICKS(1UL))
#else
#define BUTTHURT_INCREASE_PERIOD_TICKS   (SECONDS_IN_TICKS(30UL))
#define CLEAR_LIMITS_PERIOD_TICKS        (MINUTES_IN_TICKS(1))
#define CLEAR_LIMITS_UPDATE_PERIOD_TICKS (SECONDS_IN_TICKS(5UL))
#endif

#define CLEAR_LIMITS_UPDATE_THRESHOLD_TICKS (MINUTES_IN_TICKS(5UL))

#define CLEAR_LIMITS_TIME_HOURS (5UL)
#define CLEAR_LIMITS_TIME_TICKS (HOURS_IN_TICKS(CLEAR_LIMITS_TIME_HOURS))

static void dolphin_event_send_async(Dolphin* dolphin, DolphinEvent* event);
static void dolphin_event_send_wait(Dolphin* dolphin, DolphinEvent* event);

// Public API

void dolphin_deed(DolphinDeed deed) {
    Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);

    DolphinEvent event;
    event.type = DolphinEventTypeDeed;
    event.deed = deed;

    dolphin_event_send_async(dolphin, &event);

    furi_record_close(RECORD_DOLPHIN);
}

void dolphin_get_settings(Dolphin* dolphin, DolphinSettings* settings) {
    furi_check(dolphin);
    furi_check(settings);

    DolphinEvent event;
    event.type = DolphinEventTypeSettingsGet;
    event.settings = settings;
    dolphin_event_send_wait(dolphin, &event);
}

void dolphin_set_settings(Dolphin* dolphin, DolphinSettings* settings) {
    furi_check(dolphin);
    furi_check(settings);

    DolphinEvent event;
    event.type = DolphinEventTypeSettingsSet;
    event.settings = settings;
    dolphin_event_send_wait(dolphin, &event);
}

DolphinStats dolphin_stats(Dolphin* dolphin) {
    furi_check(dolphin);

    DolphinStats stats;
    DolphinEvent event;

    event.type = DolphinEventTypeStats;
    event.stats = &stats;

    dolphin_event_send_wait(dolphin, &event);

    return stats;
}

void dolphin_flush(Dolphin* dolphin) {
    furi_check(dolphin);

    DolphinEvent event;
    event.type = DolphinEventTypeFlush;

    dolphin_event_send_wait(dolphin, &event);
}

void dolphin_upgrade_level(Dolphin* dolphin) {
    furi_check(dolphin);

    DolphinEvent event;
    event.type = DolphinEventTypeLevel;

    dolphin_event_send_async(dolphin, &event);
}

FuriPubSub* dolphin_get_pubsub(Dolphin* dolphin) {
    furi_check(dolphin);
    return dolphin->pubsub;
}

void dolphin_reload_state(Dolphin* dolphin) {
    furi_check(dolphin);

    DolphinEvent event;
    event.type = DolphinEventTypeReloadState;

    dolphin_event_send_wait(dolphin, &event);
}

// Private functions

static void dolphin_butthurt_timer_callback(void* context) {
    Dolphin* dolphin = context;
    furi_assert(dolphin);

    FURI_LOG_I(TAG, "Increase butthurt");
    dolphin_state_butthurted(dolphin->state);
    dolphin_state_save(dolphin->state);
}

static void dolphin_flush_timer_callback(void* context) {
    Dolphin* dolphin = context;
    furi_assert(dolphin);

    FURI_LOG_I(TAG, "Flush stats");
    dolphin_state_save(dolphin->state);
}

static void dolphin_clear_limits_timer_callback(void* context) {
    Dolphin* dolphin = context;
    furi_assert(dolphin);

    FURI_LOG_I(TAG, "Clear limits");
    dolphin_state_clear_limits(dolphin->state);
    dolphin_state_save(dolphin->state);
}

static Dolphin* dolphin_alloc(void) {
    Dolphin* dolphin = malloc(sizeof(Dolphin));

    dolphin->state = dolphin_state_alloc();
    dolphin->pubsub = furi_pubsub_alloc();
    dolphin->event_queue = furi_message_queue_alloc(EVENT_QUEUE_SIZE, sizeof(DolphinEvent));
    dolphin->event_loop = furi_event_loop_alloc();

    dolphin->butthurt_timer = furi_event_loop_timer_alloc(
        dolphin->event_loop,
        dolphin_butthurt_timer_callback,
        FuriEventLoopTimerTypePeriodic,
        dolphin);

    dolphin->flush_timer = furi_event_loop_timer_alloc(
        dolphin->event_loop, dolphin_flush_timer_callback, FuriEventLoopTimerTypeOnce, dolphin);

    dolphin->clear_limits_timer = furi_event_loop_timer_alloc(
        dolphin->event_loop,
        dolphin_clear_limits_timer_callback,
        FuriEventLoopTimerTypePeriodic,
        dolphin);

    return dolphin;
}

static void dolphin_event_send_async(Dolphin* dolphin, DolphinEvent* event) {
    furi_assert(dolphin);
    furi_assert(event);
    event->flag = NULL;
    furi_check(
        furi_message_queue_put(dolphin->event_queue, event, FuriWaitForever) == FuriStatusOk);
}

static void dolphin_event_send_wait(Dolphin* dolphin, DolphinEvent* event) {
    furi_assert(dolphin);
    furi_assert(event);

    event->flag = furi_event_flag_alloc();
    furi_check(
        furi_message_queue_put(dolphin->event_queue, event, FuriWaitForever) == FuriStatusOk);
    furi_check(
        furi_event_flag_wait(
            event->flag, DOLPHIN_LOCK_EVENT_FLAG, FuriFlagWaitAny, FuriWaitForever) ==
        DOLPHIN_LOCK_EVENT_FLAG);
    furi_event_flag_free(event->flag);
}

static void dolphin_event_release(DolphinEvent* event) {
    if(event->flag) {
        furi_event_flag_set(event->flag, DOLPHIN_LOCK_EVENT_FLAG);
    }
}

static void dolphin_update_clear_limits_timer_period(void* context) {
    furi_assert(context);
    Dolphin* dolphin = context;

    uint32_t time_to_clear_limits =
        furi_event_loop_timer_get_remaining_time(dolphin->clear_limits_timer);

    if(time_to_clear_limits > CLEAR_LIMITS_UPDATE_THRESHOLD_TICKS) {
        DateTime date;
        furi_hal_rtc_get_datetime(&date);

        const uint32_t now_time_ticks = DATE_IN_TICKS(date.hour, date.minute, date.second);

        if(date.hour < CLEAR_LIMITS_TIME_HOURS) {
            time_to_clear_limits = CLEAR_LIMITS_TIME_TICKS - now_time_ticks;
        } else {
            time_to_clear_limits =
                CLEAR_LIMITS_PERIOD_TICKS + CLEAR_LIMITS_TIME_TICKS - now_time_ticks;
        }

        furi_event_loop_timer_start(dolphin->clear_limits_timer, time_to_clear_limits);
    }

    FURI_LOG_D(TAG, "Daily limits reset in %lu ms", time_to_clear_limits);
}

static void dolphin_reset_butthurt_timer(Dolphin* dolphin) {
    uint32_t period_ticks = BUTTHURT_INCREASE_PERIOD_TICKS;
    if(period_ticks > 0) {
        furi_event_loop_timer_start(dolphin->butthurt_timer, period_ticks);
    } else {
        furi_event_loop_timer_stop(dolphin->butthurt_timer);
    }
}

static void dolphin_process_event(FuriEventLoopObject* object, void* context) {
    UNUSED(object);

    Dolphin* dolphin = context;
    DolphinEvent event;

    FuriStatus status = furi_message_queue_get(dolphin->event_queue, &event, 0);
    furi_check(status == FuriStatusOk);

    if(event.type == DolphinEventTypeDeed) {
        dolphin_state_on_deed(dolphin->state, event.deed);

        DolphinPubsubEvent event = DolphinPubsubEventUpdate;
        furi_pubsub_publish(dolphin->pubsub, &event);
        dolphin_reset_butthurt_timer(dolphin);
        furi_event_loop_timer_start(dolphin->flush_timer, FLUSH_TIMEOUT_TICKS);

    } else if(event.type == DolphinEventTypeStats) {
        event.stats->icounter = dolphin->state->data.icounter;
        event.stats->butthurt = (dolphin->state->data.flags & DolphinFlagHappyMode) ?
                                    0 :
                                    dolphin->state->data.butthurt;
        event.stats->timestamp = dolphin->state->data.timestamp;
        event.stats->level = dolphin_get_level(dolphin->state->data.icounter);
        event.stats->level_up_is_pending =
            !dolphin_state_xp_to_levelup(dolphin->state->data.icounter);

    } else if(event.type == DolphinEventTypeFlush) {
        dolphin_flush_timer_callback(dolphin);
        furi_event_loop_timer_start(dolphin->flush_timer, FLUSH_TIMEOUT_TICKS);

    } else if(event.type == DolphinEventTypeLevel) {
        dolphin_state_increase_level(dolphin->state);
        furi_event_loop_timer_start(dolphin->flush_timer, FLUSH_TIMEOUT_TICKS);

    } else if(event.type == DolphinEventTypeReloadState) {
        dolphin_state_load(dolphin->state);
        dolphin_reset_butthurt_timer(dolphin);

    } else if(event.type == DolphinEventTypeSettingsGet) {
        event.settings->happy_mode = dolphin->state->data.flags & DolphinFlagHappyMode;

    } else if(event.type == DolphinEventTypeSettingsSet) {
        dolphin->state->data.flags &= ~DolphinFlagHappyMode;
        if(event.settings->happy_mode) dolphin->state->data.flags |= DolphinFlagHappyMode;
        dolphin->state->dirty = true;
        dolphin_state_save(dolphin->state);

    } else {
        furi_crash();
    }

    dolphin_event_release(&event);
}

static void dolphin_storage_callback(const void* message, void* context) {
    furi_assert(context);
    Dolphin* dolphin = context;
    const StorageEvent* event = message;

    if(event->type == StorageEventTypeCardMount) {
        DolphinEvent event = {
            .type = DolphinEventTypeReloadState,
        };

        dolphin_event_send_async(dolphin, &event);
    }
}

static void dolphin_init_state(Dolphin* dolphin) {
    Storage* storage = furi_record_open(RECORD_STORAGE);
    furi_pubsub_subscribe(storage_get_pubsub(storage), dolphin_storage_callback, dolphin);

    if(storage_sd_status(storage) != FSE_OK) {
        FURI_LOG_D(TAG, "SD Card not ready, skipping state");
        return;
    }

    dolphin_state_load(dolphin->state);
}

// Application thread

int32_t dolphin_srv(void* p) {
    UNUSED(p);

    if(!furi_hal_is_normal_boot()) {
        FURI_LOG_W(TAG, "Skipping start in special boot mode");

        furi_thread_suspend(furi_thread_get_current_id());
        return 0;
    }

    Dolphin* dolphin = dolphin_alloc();
    furi_record_create(RECORD_DOLPHIN, dolphin);

    dolphin_init_state(dolphin);

    furi_event_loop_subscribe_message_queue(
        dolphin->event_loop,
        dolphin->event_queue,
        FuriEventLoopEventIn,
        dolphin_process_event,
        dolphin);

    dolphin_reset_butthurt_timer(dolphin);
    furi_event_loop_timer_start(dolphin->clear_limits_timer, CLEAR_LIMITS_PERIOD_TICKS);

    furi_event_loop_tick_set(
        dolphin->event_loop,
        CLEAR_LIMITS_UPDATE_PERIOD_TICKS,
        dolphin_update_clear_limits_timer_period,
        dolphin);

    furi_event_loop_pend_callback(
        dolphin->event_loop, dolphin_update_clear_limits_timer_period, dolphin);

    furi_event_loop_run(dolphin->event_loop);

    return 0;
}
