#pragma once
#include "esp_err.h"
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    uint8_t year;   // 0-99
    uint8_t month;  // 1-12
    uint8_t day;    // 1-31
    uint8_t hour;   // 0-23
    uint8_t minute; // 0-59
    uint8_t second; // 0-59
    uint8_t weekday;// 0-6
} rtc_time_t;

// RTC初始化
esp_err_t rtc_aip8563_init(void);
// 读取RTC时间
esp_err_t rtc_aip8563_get_time(rtc_time_t *time);
// 设置RTC时间
esp_err_t rtc_aip8563_set_time(const rtc_time_t *time);


// 通过IIC获取当前时间（返回0成功，非0失败）
int rtc_get_time(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *hour, uint8_t *minute, uint8_t *second, uint8_t *weekday);
// 通过IIC设置当前时间（返回0成功，非0失败）
int rtc_set_time(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint8_t weekday);

// LVGL应用层专用API
int get_rtc_get_time(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *hour, uint8_t *minute, uint8_t *second, uint8_t *weekday);
int set_rtc_set_time(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint8_t weekday);

// 获取是否需要写入RTC的标志,仅供main.c调用
bool rtc_need_write_flag(void);
int rtc_write_pending_time(void);

// 自动计算星期几的函数（根据年月日计算weekday，0=周日，1=周一...6=周六）
uint8_t rtc_calculate_weekday(uint8_t year, uint8_t month, uint8_t day);

// 自动设置时间（无需手动传入weekday，会自动计算）
int rtc_set_time_auto_weekday(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second);

// 测试函数：验证weekday计算的正确性
void rtc_test_weekday_calculation(void);

#ifdef __cplusplus
}
#endif 