/*
 * menu_entry_5.c
 * begin 20190613 true
 *
 * settings menu functions
 */

#include "ui/menu.h"

#include "render/font.h"

#include "hw/lightsense.h"

#include "user_config.h"

#include <stdio.h>
#include <string.h>


const uint16_t sleep_times[] = {
	0,
	300,
	600,
	900,
	1200,
	1800,
};


__HIGH_CODE
void menu_5_disp(uint8_t idx)
{
	int8_t w, x;
	char txt[12];

	ssd1306fb_set_cursor(10, 7);

	uconf.flags &= ~UCONF_FLAGS_LEDS_DISABLE;

	// which item selected?
	switch (idx) {
		case 0: {
			ssd1306fb_draw_str(font_table[0].font, "Enable LEDs", 1);
			strcpy(txt, (uconf.flags & UCONF_FLAGS_LEDS_ENABLE) ? "On" : "Off");
			break;
		}
		case 1: {
			ssd1306fb_draw_str(font_table[0].font, "No Movement Sleep Time", 0);
			if (uconf.sleep_timeout) {
				sprintf(txt, "%dmin", (uconf.sleep_timeout / 60));
			} else {
				strcpy(txt, "Off");
			}
			break;
		}
		case 2: {
			ssd1306fb_draw_str(font_table[0].font, "No Movement Threshold", 0);
			strcpy(txt, "Normal");
			break;
		}
		case 3: {
			ssd1306fb_draw_str(font_table[0].font, "Wakeup Threshold", 1);
			strcpy(txt, "Normal");
			break;
		}
		case 4: {
			// constantly save value at this screen
			uconf.lsens_dark_thresh = lsens_get_dark_threshold();
			// ensure LEDs are disabled when calibrating
			uconf.flags |= UCONF_FLAGS_LEDS_DISABLE;
			
			ssd1306fb_draw_str(font_table[0].font, "Recal Lightsense DARK RM!", 0);
			sprintf(txt, "%d", uconf.lsens_dark_thresh);
			break;
		}
		case 5: {
			ssd1306fb_draw_str(font_table[0].font, "Debug: Show CPU%", 1);
			strcpy(txt, (uconf.flags & UCONF_FLAGS_SHOW_CPU_USAGE) ? "Yes" : "No");
			break;
		}
		case 6: {
			ssd1306fb_draw_str(font_table[0].font, "Debug: Show Accel", 1);
			strcpy(txt, (uconf.flags & UCONF_FLAGS_SHOW_ACCEL_ANGLE) ? "Yes" : "No");
			break;
		}
	}

	w = ssd1306fb_get_str_width(font_DejaVu_Sans_Mono_Bold_11, txt, strlen(txt), 1);
	x = 68 - (w >> 1);
	ssd1306fb_set_cursor(x, 18);
	ssd1306fb_draw_str(font_DejaVu_Sans_Mono_Bold_11, txt, 1);

	// draw box
	ssd1306fb_draw_rect(x - 2, 18, x + w + 2, 31);

	// draw extras
	menu_draw_tabs(idx);

	menu_draw_buttons(MENU_BTNSTYLE_MENU, 0x0f);
}

void menu_5_enter(uint8_t idx)
{
	int i, j;
	
	switch (idx) {
		case 0: {
			if (uconf.flags & UCONF_FLAGS_LEDS_ENABLE) {
				uconf.flags &= ~UCONF_FLAGS_LEDS_ENABLE;
			} else {
				uconf.flags |=  UCONF_FLAGS_LEDS_ENABLE;
			}
			break;
		}
		case 1: {
			j = sizeof(sleep_times) / sizeof(sleep_times[0]);
			for (i = 0; i < j; i++) {
				if (uconf.sleep_timeout == sleep_times[i]) {
					i++;
					i %= j;
					uconf.sleep_timeout = sleep_times[i];
					j = 0xff;
					break;
				}
			}

			// couldn't find a match, so set default
			if (j != 0xff) {
				uconf.sleep_timeout = sleep_times[4];
			}
			break;
		}
		case 4: {
			// reset sensor threshold to recal value
			lsens_set_dark_threshold(0xffff);
			break;
		}
		case 5: {
			if (uconf.flags & UCONF_FLAGS_SHOW_CPU_USAGE) {
				uconf.flags &= ~UCONF_FLAGS_SHOW_CPU_USAGE;
			} else {
				uconf.flags |=  UCONF_FLAGS_SHOW_CPU_USAGE;
			}
			break;
		}
		case 6: {
			if (uconf.flags & UCONF_FLAGS_SHOW_ACCEL_ANGLE) {
				uconf.flags &= ~UCONF_FLAGS_SHOW_ACCEL_ANGLE;
			} else {
				uconf.flags |=  UCONF_FLAGS_SHOW_ACCEL_ANGLE;
			}
			break;
		}
	}
}
