#include <string.h>
#include <Arduino.h>
#include "screens.h"
#include "images.h"
#include "fonts.h"
#include "actions.h"
#include "vars.h"
#include "styles.h"
#include "ui.h"

#include <string.h>
objects_t objects;
lv_obj_t *tick_value_change_obj;




void create_screen_main() {
    lv_obj_t *obj = lv_obj_create(0);
    objects.main = obj;
    lv_obj_set_pos(obj, 0, 0);
    lv_obj_set_size(obj, 240, 240);
    lv_obj_add_event_cb(obj, action_switch_screens, LV_EVENT_GESTURE, (void *)0);
    {
        lv_obj_t *parent_obj = obj;
        {
            lv_obj_t *obj = lv_led_create(parent_obj);
            objects.alarmLED = obj;
            lv_obj_set_size(obj, 100, 100);
            lv_obj_center(obj);
            lv_led_set_color(obj, lv_color_hex(0xffff4411));
            lv_led_set_brightness(obj, 0);
            lv_obj_set_style_shadow_width(obj, 20, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_shadow_spread(obj, 16, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
        }

        {
            // tempArc
            lv_obj_t *obj = lv_arc_create(parent_obj);
            //lv_obj_add_flag(obj, LV_OBJ_FLAG_ADV_HITTEST);
            //lv_obj_set_ext_click_area(obj,10);
            objects.temp_arc = obj;
            lv_obj_set_pos(obj, 15, 15);
            lv_obj_set_size(obj, 210, 210);
            lv_arc_set_range(obj, 10*2, 30*2);
            lv_arc_set_value(obj, 23*2);
            lv_obj_add_event_cb(obj, action_zoom_set_temp, LV_EVENT_PRESSED, (void *)1);
            lv_obj_add_event_cb(obj, action_zoom_set_temp, LV_EVENT_RELEASED, (void *)0);
            lv_obj_add_event_cb(obj, action_zoom_set_temp, LV_EVENT_PRESSING, (void *)1);
            //lv_obj_add_event_cb(obj, action_zoom_set_temp, LV_EVENT_GESTURE, (void *)1);
            add_style_arc(obj);
        }
        {
            lv_obj_t *btn = lv_button_create(parent_obj);
            lv_obj_remove_style_all(btn);
            lv_obj_set_size(btn, 100, 60);
            lv_obj_set_align(btn,LV_ALIGN_CENTER);
            lv_obj_add_event_cb(btn, showChartScreen, LV_EVENT_LONG_PRESSED,NULL);
            //lv_obj_add_flag(btn,LV_OBJ_FLAG_CLICKABLE);
            // tempTxt
            lv_obj_t *obj = lv_label_create(parent_obj);
            objects.temp_txt = obj;
            lv_obj_set_align(obj,LV_ALIGN_CENTER);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, "");
            lv_obj_set_style_text_font(obj, &lv_font_montserrat_38, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
        {
            // humTxt
            lv_obj_t *obj = lv_label_create(parent_obj);
            objects.hum_txt = obj;
            lv_obj_set_pos(obj, 91, 141);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_obj_align(obj,LV_ALIGN_CENTER,0,25);
            lv_label_set_text(obj, "");
            lv_obj_set_style_text_font(obj, &lv_font_montserrat_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
        {
            // pressTxt
            lv_obj_t *obj = lv_label_create(parent_obj);
            objects.press_txt = obj;
            lv_obj_set_pos(obj, 81, 78);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_obj_align(obj,LV_ALIGN_CENTER,0,-25);
            lv_label_set_text(obj, "");
            lv_obj_set_style_text_font(obj, &lv_font_montserrat_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
        {
            // Heating active
            lv_obj_t *obj = lv_label_create(parent_obj);
            objects.heaterIcn = obj;
            //lv_obj_set_pos(obj, 120-9, 205);
            lv_obj_align(obj, LV_ALIGN_BOTTOM_MID,0,-5);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, ICON_FIRE);
            lv_obj_set_style_text_font(obj, &Bootstrap_Icons_18, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
        {
            // Buffer_enabled
            lv_obj_t *obj = lv_label_create(parent_obj);
            objects.bufferIcn = obj;
            //lv_obj_set_pos(obj, 140, 200);
            lv_obj_align(obj, LV_ALIGN_BOTTOM_MID,25,-10);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, ICON_THERMOMETER_SUN);
            lv_obj_set_style_text_font(obj, &Bootstrap_Icons_18, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
        {
            // wifi_strength
            lv_obj_t *obj = lv_label_create(parent_obj);
            objects.wifiIcn = obj;
            lv_obj_align(obj, LV_ALIGN_BOTTOM_MID,-25,-10);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, ICON_WIFI_HIGH);
            lv_obj_set_style_text_font(obj, &Bootstrap_Icons_18, LV_PART_MAIN | LV_STATE_DEFAULT);
        }

        /*{
            // panel_arc
            lv_obj_t *obj = lv_obj_create(parent_obj);
            objects.panel_arc = obj;
            lv_obj_set_pos(obj, 58, 78);
            lv_obj_set_size(obj, 125, 162);
            lv_obj_set_style_bg_opa(obj, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_border_width(obj, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        }*/
    }
    
}

void create_screen_chart() {
    lv_obj_t *obj = lv_obj_create(0);
    objects.second = obj;
    lv_obj_set_pos(obj, 0, 0);
    lv_obj_set_size(obj, 240, 240);
    lv_obj_add_event_cb(obj, action_switch_screens, LV_EVENT_GESTURE, (void *)1);
    lv_obj_set_style_bg_color(obj, lv_color_hex(0xff000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    {
        lv_obj_t *parent_obj = obj;
        {
            lv_obj_t * btn_back = lv_button_create(parent_obj);
            lv_obj_remove_style_all(btn_back);
            lv_obj_set_size(btn_back, 40, 40);
            lv_obj_align(btn_back,LV_ALIGN_BOTTOM_MID,-30,-10);
            lv_obj_add_event_cb(btn_back, showMainScreen, LV_EVENT_RELEASED,NULL);
            lv_obj_t * lbl_btn_back = lv_label_create(btn_back);
            lv_obj_set_size(lbl_btn_back, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_obj_center(lbl_btn_back);
            lv_label_set_text(lbl_btn_back,LV_SYMBOL_HOME);
            
            lv_obj_t * btn_dbg = lv_button_create(parent_obj);
            lv_obj_remove_style_all(btn_dbg);
            lv_obj_set_size(btn_dbg, 40, 40);
            lv_obj_align(btn_dbg,LV_ALIGN_BOTTOM_MID,30,-10);
            lv_obj_add_event_cb(btn_dbg, showDebugScreen, LV_EVENT_RELEASED,NULL);
            lv_obj_t * lbl_btn_dbg = lv_label_create(btn_dbg);
            lv_obj_set_size(lbl_btn_dbg, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_obj_center(lbl_btn_dbg);
            lv_label_set_text(lbl_btn_dbg,LV_SYMBOL_SETTINGS);
            /*Create a container*/
            lv_obj_t * main_cont = lv_obj_create(parent_obj);
            lv_obj_remove_style_all(main_cont);
            lv_obj_set_size(main_cont, 173, 125);
            lv_obj_set_pos(main_cont, 41, 62);
            
            /*Create a transparent wrapper for the chart and the scale.
            *Set a large width, to make it scrollable on the main container*/
            lv_obj_t * wrapper = lv_obj_create(main_cont);
            lv_obj_remove_style_all(wrapper);
            lv_obj_set_size(wrapper, 173*3, lv_pct(100));
            // chart
            lv_obj_t *chart = lv_chart_create(wrapper);
            objects.chart = chart;
            lv_obj_set_style_pad_all(chart,0,LV_STATE_ANY | LV_PART_MAIN);
            lv_obj_set_pos(chart, 0, 0);
            lv_obj_set_width(chart, lv_pct(100));
            lv_obj_set_height(chart, lv_pct(100)-22);
            lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 100, 350);
            lv_chart_set_point_count(chart, 48*12);
            lv_obj_add_event_cb(chart, action_show_cursors_cb, LV_EVENT_VALUE_CHANGED, NULL);
            lv_chart_set_div_line_count(chart, 6, 48);
            lv_obj_add_event_cb(chart, chartDrawingCB, LV_EVENT_ALL, NULL);
            objects.chart_cursor = lv_chart_add_cursor(chart, lv_color_hex(0xc3c3c3), (lv_dir_t)(LV_DIR_LEFT | LV_DIR_BOTTOM | LV_DIR_TOP));
            lv_obj_set_style_line_width(chart,1,LV_PART_CURSOR | LV_STATE_DEFAULT);
            lv_obj_set_style_line_dash_gap(chart,3,LV_PART_CURSOR | LV_STATE_DEFAULT);
            lv_obj_set_style_line_dash_width(chart,1,LV_PART_CURSOR | LV_STATE_DEFAULT);
            lv_obj_set_style_radius(chart,2,LV_PART_CURSOR|LV_STATE_DEFAULT);
            lv_obj_set_style_width(chart,2,LV_PART_CURSOR|LV_STATE_DEFAULT);
            lv_obj_set_style_height(chart,2,LV_PART_CURSOR|LV_STATE_DEFAULT);
            lv_obj_set_style_bg_color(chart,lv_color_darken(lv_color_hex(0xff8ff321),10),LV_PART_CURSOR|LV_STATE_DEFAULT);

            objects.chart_series = lv_chart_add_series(chart,lv_color_hex(0xff8ff321), LV_CHART_AXIS_PRIMARY_Y);
            /*uint32_t i;
            for(i = 5; i < 36; i++) {
                lv_chart_set_next_value(chart, objects.chart_series, i);
            }*/
            // xScale
            lv_chart_set_ext_y_array(chart,objects.chart_series,history.temp);
            lv_obj_t *obj = lv_scale_create(wrapper);
            objects.x_scale = obj;
            lv_obj_set_size(obj, lv_pct(100)-3, 22);
            lv_obj_align_to(obj,chart,LV_ALIGN_OUT_BOTTOM_MID,0,0);
            lv_scale_set_mode(obj, LV_SCALE_MODE_HORIZONTAL_BOTTOM);
            lv_scale_set_range(obj, -48, 0);
            lv_scale_set_total_tick_count(obj, 48);
            
            lv_obj_add_event_cb(obj, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
            lv_scale_set_major_tick_every(obj, 6);
            lv_scale_set_label_show(obj, true);
            lv_obj_set_style_text_font(obj, &lv_font_montserrat_10, LV_PART_INDICATOR | LV_STATE_DEFAULT);
            lv_obj_set_style_line_width(obj, 1, LV_PART_INDICATOR | LV_STATE_DEFAULT);
            lv_obj_set_style_length(obj, 7, LV_PART_INDICATOR | LV_STATE_DEFAULT);
            lv_obj_set_style_line_width(obj, 1, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_line_width(obj, 1, LV_PART_ITEMS | LV_STATE_DEFAULT);
            lv_obj_set_style_length(obj, 4, LV_PART_ITEMS | LV_STATE_DEFAULT);
        }
        {
            lv_obj_update_layout(objects.chart); //wi calculate real widget heights
            // yScale
            lv_obj_t *obj = lv_scale_create(parent_obj);
            objects.y_scale = obj;
            lv_obj_set_pos(obj, 15, 72);
            lv_obj_set_size(obj, 26, lv_obj_get_height(objects.chart)-19);
            lv_scale_set_mode(obj, LV_SCALE_MODE_VERTICAL_LEFT);
            lv_scale_set_range(obj, 10, 35);
            lv_scale_set_total_tick_count(obj, 26);
            lv_scale_set_major_tick_every(obj, 5);
            lv_scale_set_label_show(obj, true);
            lv_obj_set_style_length(obj, 7, LV_PART_INDICATOR | LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(obj, &lv_font_montserrat_10, LV_PART_INDICATOR | LV_STATE_DEFAULT);
            lv_obj_set_style_line_width(obj, 1, LV_PART_INDICATOR | LV_STATE_DEFAULT);
            lv_obj_set_style_line_width(obj, 1, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_line_width(obj, 1, LV_PART_ITEMS | LV_STATE_DEFAULT);
            lv_obj_set_style_length(obj, 3, LV_PART_ITEMS | LV_STATE_DEFAULT);
        }
        {
            // chartHeader
            lv_obj_t *obj = lv_label_create(parent_obj);
            objects.chart_header = obj;
            lv_obj_set_pos(obj, 1, 30);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, "Temperature");
            lv_obj_set_style_text_font(obj, &lv_font_montserrat_20, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_align(obj, LV_ALIGN_TOP_MID, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
    }
}



void create_screen_debug() {
    lv_obj_t *obj = lv_obj_create(0);
    objects.debug = obj;
    lv_obj_set_pos(obj, 0, 0);
    lv_obj_set_size(obj, 240, 240);
    lv_obj_add_event_cb(obj, action_switch_screens, LV_EVENT_GESTURE, (void *)1);
    lv_obj_set_style_bg_color(obj, lv_color_hex(0xff000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    {
        lv_obj_t *parent_obj = obj;
        {
            lv_obj_t * btn_back = lv_button_create(parent_obj);
            lv_obj_remove_style_all(btn_back);
            lv_obj_set_size(btn_back, 40, 40);
            lv_obj_set_align(btn_back,LV_ALIGN_LEFT_MID);
            lv_obj_add_event_cb(btn_back, showMainScreen, LV_EVENT_RELEASED,NULL);
            lv_obj_t * lbl_btn = lv_label_create(btn_back);
            lv_obj_set_size(lbl_btn, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_obj_center(lbl_btn);
            lv_label_set_text(lbl_btn,LV_SYMBOL_HOME);
            lv_obj_t * btn_reboot = lv_button_create(parent_obj);
            lv_obj_remove_style_all(btn_reboot);
            lv_obj_set_size(btn_reboot, 40, 40);
            lv_obj_set_align(btn_reboot,LV_ALIGN_RIGHT_MID);
            lv_obj_add_event_cb(btn_reboot, rebootESP, LV_EVENT_RELEASED,NULL);
            lbl_btn = lv_label_create(btn_reboot);
            lv_obj_set_size(lbl_btn, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_obj_center(lbl_btn);
            lv_obj_set_style_text_font(lbl_btn, &Bootstrap_Icons_16, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_label_set_text(lbl_btn,ICON_POWER);
            /*Create a container*/
            lv_obj_t * main_cont = lv_obj_create(parent_obj);
            lv_obj_set_size(main_cont, 170, 170);
            lv_obj_center(main_cont);
            //lv_obj_remove_style_all(main_cont);
            lv_obj_t * wrapper = lv_obj_create(main_cont);
            lv_obj_remove_style_all(wrapper);
            lv_obj_set_size(wrapper, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_obj_t * obj = lv_label_create(main_cont);
            objects.debugTxt = obj;
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            
        }

    }
    
}

void create_screens() {
    lv_disp_t *dispp = lv_disp_get_default();
    lv_theme_t *theme = lv_theme_default_init(dispp, lv_palette_main(LV_PALETTE_GREEN), lv_palette_main(LV_PALETTE_RED), true, LV_FONT_DEFAULT);
    lv_disp_set_theme(dispp, theme);
    
    create_screen_main();
    create_screen_chart();
    create_screen_debug();
}
