#include <Arduino.h>


#include <Wire.h>

#include <SPI.h>
#include <TFT_eSPI.h> 
#include "FT62XXTouchScreen.h"

TFT_eSPI tft = TFT_eSPI();
FT62XXTouchScreen touchScreen = FT62XXTouchScreen(TFT_WIDTH, PIN_SDA, PIN_SCL);


#include "lvgl.h"
#include "widgets/meter/lv_meter.h"
#include "esp_freertos_hooks.h"

static const uint16_t screenWidth  = 480;
static const uint16_t screenHeight = 320;

static lv_disp_draw_buf_t disp_buf;
static lv_color_t buf[screenWidth * 10];




lv_obj_t *screenMain;
lv_obj_t *tabview;
  lv_obj_t *tab1;
    lv_obj_t *label;
    lv_obj_t *btn1;
    lv_obj_t *btn2;
  lv_obj_t *tab2;
  lv_obj_t *guage;
  lv_obj_t *meter;


static void event_handler_btn(lv_event_t * e){
    lv_event_code_t event = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(event == LV_EVENT_CLICKED) {
        if (obj == btn1)
          lv_label_set_text(label, "Hello");
        else if (obj == btn2){
          lv_label_set_text(label, "Goodbye");
        }
    }
}

void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
    uint32_t w = (area->x2 - area->x1 + 1);
    uint32_t h = (area->y2 - area->y1 + 1);

    tft.startWrite();
    tft.setAddrWindow(area->x1, area->y1, w, h);
    tft.pushColors(&color_p->full, w * h, true);
    tft.endWrite();

    lv_disp_flush_ready(disp);
}

uint16_t lastx = 0;
uint16_t lasty = 0;

bool my_input_read2(lv_indev_drv_t * drv, lv_indev_data_t*data) {
  Serial.println("#");
  TouchPoint touchPos = touchScreen.read();
  if (touchPos.touched) {
    Serial.println(String(touchPos.xPos) + ": " + String(touchPos.yPos));
    data->state = LV_INDEV_STATE_PR;
    data->point.x = touchPos.xPos;
    data->point.y = touchPos.yPos;
    lastx = touchPos.xPos;
    lasty = touchPos.yPos;
  } else {
    data->state = LV_INDEV_STATE_REL;
     data->point.x = lastx;
     data->point.y = lasty;
     
  }
  return false;
}
static void lv_tick_task(void)
{
 lv_tick_inc(portTICK_RATE_MS);
}

// static void set_value(void * indic, int32_t v)
// {
//     lv_meter_set_indicator_end_value(meter, indic, v);
// }


/**
 * A meter with multiple arcs
 */
// void lv_example_meter_2(void)
// {
//     meter = lv_meter_create(lv_scr_act());
//     lv_obj_center(meter);
//     lv_obj_set_size(meter, 200, 200);

//     /*Remove the circle from the middle*/
//     lv_obj_remove_style(meter, NULL, LV_PART_INDICATOR);

//     /*Add a scale first*/
//     lv_meter_scale_t * scale = lv_meter_add_scale(meter);
//     lv_meter_set_scale_ticks(meter, scale, 11, 2, 10, lv_palette_main(LV_PALETTE_GREY));
//     lv_meter_set_scale_major_ticks(meter, scale, 1, 2, 30, lv_color_hex3(0xeee), 15);
//     lv_meter_set_scale_range(meter, scale, 0, 100, 270, 90);

//     /*Add a three arc indicator*/
//     lv_meter_indicator_t * indic1 = lv_meter_add_arc(meter, scale, 10, lv_palette_main(LV_PALETTE_RED), 0);
//     lv_meter_indicator_t * indic2 = lv_meter_add_arc(meter, scale, 10, lv_palette_main(LV_PALETTE_GREEN), -10);
//     lv_meter_indicator_t * indic3 = lv_meter_add_arc(meter, scale, 10, lv_palette_main(LV_PALETTE_BLUE), -20);

//     /*Create an animation to set the value*/
//     lv_anim_t a;
//     lv_anim_init(&a);
//     lv_anim_set_exec_cb(&a, set_value);
//     lv_anim_set_values(&a, 0, 100);
//     lv_anim_set_repeat_delay(&a, 100);
//     lv_anim_set_playback_delay(&a, 100);
//     lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);

//     lv_anim_set_time(&a, 2000);
//     lv_anim_set_playback_time(&a, 500);
//     lv_anim_set_var(&a, indic1);
//     lv_anim_start(&a);

//     lv_anim_set_time(&a, 1000);
//     lv_anim_set_playback_time(&a, 1000);
//     lv_anim_set_var(&a, indic2);
//     lv_anim_start(&a);

//     lv_anim_set_time(&a, 1000);
//     lv_anim_set_playback_time(&a, 2000);
//     lv_anim_set_var(&a, indic3);
//     lv_anim_start(&a);
// }


void lv_ex_tabview(lv_obj_t* par)
{
    /*Create a Tab view object*/
    tabview = lv_tabview_create(par);

    /*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
    tab1 = lv_tabview_add_tab(tabview, "Tab 1");
    tab2 = lv_tabview_add_tab(tabview, "Tab 2");
}

void lv_Create_text(lv_obj_t* par)
{
  label = lv_label_create(par);
  lv_label_set_long_mode(label, LV_LABEL_LONG_CLIP);
  lv_label_set_text(label, "Press a button");
  lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER,0);
  lv_obj_set_size(label, 240, 40);
  lv_obj_set_pos(label, 0, 15);
}

void lv_Create_button1(lv_obj_t* par)
{
  btn1 = lv_btn_create(par);
  lv_obj_add_event_cb(btn1, event_handler_btn,LV_EVENT_ALL);
  lv_obj_set_width(btn1, 70);
  lv_obj_set_height(btn1, 32);
  lv_obj_set_pos(btn1, 32, 100);
  lv_obj_t * label1 = lv_label_create(btn1);
  lv_label_set_text(label1, "Hello");
}

void lv_Create_button2(lv_obj_t* par)
{
  btn2 = lv_btn_create(par);
  lv_obj_add_event_cb(btn2, event_handler_btn,LV_EVENT_ALL);
  lv_obj_set_width(btn2, 70);
  lv_obj_set_height(btn2, 32);
  lv_obj_set_pos(btn2, 142, 100);
  lv_obj_t * label2 = lv_label_create(btn2);
  lv_label_set_text(label2, "Goodbye");
}


void setup() {
  lv_disp_drv_t disp_drv;
  lv_indev_drv_t indev_drv;

  Serial.begin(115200);
  lv_init();
  // Setup tick hook for lv_tick_task
  /*esp_err_t err = */esp_register_freertos_tick_hook((esp_freertos_tick_cb_t)lv_tick_task);

  // Enable TFT
  tft.begin();
  tft.setRotation(1);

  // Enable Backlight
  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL,1);

  // Start TouchScreen
  touchScreen.begin();


  // Display Buffer
  lv_disp_draw_buf_init(&disp_buf, buf, NULL, screenWidth * 10);

  // Init Display
  lv_disp_drv_init(&disp_drv);
  disp_drv.hor_res = 480;
  disp_drv.ver_res = 320;
  disp_drv.flush_cb = my_disp_flush;
  disp_drv.draw_buf = &disp_buf;
  lv_disp_drv_register(&disp_drv);

  // Init Touchscreen
  lv_indev_drv_init(&indev_drv);
  indev_drv.type = LV_INDEV_TYPE_POINTER;
  indev_drv.read_cb = my_input_read2;
  lv_indev_drv_register(&indev_drv);

  // Screen Object
  screenMain = lv_obj_create(NULL, NULL);

  lv_ex_tabview(screenMain);

  // Text
  lv_Create_text(tab1);

  // BUtton 1
  lv_Create_button1(tab1);


  // Button 2
  lv_Create_button2(tab1);

  lv_gauge_2(tab2);

  // Screen load
  lv_scr_load(screenMain);
}

void loop() {
  lv_task_handler();
  delay(1);
}
