#include "lvgl.h"
#include "img_def.h"
#include "demo.h"

static lv_obj_t* g_start_page_obj;
static lv_obj_t* g_animing_start;

extern lv_obj_t *meter;
extern lv_meter_indicator_t * indic;
extern lv_meter_indicator_t * indic1;

extern lv_obj_t * oil_meter;
extern lv_meter_indicator_t *oil_indic1;
extern lv_meter_indicator_t *oil_indic2;

extern lv_obj_t * temp_meter;
extern lv_meter_indicator_t *temp_indic1;
extern lv_meter_indicator_t *temp_indic2;

#define ANIMIMG_NUM 3
LV_ATTRIBUTE_LARGE_CONST static const lv_img_dsc_t* test_animo[4] =
{
    IMG_DM1_OIL,
    IMG_DM1_TCS,
    IMG_DM1_TEMP,
    IMG_DM1_NULL,
};

static void my_lvgl_timer(lv_timer_t* timer)
{
    static uint8_t page_switch_flag = 0;

    printf("get_animimg_status()========= %d \n",get_animimg_status());
		switch(page_switch_flag)
		{
		  case 2:
                if (timer != NULL)
                {
                    lv_timer_del(timer);
                    timer = NULL;
                    lv_obj_del(g_start_page_obj);
                    g_start_page_obj = NULL;
                }
                    win_init();
			break;


		}
		if(++page_switch_flag>10)
		{
		  page_switch_flag=10;
		}

    if (get_animimg_status() >= 2&& page_switch_flag==0)
    {
        page_switch_flag = 1;
    }

}

void startingup_animing() //开机动画
{
    static lv_style_t startingup_style_base;
    lv_style_init(&startingup_style_base);
    //样式背景颜色：
    lv_style_set_bg_color(&startingup_style_base, lv_color_make(0x00, 0x00, 0x00));//背景黑色
    //样式外边距：1pix
    lv_style_set_border_width(&startingup_style_base, 1);
    //样式外边框颜色：
    lv_style_set_border_color(&startingup_style_base, lv_color_make(0x00, 0x00, 0x00));//边框白色色

    g_start_page_obj = lv_tileview_create(lv_scr_act());                 //创建titlewindow
    lv_obj_set_size(g_start_page_obj, LV_HOR_RES, LV_VER_RES);       //设置到屏幕大小

    lv_obj_set_style_bg_color(g_start_page_obj, lv_color_make(0x00, 0x00, 0x00), 0);//设置背景颜色为黑

    g_animing_start = lv_animimg_create(g_start_page_obj); //动画1
    lv_obj_center(g_animing_start); //居中
    lv_animimg_set_src(g_animing_start, (lv_img_dsc_t**)test_animo, ANIMIMG_NUM);
    lv_animimg_set_duration(g_animing_start, 1500);
    lv_animimg_set_repeat_count(g_animing_start, 1);
}

void moto_animo(void){
    startingup_animing();

    lv_timer_t* timer;
    timer = lv_timer_create(my_lvgl_timer,1000,  g_start_page_obj);
    lv_timer_ready(timer);
    lv_animimg_start(g_animing_start);

}

static void anim_arc_value_cb(void * var, int32_t v)
{
    lv_arc_set_value(var, v);
}

static void anim_pointer_value_cb(void * var, int32_t v)
{
    static uint8_t state =0;
    if(v==0){
        if(state ==1 && Get_sys_power_on_self_test() !=100){
            printf("animo end.\n");
            Set_sys_power_on_self_test(100);
//            lv_anim_del_all();
//            lv_anim_del(total, anim_mileage_value_cb);
        }
        printf("v==0");
        state++;
    }
    lv_img_set_angle(var,v);
}

static void anim_oilarc_value_cb(void * var, int32_t v)
{
    if(v <60){
        lv_meter_set_indicator_end_value(var, oil_indic1, v);
        lv_meter_set_indicator_end_value(var, oil_indic2, 60);
    }else
        lv_meter_set_indicator_end_value(var, oil_indic2, v);
}

static void anim_temparc_value_cb(void * var, int32_t v)
{
    if(v <210){
        lv_meter_set_indicator_end_value(var, temp_indic1, v);
        lv_meter_set_indicator_end_value(var, temp_indic2, 210);
    }else
        lv_meter_set_indicator_end_value(var, temp_indic2, v);
}

static void anim_value_cb(void * var, int32_t v)
{
    lv_label_set_text_fmt(var, "%d",v);
}

static void anim_mileage_value_cb(void * var, int32_t v)
{
    lv_label_set_text_fmt(var, "%d",v*111111);
}


void self_checking_animo(lv_obj_t * parent){

    if(Get_sys_power_on_self_test() ==100)
    return;

    lv_obj_t * ui_Arc1 = lv_obj_get_child(parent,5);
    lv_obj_t * hand_img = lv_obj_get_child(parent,6);

    lv_obj_t * speed = lv_obj_get_child(parent,7);
    lv_obj_t * gear = lv_obj_get_child(parent,8);
    lv_obj_t * gear_null = lv_obj_get_child(parent,9);
    lv_obj_t * tcs = lv_obj_get_child(parent,10);
    lv_obj_t * time_label = lv_obj_get_child(parent,11);
    lv_obj_t * max_speed = lv_obj_get_child(parent,12);
    lv_obj_t * avg_speed = lv_obj_get_child(parent,13);
    lv_obj_t * trip = lv_obj_get_child(parent,14);
    lv_obj_t * total = lv_obj_get_child(parent,15);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, ui_Arc1);
    lv_anim_set_values(&a, 0, 120);//动画范围
    lv_anim_set_time(&a, 1000);//动画播放时长
    lv_anim_set_playback_time(&a, 1000);//播放动画后沿途返回时间
    lv_anim_set_repeat_delay(&a, 1000);//往返两次动画播放间隔 播放动画间的延时
//    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);//重复
    lv_anim_set_repeat_count(&a, 1);//重复
    lv_anim_set_path_cb(&a, lv_anim_path_linear);//线性变化
    lv_anim_set_exec_cb(&a, anim_arc_value_cb);//调用函数
    lv_anim_start(&a);

    lv_anim_set_var(&a, hand_img);
    lv_anim_set_values(&a, 0, 3000);//动画范围
    lv_anim_set_exec_cb(&a, anim_pointer_value_cb);//调用函数
    lv_anim_start(&a);

    lv_anim_set_var(&a, oil_meter);
    lv_anim_set_values(&a, 0, 270);//动画范围
    lv_anim_set_exec_cb(&a, anim_oilarc_value_cb);//调用函数
    lv_anim_start(&a);

    lv_anim_set_var(&a, temp_meter);
    lv_anim_set_values(&a, 0, 270);//动画范围
    lv_anim_set_exec_cb(&a, anim_temparc_value_cb);//调用函数
    lv_anim_start(&a);

    lv_anim_set_var(&a, speed);
    lv_anim_set_values(&a, 0, 255);//动画范围
    lv_anim_set_exec_cb(&a, anim_value_cb);//调用函数
    lv_anim_start(&a);

    lv_anim_set_var(&a, max_speed);
    lv_anim_start(&a);

    lv_anim_set_var(&a, avg_speed);
    lv_anim_start(&a);

    lv_anim_set_var(&a, gear);
    lv_anim_set_values(&a, 0, 9);//动画范围
//    lv_anim_set_exec_cb(&a, anim_value_cb);//调用函数
    lv_anim_start(&a);

    lv_anim_set_var(&a, trip);
    lv_anim_set_values(&a, 0, 9);//动画范围
    lv_anim_set_exec_cb(&a, anim_mileage_value_cb);//调用函数
    lv_anim_start(&a);

    lv_anim_set_var(&a, total);
    lv_anim_set_values(&a, 0, 9);//动画范围
    lv_anim_set_exec_cb(&a, anim_mileage_value_cb);//调用函数
    lv_anim_start(&a);

//    lv_anim_del(ui_Arc1, anim_arc_value_cb);
//    lv_anim_del(hand_img, anim_pointer_value_cb);
//    lv_anim_del(oil_meter, anim_oilarc_value_cb);
//    lv_anim_del(temp_meter, anim_temparc_value_cb);
//
//    lv_anim_del(speed, anim_value_cb);
//    lv_anim_del(max_speed, anim_value_cb);
//    lv_anim_del(avg_speed, anim_value_cb);
//    lv_anim_del(gear, anim_value_cb);
//
//    lv_anim_del(trip, anim_mileage_value_cb);
//    lv_anim_del(total, anim_mileage_value_cb);


    lv_obj_add_flag(gear_null,LV_OBJ_FLAG_HIDDEN);

}

