/*
 * keypad_screen.c
 *
 *  Created on: Aug 11, 2023
 *      Author: leo
 */

#include "keypad_screen.h"

#include <src/font/lv_symbol_def.h>
#include <stdio.h>

#include "lvgl.h"

static keypad_data* kp_data;
static uint32_t value;
lv_obj_t* value_readout;

static const char * btnm_map[] = {"7", "8", "9", "\n", "4", "5", "6", "\n", "1", "2", "3", "\n", "X", "0", LV_SYMBOL_BACKSPACE, NULL};

static void back_button_handler(lv_event_t * e){
  lv_event_code_t code = lv_event_get_code(e);

	if(code == LV_EVENT_CLICKED) {
		lv_obj_t* curr_scr = lv_scr_act();
		lv_scr_load(kp_data->parent);
		lv_obj_del(curr_scr);
	}
}

static void ok_button_handler(lv_event_t * e){
  lv_event_code_t code = lv_event_get_code(e);

	if(code == LV_EVENT_CLICKED) {
    kp_data->value = value;
		lv_obj_t* curr_scr = lv_scr_act();
		lv_scr_load(kp_data->parent);
		lv_obj_del(curr_scr);
    kp_data->cb(kp_data);
	}
}

static void keypress_handler(lv_event_t * e){

  lv_event_code_t code = lv_event_get_code(e);
  lv_obj_t * obj = lv_event_get_target(e);
  if(code == LV_EVENT_VALUE_CHANGED) {
    uint32_t id = lv_btnmatrix_get_selected_btn(obj);
    const char * txt = lv_btnmatrix_get_btn_text(obj, id);

    if(txt[0] >= 48 && txt[0] <= 57){ // number
      uint8_t digit = txt[0] - 48;
      value *= 10;
      value += digit;
    }
    else if(txt[0] == 'X') value = 0;
    else value /= 10;

    lv_label_set_text_fmt(value_readout, "%02d.%03d", value/1000, value%1000);
  }
}

void Keypad_screen_launch(keypad_data* data){
  kp_data = data;
  value = 0;
  
  // main screen
  lv_obj_t* scr = lv_obj_create(NULL);

	// create the top menu on the top 20%
	lv_obj_t* top_menu = lv_obj_create(scr);
	lv_obj_set_align(top_menu, LV_ALIGN_TOP_LEFT);
	lv_obj_set_size(top_menu, lv_pct(100), lv_pct(20));
	lv_obj_set_style_pad_all(top_menu, 5, 0);

	// add a back button
	lv_obj_t* back_button = lv_btn_create(top_menu);
	lv_obj_set_size(back_button, lv_pct(10), lv_pct(100));
	lv_obj_set_align(back_button, LV_ALIGN_TOP_LEFT);
	lv_obj_add_event_cb(back_button, back_button_handler, LV_EVENT_ALL, NULL);

	// add the back label
	lv_obj_t* back_label = lv_label_create(back_button);
	lv_label_set_text(back_label, LV_SYMBOL_LEFT);
	lv_obj_center(back_label);

  // add a readout of the set value
  value_readout = lv_label_create(top_menu);
  lv_label_set_text_fmt(value_readout, "%02d.%03d", value/1000, value%1000);
  lv_obj_center(value_readout);

	// add a validation button
	lv_obj_t* ok_button = lv_btn_create(top_menu);
	lv_obj_set_size(ok_button, lv_pct(10), lv_pct(100));
	lv_obj_set_align(ok_button, LV_ALIGN_TOP_RIGHT);
	lv_obj_add_event_cb(ok_button, ok_button_handler, LV_EVENT_ALL, NULL);

	// add the ok label
	lv_obj_t* ok_label = lv_label_create(ok_button);
	lv_label_set_text(ok_label, LV_SYMBOL_OK);
	lv_obj_center(ok_label);

  lv_obj_t * btnm1 = lv_btnmatrix_create(scr);
  lv_btnmatrix_set_map(btnm1, btnm_map);
	lv_obj_align_to(btnm1, top_menu, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
	lv_obj_set_size(btnm1, lv_pct(100), lv_pct(80));
  lv_obj_add_event_cb(btnm1, keypress_handler, LV_EVENT_ALL, NULL);
  
  lv_scr_load(scr);
}
