/*
 * task_buttons.c
 *
 *  Created on: Mar 14, 2025
 *      Author: gca76
 */

#include "rgb.h"
#include "buttons.h"

#include "tim.h"
#include "pwm.h"
#include "task_display.h"
#include "globals.h"

uint8_t toggleSerialDebugging = 1;

// Double click
static uint32_t last_press_time = 0;
static bool waiting_for_second_tap = false;
static bool test_mode_active_check = false;
static uint32_t test_press_speed = 700;

// Joystick press
const uint32_t debounce_delay = 50;  // ms
static uint32_t click_hold_start = 0;
static bool hold_toggle_done = false;
static uint32_t time_turn_on_goal_state = 1000;
static uint32_t click_press_start = 0;
static bool click_is_pressed = false;
static bool click_ready = false;
static bool goalstate_ready = false;

void button_task_execute(void)
{
	if (!GOALSTATE){ // turn off button inputs when in goal state
		  //The top LED (DS3) SW1 press increases the duty cycle of DS3 by increments of 25% and wraps back to 0% when it exceeds 100%.
		  if (buttons_checkButton(UP) == PUSHED)
		  {
				STEPCOUNT += 80;
		  }

	//  The bottom LED (DS2) illuminates white (all RGB on) while SW2 is pressed, otherwise
	//	  the LED is off.
		  setSerialDebugging(toggleSerialDebugging);
		  if (buttons_checkButton(DOWN) == PUSHED)
		  {
			  rgb_led_toggle(RGB_DOWN);
			  toggleSerialDebugging = !toggleSerialDebugging;
			  test_mode_active_check = true;
		  }

	//	  The right LED (DS4) illuminates white (all RGB on) while SW3 is pressed, otherwise the
	//	  LED is off.
		  if (buttons_checkButton(RIGHT) == PUSHED)
		  {
			  rgb_led_toggle(RGB_RIGHT);
		  }

	//	  The left LED (DS1) illuminates white (all RGB on) while SW4 is pressed, otherwise the
	//	  LED is off.
		  if (buttons_checkButton(LEFT) == PUSHED)
		  {
			  rgb_led_toggle(RGB_LEFT);
		  }

		//  Enter or leave TEST MODE
			if (test_mode_active_check) {
				uint32_t now = HAL_GetTick();

				if (!waiting_for_second_tap) {
					// First tap
					waiting_for_second_tap = true;
					last_press_time = now;
				} else {
					// Second tap detected
					if (now - last_press_time <= test_press_speed) {
						test_mode_active = !test_mode_active;  // Toggle test mode
						waiting_for_second_tap = false;        // Reset state
					} else {
						// Too slow — treat as a new first tap
						last_press_time = now;
					}
				}
				test_mode_active_check = false;
			}

			// Timeout: reset if too slow to second-tap
			if (waiting_for_second_tap && HAL_GetTick() - last_press_time > test_press_speed) {
				waiting_for_second_tap = false;
			}
	}







		// Joystick push
		GPIO_PinState click_state = HAL_GPIO_ReadPin(JOYSTICK_CLICK_GPIO_Port, JOYSTICK_CLICK_Pin);
		if (GOAL_PROGRESS){
			if (!GOALSTATE) {
				// Wait for joystick release before allowing long-press entry
				if (click_state == GPIO_PIN_RESET) {
					click_ready = true;
					click_hold_start = 0;
					hold_toggle_done = false;
				}

				if (click_state == GPIO_PIN_SET && click_ready) {
					if (click_hold_start == 0) {
						click_hold_start = HAL_GetTick();
					} else if (!hold_toggle_done && HAL_GetTick() - click_hold_start >= time_turn_on_goal_state) {
						// Enter GOALSTATE
//						rgb_led_toggle(RGB_DOWN);
						hold_toggle_done = true;
						GOALSTATE = true;
						goalstate_ready = false;  // ensure reset before short/long press
					}
				}

			} else {
				// GOALSTATE active
				// Require joystick release before action
				if (click_state == GPIO_PIN_RESET && !goalstate_ready) {
					goalstate_ready = true;
					click_is_pressed = false;
				}

				if (click_state == GPIO_PIN_SET && goalstate_ready && !click_is_pressed) {
					click_press_start = HAL_GetTick();
					click_is_pressed = true;
				}

				if (click_state == GPIO_PIN_RESET && goalstate_ready && click_is_pressed) {
					uint32_t duration = HAL_GetTick() - click_press_start;

					// Exit GOALSTATE and turn off RGB_DOWN
					GOALSTATE = false;

					if (duration >= 1000) {
						GOAL = SETGOAL;
					} else {
						SETGOAL = GOAL;
					}

					// Reset state
					click_is_pressed = false;
					goalstate_ready = false;
				}
			}
		}











}
