/*
 * home_screen.c
 *
 *  Created on: Aug 7, 2023
 *      Author: leo
 */

#include <src/core/lv_disp.h>
#include <stdint.h>
#include <stdio.h>

#include "PSE_unit_edit_screen.h"

#include "PSE_unit.h"
#include "keypad_screen.h"
#include "pse_stepper_planer.h"
#include "home_screen.h"

#include "lvgl.h"

static lv_obj_t* this_screen;
static pse_unit* c_pse_units;
static pse_unit* c_pse_unit;
static uint8_t c_pse_units_num;

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

	if(code == LV_EVENT_CLICKED) {
		save_units(c_pse_units, c_pse_units_num);

		// update stepper pwm generation
		pse_stepper_planer_compute_sps(c_pse_unit);

		// go back to the main menu
		lv_obj_t* curr_scr = lv_scr_act();
		Home_Screen_Gen(c_pse_units, c_pse_units_num);
		lv_obj_del(curr_scr);
	}
}

static lv_coord_t widg_col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
static lv_coord_t widg_row_dsc[] = {LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};

static keypad_data kp_data;

lv_obj_t* flow_widget_label;
static void update_flow(keypad_data* data){
	pse_unit* unit = data->user_data;
	unit->flow = data->value;
	lv_label_set_text_fmt(flow_widget_label, "Debit : \n%lu.%lu\nmL/mn", unit->flow / 1000, unit->flow % 1000);
}
static void flow_edit_handler(lv_event_t* e){
  lv_event_code_t code = lv_event_get_code(e);

	if(code == LV_EVENT_CLICKED) {
		pse_unit* unit = lv_event_get_user_data(e);
		kp_data = (keypad_data){
			.parent = this_screen,
			.cb = update_flow,
			.user_data = unit,
		};
		Keypad_screen_launch(&kp_data);
	}
}

static lv_obj_t* flow_widget(lv_obj_t* parent, pse_unit* unit){
	// The main container
	lv_obj_t* cont = lv_obj_create(parent);
	lv_obj_set_size(cont, lv_pct(24), lv_pct(100));
	lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
	lv_obj_add_event_cb(cont, flow_edit_handler, LV_EVENT_ALL, unit);

	// flow setting
	lv_obj_t* flow = lv_label_create(cont);
	lv_obj_set_width(flow, lv_pct(100));
	lv_obj_set_flex_grow(flow, 1);
	lv_label_set_text_fmt(flow, "Debit : \n%lu.%lu\nmL/mn", unit->flow / 1000, unit->flow % 1000);
	flow_widget_label = flow;
	return cont;
}

lv_obj_t* volume_widget_label;
static void update_volume(keypad_data* data){
	pse_unit* unit = data->user_data;
	unit->set_volume = data->value;
	lv_label_set_text_fmt(volume_widget_label, "Volume : \n%lu.%lu\nmL", unit->set_volume / 1000, unit->set_volume % 1000);
}
static void volume_edit_handler(lv_event_t* e){
  lv_event_code_t code = lv_event_get_code(e);

	if(code == LV_EVENT_CLICKED) {
		pse_unit* unit = lv_event_get_user_data(e);
		kp_data = (keypad_data){
			.parent = this_screen,
			.cb = update_volume,
			.user_data = unit,
		};
		Keypad_screen_launch(&kp_data);
	}
}
static lv_obj_t* volume_widget(lv_obj_t* parent, pse_unit* unit){
	// The main container
	lv_obj_t* cont = lv_obj_create(parent);
	lv_obj_set_size(cont, lv_pct(24), lv_pct(100));
	lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
	lv_obj_add_event_cb(cont, volume_edit_handler, LV_EVENT_ALL, unit);

	// volume setting
	lv_obj_t* vol = lv_label_create(cont);
	lv_obj_set_width(vol, lv_pct(100));
	lv_obj_set_flex_grow(vol, 1);
	lv_label_set_text_fmt(vol, "Volume : \n%lu.%lu\nmL", unit->set_volume / 1000, unit->set_volume % 1000);
	volume_widget_label = vol;
	return cont;
}

lv_obj_t* syringe_widget_label;
static void update_syringe(keypad_data* data){
	pse_unit* unit = data->user_data;
	unit->syringe->diameter = data->value;
	pse_syringe* syringe = unit->syringe;
	lv_label_set_text_fmt(syringe_widget_label, "Seringue : \n%s\nD %d.%d mm", syringe->name, syringe->diameter/1000, syringe->diameter%1000);
}
static void syringe_edit_handler(lv_event_t* e){
  lv_event_code_t code = lv_event_get_code(e);

	if(code == LV_EVENT_CLICKED) {
		pse_unit* unit = lv_event_get_user_data(e);
		kp_data = (keypad_data){
			.parent = this_screen,
			.cb = update_syringe,
			.user_data = unit,
		};
		Keypad_screen_launch(&kp_data);
	}
}
static lv_obj_t* syringe_widget(lv_obj_t* parent, pse_unit* unit){
	// The main container
	lv_obj_t* cont = lv_obj_create(parent);
	lv_obj_set_size(cont, lv_pct(24), lv_pct(100));
	lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
	 lv_obj_add_event_cb(cont, syringe_edit_handler, LV_EVENT_ALL, unit);

	pse_syringe* syringe = unit->syringe;
	// syringe setting
	lv_obj_t* vol = lv_label_create(cont);
	lv_obj_set_width(vol, lv_pct(100));
	lv_obj_set_flex_grow(vol, 1);
	lv_label_set_text_fmt(vol, "Seringue : \n%s\nD %d.%d mm", syringe->name, syringe->diameter/1000, syringe->diameter%1000);
	syringe_widget_label = vol;
	return cont;
}

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

	pse_unit* unit = lv_event_get_user_data(e);

	if(code == LV_EVENT_PRESSED) {
		unit->stepper_conf->step_max = 1;
		pse_sp_set_dir(unit->stepper_conf, 0);
		pse_sp_start_axis(unit->stepper_conf);
	}
	else if(code == LV_EVENT_RELEASED){
		unit->stepper_conf->step_max = 0;
		pse_sp_stop_axis(unit->stepper_conf);
	}
}
static void jog_backward_button_handler(lv_event_t* e){
	lv_event_code_t code = lv_event_get_code(e);

	pse_unit* unit = lv_event_get_user_data(e);

	if(code == LV_EVENT_PRESSED) {
		unit->stepper_conf->step_max = 1;
		pse_sp_set_dir(unit->stepper_conf, 1);
		pse_sp_start_axis(unit->stepper_conf);
	}
	else if(code == LV_EVENT_RELEASED){
		unit->stepper_conf->step_max = 0;
		pse_sp_stop_axis(unit->stepper_conf);
	}
}
static void set_home_button_handler(lv_event_t* e){
	lv_event_code_t code = lv_event_get_code(e);

	pse_unit* unit = lv_event_get_user_data(e);

	if(code == LV_EVENT_CLICKED) {
		unit->stepper_conf->steps_counter = 0;
	}
}

static lv_obj_t* controls_widget(lv_obj_t* parent, pse_unit* unit){
	// The main container
	lv_obj_t* cont = lv_obj_create(parent);
	lv_obj_set_size(cont, lv_pct(24), lv_pct(100));
	lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
	// lv_obj_add_event_cb(cont, unit_widget_clicked_handler, LV_EVENT_ALL, pse_unit);

	// forward
	lv_obj_t* forw = lv_btn_create(cont);
	lv_obj_t* forw_label = lv_label_create(forw);
	lv_obj_set_width(forw, lv_pct(100));
	lv_obj_set_flex_grow(forw, 1);
	lv_obj_add_event_cb(forw, jog_forward_button_handler, LV_EVENT_ALL, unit);
	lv_label_set_text(forw_label, LV_SYMBOL_RIGHT);
	lv_obj_center(forw_label);
	// backward
	lv_obj_t* back = lv_btn_create(cont);
	lv_obj_t* back_label = lv_label_create(back);
	lv_obj_set_width(back, lv_pct(100));
	lv_obj_set_flex_grow(back, 1);
	lv_obj_add_event_cb(back, jog_backward_button_handler, LV_EVENT_ALL, unit);
	lv_label_set_text(back_label, LV_SYMBOL_LEFT);
	lv_obj_center(back_label);
	// set Home
	lv_obj_t* home = lv_btn_create(cont);
	lv_obj_t* home_label = lv_label_create(home);
	lv_obj_set_width(home, lv_pct(100));
	lv_obj_set_flex_grow(home, 1);
	lv_obj_add_event_cb(home, set_home_button_handler, LV_EVENT_ALL, unit);
	lv_label_set_text(home_label, LV_SYMBOL_HOME);
	lv_obj_center(home_label);
	return cont;
}

void PSE_unit_edit_screen_Gen(pse_unit* units, pse_unit* c_unit, uint8_t units_num){
	// Create a new screen
	lv_obj_t* scr = lv_obj_create(NULL);
	this_screen = scr;

	c_pse_units = units;
	c_pse_unit = c_unit;
	c_pse_units_num = units_num;

	// 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);

	// grid layout for the widgets
	lv_obj_t* units_grid = lv_obj_create(scr);
	lv_obj_align_to(units_grid, top_menu, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
	lv_obj_set_size(units_grid, lv_pct(100), lv_pct(80));
	lv_obj_set_style_pad_all(units_grid, 5, 0);
	lv_obj_set_layout(units_grid, LV_LAYOUT_GRID);

	lv_obj_set_grid_dsc_array(units_grid, widg_col_dsc, widg_row_dsc);

	// Flow widget
	lv_obj_t* flow = flow_widget(units_grid, c_unit);
	lv_obj_set_grid_cell(flow, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
	// Set volume widget
	lv_obj_t* vol = volume_widget(units_grid, c_unit);
	lv_obj_set_grid_cell(vol, LV_GRID_ALIGN_CENTER, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
	// Syringe widget
	lv_obj_t* syringe = syringe_widget(units_grid, c_unit);
	lv_obj_set_grid_cell(syringe, LV_GRID_ALIGN_CENTER, 2, 1, LV_GRID_ALIGN_CENTER, 0, 1);
	// controls widget
	lv_obj_t* controls = controls_widget(units_grid, c_unit);
	lv_obj_set_grid_cell(controls, LV_GRID_ALIGN_CENTER, 3, 1, LV_GRID_ALIGN_CENTER, 0, 1);

	// fade in the new screen
	lv_scr_load_anim(scr, LV_SCR_LOAD_ANIM_NONE, 0, 0, true);
}
