#include "lvgl.h"
#include "img_def.h"
#include "mxc_meter.h"
#include "moto_list.h"

#define _EMOTO_   // 宏开关
#define _OMOTO_


Local_UiCommon_t local_UiCommon = {0};
daily_data_t custom_data;
lv_coord_t child_id = 0;

daily_data_t *daily_get_UiData(void)
{
    return &custom_data;
}

Local_UiCommon_t *local_get_UiCommon(void)
{
    return &local_UiCommon;
}

ENUM_CONTROL_WIN_ID get_top_history_win(void)    //get lastest win_id history record
{
//    Local_UiCommon_t *local_common = local_get_UiCommon();
//    for(uint8_t i=0; i<HISTORY_WIN_LEVEL;i++)
//    {
//        if(local_common->page_record[HISTORY_WIN_LEVEL-1-i].flag)
//        {
//            printf("get_top_history_win i: %d win_id %d \r\n",\
//            HISTORY_WIN_LEVEL-1-i,local_common->page_record[HISTORY_WIN_LEVEL-1-i].win_id);
//            return local_common->page_record[HISTORY_WIN_LEVEL-1-i].win_id;
//        }
//
//    }
    return 0;
}

void Set_UI_page_id(ENUM_CONTROL_WIN_ID id)
{
    printf("Set_UI_page_id: %d \r\n",id);
    daily_get_UiData()->ui_page_id = id;
}

ENUM_CONTROL_WIN_ID Get_UI_page_id(void)
{
    return daily_get_UiData()->ui_page_id;
}

lv_coord_t get_child_id()
{
    return child_id;
}

void set_child_id(lv_coord_t id)
{
    child_id = id;
}

void  set_win_history_record(ENUM_CONTROL_WIN_ID page_id, uint32_t id)
{
    Local_UiCommon_t *local_common = local_get_UiCommon();
    for(uint8_t i=0; i<HISTORY_WIN_LEVEL;i++)
    {
        if(false==local_common->page_record[i].flag)
        {
            local_common->page_record[i].flag=true;
            local_common->page_record[i].win_id=page_id;
            local_common->page_record[i].index=id;
            return;
        }
    }
}

uint32_t  get_win_history_record(ENUM_CONTROL_WIN_ID page_id)
{
    Local_UiCommon_t *local_common = local_get_UiCommon();
    uint32_t id=0xff;
    for(uint8_t i=0; i<HISTORY_WIN_LEVEL;i++)
    {
        if(local_common->page_record[HISTORY_WIN_LEVEL-1-i].flag
        && (uint8_t)page_id== local_common->page_record[HISTORY_WIN_LEVEL-1-i].win_id)
        {
            id=local_common->page_record[HISTORY_WIN_LEVEL-1-i].index;
            set_child_id(id);
            return id;
        }
    }
    set_child_id(id);
    return id;
}
bool  get_win_history_record_flag(ENUM_CONTROL_WIN_ID page_id)
{
    Local_UiCommon_t *local_common = local_get_UiCommon();
    bool flag=false;
    uint8_t id=0;
    for(uint8_t i=0; i<HISTORY_WIN_LEVEL;i++)
    {
        if((uint8_t)page_id== local_common->page_record[HISTORY_WIN_LEVEL-1-i].win_id)
        {
            flag=local_common->page_record[HISTORY_WIN_LEVEL-1-i].flag;
            return flag;
        }
    }
    return flag;
}

void app_menu_comm_entry(lv_obj_t * obj,  ENUM_CONTROL_WIN_ID win_id, bool register_encode)
{
    Set_UI_page_id(win_id);   //set win id
    if(get_win_history_record_flag(win_id))
    {
        set_child_id(get_win_history_record(win_id));  //get record
    }
    else {
        set_win_history_record( win_id, 0);
        set_child_id(0);
    }

//    if(register_encode){
//        do_register_obj_encode_group(obj);
//    }else{
//        do_unregister_encode_group();
//    }
}


#if 0
SysData_t UserData;

void SystemDate_Init(void)
{
    UserData.mSoftMainVersion = 0;	       //主版本号
	UserData.mSoftSubVersion = 0;	       //次版本号
	//uint8_t bt_name[BT_NAME_MAX_LEN];
	UserData.mSetMode = 0;                //高字节 SET 1 为短按 2为长按  低字节 MODE 1 为短按 2为长按
	UserData.mSpeed = 0;                  //速度信息
	UserData.mGearInfo = 0;               //档位信息
	UserData.mTotalMile = 0;              //总计里程
    UserData.mTripMile = 0;               //单次行驶里程
    UserData.mMileClearTags = 0;          //小计里程清零标志
	UserData.mWifi = 0;				   //WIFI
	UserData.mBlueTooth = 0;              //蓝牙
	UserData.mTurn = 0;				   //转向 	0-无 1-左转 2-右转
	UserData.mTurnPrompt = 0;			   //转向提示	0无 1左 2右
    UserData.mTurnLight = 0;              //转向灯  0-无 1-左转向灯 2-右转向灯
    UserData.mBeamLight = 0;              //灯光	0-无灯光 1-远光 2-近光 3-自动大灯
    UserData.mSideMarkerLight = 0;        //示宽灯 0-无灯光 1-示宽灯
    UserData.mFanLight = 0;               //风扇灯 0-无灯光 1-灯亮
    UserData.mABSLight = 0;               //0-无灯光 1-灯亮
    UserData.mOilPressLight = 0;          //机油压力报警灯 0-无灯光 1-灯亮
    UserData.mEngineLight = 0;            //0-无灯光 1-灯亮
    UserData.mRefreshTags = 0;            //0-无刷新 1-油量/水温/电池电压刷新标志

    UserData.mLightValue = 0;             //灯光亮度,具体的light_value值
	UserData.mSelfCheck = 0;	           //开机自检
	UserData.mLightSensingVolt = 0;       //光感电压值 （暂定范围0-3300）

	UserData.return_demo = 0;             //????
	memcpy(&UserData.mUuid, 0, 32*sizeof(char));
	memcpy(&UserData.mBT_name, 0, 32*sizeof(char));
	//UserData.mUuid[32] = {0};
	//UserData.mBT_name[32] = {0};
	UserData.mCurrentFaultCode = 0;		//当前故障码
	UserData.mHistoryFaultCode = 0;		//历史故障码

	UserData.mBTData_Inherit = 0;          //继承的蓝牙数据(保留)
	UserData.mTtrb = 0;					//开机检测是否获取到时间的标志
	UserData.mTtrb_Time = 0;				//设施时间后检测是否正常设置时间的标志
	UserData.mOdo_Reset = 0;				//小计里程清零后检测是否正常设置时间的标志
	UserData.mDemo_Position = 0;			//是否在demo展示界面的标志

    //告警错误
	UserData.mErr.mEngine = 0;
    UserData.mErr.mEngineOil = 0;
    UserData.mErr.mAbs = 0;
    UserData.mErr.mTcs = 0;
    UserData.mErr.mFan = 0;
    UserData.mErr.mNeutralGear = 0;
    UserData.mErr.mWaterTemper = 0;
    UserData.mErr.mBrakesFault = 0;

    //保存数据
	UserData.mSaveData.mFactorySet = 0;      //出厂设置
	UserData.mSaveData.mLanguage = 0;		//语言
	UserData.mSaveData.mDisplayUint = 0;		//单位
	UserData.mSaveData.mTheme = 0;			//当前主题
	UserData.mSaveData.mThemeState = 0;		//主题状态
	UserData.mSaveData.mLightValue = 0;		//亮度
	UserData.mSaveData.mBT_OnOff = 0;		//蓝牙
	UserData.mSaveData.mBT_State = 0;		//状态
	UserData.mSaveData.mGearPos = 0;			//档位
	UserData.mSaveData.mFuel = 0;			//油量
	UserData.mSaveData.mTotalMile = 0;		//里程数

	//时间变量
	UserData.mTime.ucYear = 0;
	UserData.mTime.ucMonth = 0;
	UserData.mTime.ucDate = 0;
	UserData.mTime.ucHour = 0;
	UserData.mTime.ucMinute = 0;
	UserData.mTime.ucSecond = 0;

	UserData.mPlan = 0;		//总进度标志
	UserData.mPace = 0;		//分进度标志
	UserData.mUpGrade = 0;	    //内存拷贝标志

	//电摩专用属性
	#ifdef _EMOTO_
	UserData.mBatteryRatio = 0;      //电池电量占比  1个字节（eg100% 传输64）
	UserData.mBatteryVoltage = 0;    //电池电压 （eg3.3V 传输3300）
    #endif
    //油摩专用属性
    #ifdef _OMOTO_
	UserData.mEspeed = 0;            //转速信息
	UserData.mFuel = 0;              //油量指示
	UserData.mWaterTemper = 0;       //水温指示
	UserData.mAccumulatorVolt = 0;   //电瓶电压
    #endif
}

void updateCurTime()
{
 struct tm
 {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
};

	 struct tm CurTime={0};
	_getsystime(&CurTime);
	/*
	custom_data.time.ucDate = CurTime.tm_mday;
	custom_data.time.ucYear = CurTime.tm_year;
	custom_data.time.ucMonth = CurTime.tm_mon;
	custom_data.time.ucHour = CurTime.tm_hour;
	custom_data.time.ucMinute = CurTime.tm_min;
	custom_data.time.ucSecond = CurTime.tm_sec;
	*/

    UserData.mTime.ucDate = CurTime.tm_mday;
    UserData.mTime.ucYear = CurTime.tm_mday;
    UserData.mTime.ucMonth = CurTime.tm_mday;
    UserData.mTime.ucHour = CurTime.tm_mday;
    UserData.mTime.ucMinute = CurTime.tm_mday;
    UserData.mTime.ucSecond = CurTime.tm_mday;
}

STime_t* getCurrentTime1()
{
   updateCurTime();
   return &(UserData.mTime);

}

SysData_t* GetDailyData1(void)
{
	return &UserData;
}

const char digitalArray[10] = "0123456789";
static char StrCurTime[6] = {0};
extern STime_t * getCurrentTime1();

char * GetCurTimeString()
{
	updateCurTime();
	StrCurTime[0] = digitalArray[UserData.mTime.ucHour/10];
	StrCurTime[1] = digitalArray[UserData.mTime.ucHour%10];
	StrCurTime[2] =':';
	StrCurTime[3] = digitalArray[UserData.mTime.ucMinute/10];
	StrCurTime[4] = digitalArray[UserData.mTime.ucMinute%10];
	return StrCurTime;
}

#define DEF_DATE_YEAR   2024
#define DEF_DATE_MONTH  1
#define DEF_DATE_DAY    1

void InitSysDateTime(void)
{
    GetSysDateTime(DEF_DATE_YEAR, DEF_DATE_MONTH, DEF_DATE_DAY);
}


void GetSysDateTime(uint16_t year, uint8_t month, uint8_t day)
{
	UserData.mTime.ucYear = year;
	UserData.mTime.ucMonth = month;
	UserData.mTime.ucDate = day;
	UserData.mTime.ucHour = 0;
	UserData.mTime.ucMinute = 0;
	UserData.mTime.ucSecond = 0;
}

void UpdateSysTime(void)
{
    bool time_12_is_am=false;
	if(++UserData.mTime.ucSecond>=60)
	{
		UserData.mTime.ucSecond = 0;
		if(++UserData.mTime.ucMinute>=60)
		{
		    UserData.mTime.ucMinute = 0;
		    if(++UserData.mTime.ucHour>=12)
		    {
		        if(time_12_is_am)
		        {
		            UserData.mTime.ucDate++;
		            time_12_is_am=0;
		        }else{
		            time_12_is_am=1;
			      }
		        UserData.mTime.ucHour = 0;
		    }
		}
	}
	//UpdatePace();
	printf("%04d-%d%d-%d%d %d%d:%d%d.%d%d\n",
        UserData.mTime.ucYear, UserData.mTime.ucMonth/10, UserData.mTime.ucMonth%10,
        UserData.mTime.ucDate/10, UserData.mTime.ucDate%10,
        UserData.mTime.ucHour/10, UserData.mTime.ucHour%10,
        UserData.mTime.ucMinute/10, UserData.mTime.ucMinute%10,
        UserData.mTime.ucSecond/10, UserData.mTime.ucSecond%10);
}

#else if
//���ݳ�ʼ��
void system_date_init(void)
{

    custom_data.softwar_host = 0;
	custom_data.softwar_order = 1;

	custom_data.save_info.language = 0;
	custom_data.save_info.display_uint = 0;
	custom_data.save_info.theme = 0;
	custom_data.save_info.theme_state = 1;
	custom_data.save_info.light_value = 3;
	custom_data.save_info.bt_on_off = 1;
	custom_data.save_info.bt_state = 0;

    custom_data.veer = 0;
	custom_data.veer_hint = 0;
	custom_data.lamplight = 0;

	custom_data.power_on_self_test =0;

    if(custom_data.light_perception_value == 0)
		custom_data.light_perception_value=44;
    custom_data.light = 5;

    custom_data.return_demo = 0;

    //�ٶ� ת��
	custom_data.velocity = 0;
	custom_data.veer_velocity = 0;
	custom_data.grade = 0;
	custom_data.oil = 0;
	custom_data.temp = 0;
	custom_data.trip_mileage = 0;
	custom_data.total_mileage = 0;
    custom_data.voltage =0;

	custom_data.warning.engine = 0;
	custom_data.warning.machine_oil = 0;
	custom_data.warning.abs = 0;
	custom_data.warning.tcs = 0;
	custom_data.warning.fan = 0;
	custom_data.warning.null = 0;

	custom_data.now_defect_code = 0x2255;
	custom_data.his_defect_code = 0x400;

	custom_data.ttrb = 0;
	custom_data.ttrb_time = 10;
	custom_data.odo_reset = 10;
	custom_data.demo_position = 0;

	custom_data.plan = 0;
	custom_data.pace = 0;
	custom_data.upgrade = 0;

}

void updateCurTime()
{
 struct tm {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
};

	 struct tm CurTime={0};
	_getsystime(&CurTime);
	custom_data.time.ucDate = CurTime.tm_mday;
	custom_data.time.ucYear = CurTime.tm_year;
	custom_data.time.ucMonth = CurTime.tm_mon;
	custom_data.time.ucHour = CurTime.tm_hour;
	custom_data.time.ucMinute = CurTime.tm_min;
	custom_data.time.ucSecond = CurTime.tm_sec;
}

stTime_type * getCurrentTime()
{
   updateCurTime();
  return &(custom_data.time);

}

daily_data_t* GetDailyData(void)
{
	return &custom_data;
}

const char digitalArray[10] = "0123456789";
static char sCurTime[6] = {0};
extern stTime_type * getCurrentTime();

char * GetCurTimeString()
{
	updateCurTime();
	sCurTime[0] = digitalArray[custom_data.time.ucHour/10];
	sCurTime[1] = digitalArray[custom_data.time.ucHour%10];
	sCurTime[2] =':';
	sCurTime[3] = digitalArray[custom_data.time.ucMinute/10];
	sCurTime[4] = digitalArray[custom_data.time.ucMinute%10];
	return sCurTime;
}

#define DEF_DATE_YEAR   2024
#define DEF_DATE_MONTH  2
#define DEF_DATE_DAY    3

void InitSysDateTime(void)
{
    GetSysDateTime(DEF_DATE_YEAR, DEF_DATE_MONTH, DEF_DATE_DAY);
}


void GetSysDateTime(uint16_t year, uint8_t month, uint8_t day)
{
	custom_data.time.ucYear = year;
	custom_data.time.ucMonth = month;
	custom_data.time.ucDate= day;
	custom_data.time.ucHour=5;
	custom_data.time.ucMinute=59;
	custom_data.time.ucSecond=50 ;
}

void UpdateSysTime(void)
{
    bool time_12_is_am=false;
	if(++custom_data.time.ucSecond>=60)
	{
		custom_data.time.ucSecond = 0;
		if(++custom_data.time.ucMinute>=60)
		{
		    custom_data.time.ucMinute = 0;
		    if(++custom_data.time.ucHour>=12)
		    {
		        if(time_12_is_am)
		        {
		            custom_data.time.ucDate++;
		            time_12_is_am=0;
		        }else{
		            time_12_is_am=1;
			      }
		        custom_data.time.ucHour = 0;
		    }
		}
	}
	//UpdatePace();
	printf("%04d-%d%d-%d%d %d%d:%d%d.%d%d\n",
        custom_data.time.ucYear,custom_data.time.ucMonth/10,custom_data.time.ucMonth%10,
        custom_data.time.ucDate/10,custom_data.time.ucDate%10,
        custom_data.time.ucHour/10,custom_data.time.ucHour%10,
        custom_data.time.ucMinute/10,custom_data.time.ucMinute%10,
        custom_data.time.ucSecond/10,custom_data.time.ucSecond%10);
}

#endif



