/*
 * Copyright (C) 2023 Daniele Lacamera <root@danielinux.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include <stddef.h>
#include "ui.h"
#include "button.h"
#include <stdio.h>
#include "ksp-serial.h"


const char logging_name[] = "Logging";
static void logging_draw(void);
static void logging_run(uint32_t ev, void *arg);
static void logging_init(void)
{

}

struct screen logging_screen = {
    .draw = logging_draw,
    .name = logging_name
};

struct task logging_task = {
    .init = logging_init,
    .run = logging_run,
    .events = EV_BUTTON | EV_HEARTBEAT | EV_VESSELDATA,
    .screen = &logging_screen,
    .name =  logging_name
};


static int button = -1;
static void logging_run(uint32_t event, void *arg)
{
    uint32_t potl;
    struct sample *s;
    int ts;
    if (screen_get_focus() != &logging_screen)
        return;
    if (ui_menu_is_on()) {
        if (event & EV_BUTTON)
            ui_redraw(EV_BUTTON);
        return;
    }
    ts = input_detect_touch();
    if (ts == TS_TOUCH_NONE) {
    } else {
        event |= EV_BUTTON;
        button = 12;
    }
    if (event & EV_BUTTON) {
        if (button < 0)
            button = ui_process_button_pressed();
        switch(button) {
            case BUTTON_DPADU:
                break;
            case BUTTON_DPADD:
                break;
            case BUTTON_DPADL:
                break;
            case BUTTON_DPADR:
                break;
            case 7:
                ui_menu(1);
                ui_redraw(EV_NONE);
                break;
            case 4:
            case 12:
                break;
            case 5:
                break;

            case 8:
                break;
            case 9:
                break;
                
            case 10:
                break;
            case 11:
                break;
        }
        clear_event(EV_BUTTON);
    } else if (event & EV_VESSELDATA) {
        char mtime[40];
        ui_fill_area(0, BLACK, 40, 234, 200, 244);
        snprintf(mtime, 40, "MTIME: %u", cur_vdata->MissionTime);
        ui_text_at(0, CYAN, 40, 234, mtime);
        clear_event(EV_VESSELDATA);
    }
    
    pot_read(&potl, NULL);
    ui_fill_area(0, BLACK, 40, 40, 130, 60);
    ui_text_at(0, GREEN, 40, 40, "Roll:");
    ui_text_at(0, GREEN, 40, 50, "None");
    
    ui_fill_area(0, BLACK, 105, 40, 135, 60);
    ui_text_at(0, YELLOW, 105, 40, "Yaw:");
    ui_text_at(0, YELLOW, 105, 50, "None");
    
    ui_fill_area(0, BLACK, 40, 214, 80, 234);
    ui_text_at(0, GREEN, 40, 214, "Pitch:");
    ui_text_at(0, GREEN, 40, 224, "None");
    
    ui_fill_area(0, BLACK, 85, 214, 120, 234);
    ui_text_at(0, YELLOW, 85, 214, "Pow:");
    ui_text_at(0, YELLOW, 85, 224, ui_printn((potl * 100) / 4096));

}

static void logging_draw(void)
{
    int i;
    char btn_str[4] = "";
    /* background to foreground */
    ui_fill_area(0, DARKRED, 0, 0, xres, yres);
    /* Logo & appname */
    ui_text_at (0, GREY, 35, 4, "Debug Info");
    ui_text_at (0, YELLOW, 160, 40, "Button pressed:");
    snprintf(btn_str, 4, "%d", button);
    ui_text_at (0, YELLOW, 160, 80, btn_str);
    //image_at(0, logo, 5, 4, logox, logoy);
}

void logging_setup(void)
{
    register_task(&logging_task);
    register_screen(&logging_task, &logging_screen);
}
