/*
 * $Id: menu_2_fn.c 464 2019-08-01 02:15:54Z true $
 * begin 20190612 true
 *
 * led setup menu functions
 */

#include "ui/menu.h"
#include "disp/font.h"

#include "led/rgbled.h"

#include "user_config.h"

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


static uint8_t idata edit_mode = MENU_BTNSTYLE_MENU;
static uint8_t idata edit_idx;


static void menu_2_val_inc(uint8_t *val, uint8_t loop)
{
	if (!loop && *val == 0xff) return;
	(*val)++;
}

static void menu_2_val_dec(uint8_t *val, uint8_t loop)
{
	if (!loop && *val == 0x00) return;
	(*val)--;
}

void menu_2_btn_next()
{
	switch (edit_idx) {
		case 1: menu_2_val_inc(&uconf.favcolor_hue, 1); break;
		case 2: menu_2_val_inc(&uconf.favcolor_sat, 0); break;
		case 3: menu_2_val_inc(&uconf.favcolor_val, 0); break;
		case 4: menu_2_val_inc(&uconf.altcolor_hue, 1); break;
		case 5: menu_2_val_inc(&uconf.altcolor_sat, 0); break;
		case 6: menu_2_val_inc(&uconf.altcolor_val, 0); break;
	}
}

void menu_2_btn_prev()
{
	switch (edit_idx) {
		case 1: menu_2_val_dec(&uconf.favcolor_hue, 1); break;
		case 2: menu_2_val_dec(&uconf.favcolor_sat, 0); break;
		case 3: menu_2_val_dec(&uconf.favcolor_val, 0); break;
		case 4: menu_2_val_dec(&uconf.altcolor_hue, 1); break;
		case 5: menu_2_val_dec(&uconf.altcolor_sat, 0); break;
		case 6: menu_2_val_dec(&uconf.altcolor_val, 0); break;
	}
}

void menu_2_btn_exit()
{
	edit_mode = MENU_BTNSTYLE_MENU;
	menu_btn_use_std();
}

void menu_2_btn_use(uint8_t idx)
{
	btn[0].push_cb = &menu_2_btn_prev;
	btn[0].held_cb = &menu_2_btn_prev;
	btn[0].release_cb = 0;
	btn[0].hold_time = 12;
	btn[0].hold_retrig = (idx == 1 || idx == 4) ? 8 : 14;

	btn[1].push_cb = &menu_2_btn_exit;
	btn[1].held_cb = 0;
	btn[1].release_cb = 0;

	btn[2].push_cb = &menu_2_btn_exit;
	btn[2].held_cb = 0;
	btn[2].release_cb = 0;

	btn[3].push_cb = &menu_2_btn_next;
	btn[3].held_cb = &menu_2_btn_next;
	btn[3].release_cb = 0;
	btn[3].hold_time = 12;
	btn[3].hold_retrig = btn[0].hold_retrig;
}


void menu_2_disp(uint8_t idx)
{
	int8_t w, x;
	uint8_t f;
	char xdata txt[12];

	if (edit_mode == MENU_BTNSTYLE_LED_EDIT) {
		menu_2_btn_use(idx);
	}

	ssd1306fb_set_cursor(10, 7);

	if (idx >= 1 && idx <= 6) {
		f = (idx - 1) / 3;
		ssd1306fb_draw_str(font_table[0].font, (f == 0) ? "Favorite " : "Alternate ", 1);
	}

	// which item selected?
	switch (idx) {
		case 0: {
			ssd1306fb_draw_str(font_table[0].font, "LED Program", 1);
			ssd1306fb_set_cursor(16, 15);
			ssd1306fb_draw_str(font_table[0].font, ledprog[(uconf.ledprog_active & 0x0f)].name, 1);

			goto MENU_2_DRAW_TEXT_DONE;
		}
		case 7: {
			ssd1306fb_draw_str(font_table[0].font, "Speed", 1);
			sprintf(txt, "%Bu0%%", (uint8_t)(13 - (uconf.ledprog_active >> 4)));
			break;
		}
		case 1:
		case 4: {
			ssd1306fb_draw_str(font_table[0].font, "Color Hue", 1);

			// bar position
			f = (idx == 1) ? uconf.favcolor_hue : uconf.altcolor_hue;

			// draw hue markers
			ssd1306fb_draw_vline(36 + 21, 19, 21);
			ssd1306fb_draw_vline(36 + 42, 19, 21);

			goto MENU_2_EDIT_COLOR_LINE;
		}
		case 2:
		case 5: {
			ssd1306fb_draw_str(font_table[0].font, "Color Sat.", 1);

			// bar position
			f = (idx == 2) ? uconf.favcolor_sat : uconf.altcolor_sat;

			goto MENU_2_PCT_COLOR_TEXT;
		}
		case 3:
		case 6: {
			ssd1306fb_draw_str(font_table[0].font, "Color Value", 1);

			// bar position
			f = (idx == 3) ? uconf.favcolor_val : uconf.altcolor_val;

MENU_2_PCT_COLOR_TEXT:
			// percentage text
			sprintf(txt, "%3u%%", ((uint16_t)f * 100) / 255);
			ssd1306fb_set_cursor(50, 24);
			ssd1306fb_draw_str(font_table[0].font, txt, 1);

MENU_2_EDIT_COLOR_LINE:
			// draw value line
			f >>= 2;
			f += 36;
			ssd1306fb_draw_hline(36, 99, 20);

			// draw end markers
			ssd1306fb_draw_vline(36,      19, 21);
			ssd1306fb_draw_vline(36 + 63, 19, 21);

			// draw position triangle
			ssd1306fb_draw_hline(f - 2, f + 2, 16);
			ssd1306fb_draw_line(f + 2, 16, f, 19);
			ssd1306fb_draw_line(f - 2, 16, f, 19);

			if (edit_mode == MENU_BTNSTYLE_LED_EDIT) {
				if (idx == 1 || idx == 4) {
					// RGBR indicators
					ssd1306fb_set_cursor(33, 21);
					ssd1306fb_draw_str(font_table[0].font, "R", 0);

					oled.cursor_x = 33 + 21;
					ssd1306fb_draw_str(font_table[0].font, "G", 0);

					oled.cursor_x = 33 + 42;
					ssd1306fb_draw_str(font_table[0].font, "B", 0);

					oled.cursor_x = 33 + 63;
					ssd1306fb_draw_str(font_table[0].font, "R", 0);
				} else {
					// 0 - 100% indicators
					ssd1306fb_set_cursor(28 + 7, 21);
					ssd1306fb_draw_str(font_table[0].font, "0%", 0);

					oled.cursor_x = 28 + 63 - 14;
					ssd1306fb_draw_str(font_table[0].font, "100%", 0);
				}
			}

			goto MENU_2_DRAW_TEXT_DONE;
		}
	}

	w = ssd1306fb_get_str_width(font_table[1].font, txt, strlen(txt), 1);
	x = 68 - (w >> 1);
	ssd1306fb_set_cursor(x, 18);
	ssd1306fb_draw_str(font_table[1].font, txt, 1);

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

MENU_2_DRAW_TEXT_DONE:
	// draw extras
	menu_draw_tabs(idx);

	menu_draw_buttons(edit_mode, 0x0f);
}

void menu_2_enter(uint8_t idx)
{
	uint8_t a;

	edit_idx = idx;

	switch (idx) {
		case 0: {
			a = (sizeof(ledprog) / sizeof(ledprog[0]));
			uconf.ledprog_active++;
			if ((uconf.ledprog_active & 0x0f) >= a) {
				uconf.ledprog_active &= 0xf0;
			}
			break;
		}
		case 7: {
			a = uconf.ledprog_active >> 4;
			a++;
			if (a > 12) {
				a = 1;
			}
			uconf.ledprog_active &= 0x0f;
			uconf.ledprog_active |= (a << 4);
			break;
		}
		default: {
			if (edit_mode == MENU_BTNSTYLE_LED_EDIT) {
				edit_mode = MENU_BTNSTYLE_MENU;
				menu_btn_use_std();
			} else {
				edit_mode = MENU_BTNSTYLE_LED_EDIT;
			}
		}
	}
}
