/**
 * Marlin 3D Printer Firmware
 * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
 *
 * Based on Sprinter and grbl.
 * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "../../../inc/MarlinConfigPre.h"

#if HAS_TFT_LVGL_UI

#include "draw_ui.h"
#include <lv_conf.h>

#include "../../../gcode/queue.h"
#include "../../../gcode/gcode.h"
#include "../../../inc/MarlinConfig.h"

#if ENABLED(EEPROM_SETTINGS)
  #include "../../../module/settings.h"
#endif

#if HAS_BED_PROBE
  #include "../../../module/probe.h"
#endif

extern lv_group_t *g;
static lv_obj_t *scr;

static lv_obj_t *labelV, *buttonV, *zOffsetText;

enum {
  ID_BABYSTEP_X_P = 1,
  ID_BABYSTEP_X_N,
  ID_BABYSTEP_Y_P,
  ID_BABYSTEP_Y_N,
  ID_BABYSTEP_Z_P,
  ID_BABYSTEP_Z_N,
  ID_BABYSTEP_DIST,
  ID_BABYSTEP_RETURN
};

static float babystep_dist  = 0.01;
static uint8_t has_adjust_z = 0;

static void event_handler(lv_obj_t *obj, lv_event_t event) {
  if (event != LV_EVENT_RELEASED) return;
  char baby_buf[30] = { 0 };
  char str_1[16];
  switch (obj->mks_obj_id) {
    case ID_BABYSTEP_X_P:
      sprintf_P(baby_buf, PSTR("M290 X%s"), dtostrf(babystep_dist, 1, 3, str_1));
      gcode.process_subcommands_now(F(baby_buf));
      has_adjust_z = 1;
      break;
    case ID_BABYSTEP_X_N:
      sprintf_P(baby_buf, PSTR("M290 X%s"), dtostrf(-babystep_dist, 1, 3, str_1));
      gcode.process_subcommands_now(F(baby_buf));
      has_adjust_z = 1;
      break;
    case ID_BABYSTEP_Y_P:
      sprintf_P(baby_buf, PSTR("M290 Y%s"), dtostrf(babystep_dist, 1, 3, str_1));
      gcode.process_subcommands_now(F(baby_buf));
      has_adjust_z = 1;
      break;
    case ID_BABYSTEP_Y_N:
      sprintf_P(baby_buf, PSTR("M290 Y%s"), dtostrf(-babystep_dist, 1, 3, str_1));
      gcode.process_subcommands_now(F(baby_buf));
      has_adjust_z = 1;
      break;
    case ID_BABYSTEP_Z_P:
      sprintf_P(baby_buf, PSTR("M290 Z%s"), dtostrf(babystep_dist, 1, 3, str_1));
      gcode.process_subcommands_now(F(baby_buf));
      has_adjust_z = 1;
      break;
    case ID_BABYSTEP_Z_N:
      sprintf_P(baby_buf, PSTR("M290 Z%s"), dtostrf(-babystep_dist, 1, 3, str_1));
      gcode.process_subcommands_now(F(baby_buf));
      has_adjust_z = 1;
      break;
    case ID_BABYSTEP_DIST:
      if (ABS((int)(100 * babystep_dist)) == 1)
        babystep_dist = 0.05;
      else if (ABS((int)(100 * babystep_dist)) == 5)
        babystep_dist = 0.1;
      else
        babystep_dist = 0.01;
      disp_baby_step_dist();
      break;
    case ID_BABYSTEP_RETURN:
      if (has_adjust_z == 1) {
        TERN_(EEPROM_SETTINGS, (void)settings.save());
        has_adjust_z = 0;
      }
      goto_previous_ui();
      break;
  }
}

void lv_draw_baby_stepping() {
  scr = lv_screen_create(BABYSTEP_UI);
  lv_big_button_create(scr, "F:/bmp_xAdd.bin", move_menu.x_add, INTERVAL_W, titleHeight, event_handler, ID_BABYSTEP_X_P);
  lv_big_button_create(scr, "F:/bmp_xDec.bin", move_menu.x_dec, INTERVAL_W, BTN_SIZE_Y + INTERVAL_H + titleHeight, event_handler, ID_BABYSTEP_X_N);
  lv_big_button_create(scr, "F:/bmp_yAdd.bin", move_menu.y_add, BTN_SIZE_X + INTERVAL_W * 2, titleHeight, event_handler, ID_BABYSTEP_Y_P);
  lv_big_button_create(scr, "F:/bmp_yDec.bin", move_menu.y_dec, BTN_SIZE_X + INTERVAL_W * 2, BTN_SIZE_Y + INTERVAL_H + titleHeight, event_handler, ID_BABYSTEP_Y_N);
  lv_big_button_create(scr, "F:/bmp_zAdd.bin", move_menu.z_add, BTN_SIZE_X * 2 + INTERVAL_W * 3, titleHeight, event_handler, ID_BABYSTEP_Z_P);
  lv_big_button_create(scr, "F:/bmp_zDec.bin", move_menu.z_dec, BTN_SIZE_X * 2 + INTERVAL_W * 3, BTN_SIZE_Y + INTERVAL_H + titleHeight, event_handler, ID_BABYSTEP_Z_N);
  buttonV = lv_imgbtn_create(scr, nullptr, BTN_SIZE_X * 3 + INTERVAL_W * 4, titleHeight, event_handler, ID_BABYSTEP_DIST);
  labelV = lv_label_create_empty(buttonV);
  #if HAS_ROTARY_ENCODER
    if (gCfgItems.encoder_enable)
      lv_group_add_obj(g, buttonV);
  #endif

  lv_big_button_create(scr, "F:/bmp_return.bin", common_menu.text_back, BTN_SIZE_X * 3 + INTERVAL_W * 4, BTN_SIZE_Y + INTERVAL_H + titleHeight, event_handler, ID_BABYSTEP_RETURN);

  disp_baby_step_dist();

  zOffsetText = lv_label_create(scr, 290, TITLE_POS_Y, nullptr);
  disp_z_offset_value();
}

void disp_baby_step_dist() {
  if ((int)(100 * babystep_dist) == 1)
    lv_imgbtn_set_src_both(buttonV, "F:/bmp_baby_move0_01.bin");
  else if ((int)(100 * babystep_dist) == 5)
    lv_imgbtn_set_src_both(buttonV, "F:/bmp_baby_move0_05.bin");
  else if ((int)(100 * babystep_dist) == 10)
    lv_imgbtn_set_src_both(buttonV, "F:/bmp_baby_move0_1.bin");

  if (gCfgItems.multiple_language) {
    if ((int)(100 * babystep_dist) == 1) {
      lv_label_set_text(labelV, move_menu.step_001mm);
      lv_obj_align(labelV, buttonV, LV_ALIGN_IN_BOTTOM_MID, 0, BUTTON_TEXT_Y_OFFSET);
    }
    else if ((int)(100 * babystep_dist) == 5) {
      lv_label_set_text(labelV, move_menu.step_005mm);
      lv_obj_align(labelV, buttonV, LV_ALIGN_IN_BOTTOM_MID, 0, BUTTON_TEXT_Y_OFFSET);
    }
    else if ((int)(100 * babystep_dist) == 10) {
      lv_label_set_text(labelV, move_menu.step_01mm);
      lv_obj_align(labelV, buttonV, LV_ALIGN_IN_BOTTOM_MID, 0, BUTTON_TEXT_Y_OFFSET);
    }
  }
}

void disp_z_offset_value() {
  char buf[20];
  #if HAS_BED_PROBE
    char str_1[16];
    sprintf_P(buf, PSTR("Offset Z: %s mm"), dtostrf(probe.offset.z, 1, 3, str_1));
  #else
    strcpy_P(buf, PSTR("Offset Z: 0 mm"));
  #endif
  lv_label_set_text(zOffsetText, buf);
}

void lv_clear_baby_stepping() {
  #if HAS_ROTARY_ENCODER
    if (gCfgItems.encoder_enable) lv_group_remove_all_objs(g);
  #endif
  lv_obj_del(scr);
}

#endif // HAS_TFT_LVGL_UI
