#ifndef PLATFORM_H
#define PLATFORM_H

#include "type.h"  // 基础类型（bool8、uint32等）

// ==============================================================================


// ===================== 跨平台兼容：Windows/嵌入式/Linux 通用 =====================
// 适配嵌入式/Linux：未定义_WIN32时，映射Windows关键字为原生C标准类型
#define PLATFORM_THREAD_INVALID NULL
#define PLATFORM_SEM_INVALID NULL
#define PLATFORM_TIMER_INVALID NULL
#define PLATFORM_MUTEX_INVALID NULL

// -------------------------- 类型定义 --------------------------
// 线程类型与函数指针
typedef void* PlatformThread;
typedef void (*PlatformThreadFunc)(void*);

// 互斥锁类型
typedef void* PlatformMutex;

// 信号量类型（通用句柄）
typedef void* PlatformSem;

// 套接字类型
typedef int32_xxl PlatformSocket;
#define PLATFORM_SOCKET_INVALID -1


// 定时器类型与函数指针
typedef void* PlatformTimer;
typedef void (*PlatformTimerFunc)(void*);  // 修正函数指针命名

// 时间类型（统一用64位表示时间戳）
typedef uint64_xxl PlatformTime;

// 内存分配标记
typedef enum {
    PLATFORM_MEM_DEFAULT,    // 默认分配
    PLATFORM_MEM_SECURE      // 安全内存
} PlatformMemType;

// -------------------------- 通用函数声明（跨平台接口） --------------------------
// 线程接口
PlatformThread platform_thread_create(PlatformThreadFunc func, void* arg);
void platform_thread_join(PlatformThread thread);
void platform_thread_detach(PlatformThread thread);
uint32_xxl platform_thread_id(void);

// 互斥锁接口
PlatformMutex platform_mutex_create(void);
void platform_mutex_lock(PlatformMutex mutex);
void platform_mutex_unlock(PlatformMutex mutex);
void platform_mutex_destroy(PlatformMutex mutex);
uint32_xxl platform_strlen(const char* str);

// 信号量接口
PlatformSem platform_sem_create(int initial_count, int max_count);
void platform_sem_destroy(PlatformSem sem);
//int32_xxl platform_sem_post(PlatformSem sem);
int32_xxl platform_sem_timedwait(PlatformSem sem, uint32_xxl timeout_ms);


// 平台初始化/反初始化
//bool_xxl platform_init(void);
//void platform_deinit(void);

// 网络接口（UDP）
PlatformSocket net_udp_socket_create(void);
//bool_xxl platform_udp_bind(PlatformSocket sock, uint16_xxl port);
//int32_xxl net_udp_sendto(PlatformSocket sock, const char* ip, uint16_xxl port,
//                         const uint8_xxl* data, uint32_xxl len);
//int32_xxl platform_udp_recvfrom(PlatformSocket sock, uint8_xxl* buf, uint32_xxl max_len,
//                           char* src_ip, uint16_xxl* src_port, uint32_xxl timeout_ms);
//void net_socket_close(PlatformSocket sock);
//bool_xxl platform_check_port_in_use(uint16_xxl port);

// 时间接口
uint32_xxl platform_get_tick_ms(void);
uint64_xxl platform_get_tick_us(void);
PlatformTime platform_get_utc_seconds(void);
PlatformTime platform_get_utc_ms(void);
void platform_delay_ms(uint32_xxl ms);

// 定时器接口
PlatformTimer platform_timer_create(PlatformTimerFunc func, void* arg, uint32_xxl period_ms);
bool_xxl platform_timer_start(PlatformTimer timer);
bool_xxl platform_timer_stop(PlatformTimer timer);
void platform_timer_destroy(PlatformTimer timer);

// 内存接口
void* platform_malloc(uint32_xxl size);
void* platform_calloc(uint32_xxl num, uint32_xxl size);
//void* platform_malloc_aligned(uint32_xxl size, uint32_xxl align);

void platform_free(void* ptr);
void platform_free_aligned(void* ptr);

// 随机数接口
bool_xxl platform_rand_generate(uint8_xxl* buf, uint32_xxl len);
bool_xxl platform_rand_generate_crypto(uint8_xxl* buf, uint32_xxl len);

bool_xxl platform_file_exists(const char* path);
uint32_xxl platform_get_file_size(const char* path);

// 字符串/内存操作
uint32_xxl platform_memcpy_s(void* dest, uint32_xxl dest_max_len, const void* src, uint32_xxl src_len);
uint32_xxl platform_memset_s(void* dest, uint32_xxl dest_max_len, uint8_xxl value, uint32_xxl set_len);
uint32_xxl platform_strcpy_s(char* dest, uint32_xxl dest_max_len, const char* src);
int32_xxl platform_strcmp(const char* str1, const char* str2);
char* platform_strchr(const char* str, int32_xxl ch);
// ========== 新增：字符串查找接口（解决LNK2019错误） ==========
/**
 * @brief 跨平台字符串查找（等效于标准库strstr）
 * @param str 源字符串
 * @param substr 要查找的子字符串
 * @return 找到返回子串起始指针，未找到返回NULL
 */
char* platform_strstr(const char* str, const char* substr);
uint32_xxl platform_strcat_s(char* dest, uint32_xxl dest_max_len, const char* src);
uint32_xxl platform_be32_to_cpu(const uint8_xxl* in);
void platform_cpu_to_be32(uint8_xxl* out, uint32_xxl value);

// -------------------------- Windows 平台特有实现声明（仅补充） --------------------------
//#ifdef WIN32
// 线程实现
uint32_xxl platform_thread_id_win32(void);
PlatformThread platform_thread_create_win32(PlatformThreadFunc func, void* arg);
void platform_thread_join_win32(PlatformThread thread);
void platform_thread_detach_win32(PlatformThread thread);

// 互斥锁实现
PlatformMutex platform_mutex_create_win32(void);
void platform_mutex_lock_win32(PlatformMutex mutex);
void platform_mutex_unlock_win32(PlatformMutex mutex);
void platform_mutex_destroy_win32(PlatformMutex mutex);
uint32_xxl platform_strlen_win32(const char* str);

// 信号量实现
PlatformSem platform_sem_create_win32(int initial_count, int max_count);
void platform_sem_destroy_win32(PlatformSem sem);
int32_xxl platform_sem_post_win32(PlatformSem sem);
int32_xxl platform_sem_timedwait_win32(PlatformSem sem, uint32_xxl timeout_ms);
int32_xxl platform_memcmp(const void* buf1, const void* buf2, uint32_xxl len);

// 平台初始化实现
bool_xxl platform_init_win32(void);
void platform_deinit_win32(void);
int32_xxl platform_udp_recvfrom_win32(PlatformSocket sock, uint8_xxl* buf, uint32_xxl max_len,
                                char* src_ip, uint16_xxl* src_port, uint32_xxl timeout_ms);
bool_xxl platform_check_port_in_use_win32(uint16_xxl port);

// 时间接口实现
uint32_xxl platform_get_tick_ms_win32(void);
uint64_xxl platform_get_tick_us_win32(void);
PlatformTime platform_get_utc_seconds_win32(void);
PlatformTime platform_get_utc_ms_win32(void);
//void platform_delay_ms_win32(uint32_xxl ms);

// 定时器实现（核心：明确声明避免类型截断）
PlatformTimer platform_timer_create_win32(PlatformTimerFunc func, void* arg, uint32_xxl period_ms);
bool_xxl platform_timer_start_win32(PlatformTimer timer);
bool_xxl platform_timer_stop_win32(PlatformTimer timer);
void platform_timer_destroy_win32(PlatformTimer timer);

// 内存接口实现
void* platform_malloc_win32(uint32_xxl size);
//void* platform_calloc_win32(uint32_xxl num, uint32_xxl size);
//void* platform_malloc_aligned_win32(uint32_xxl size, uint32_xxl align, PlatformMemType type);
void platform_free_win32(void* ptr);
void platform_free_aligned_win32(void* ptr);

// 随机数接口实现
bool_xxl platform_rand_generate_win32(uint8_xxl* buf, uint32_xxl len);
bool_xxl platform_rand_generate_crypto_win32(uint8_xxl* buf, uint32_xxl len);

// 文件系统接口实现
bool_xxl platform_file_exists_win32(const char* path);
uint32_xxl platform_get_file_size_win32(const char* path);
uint32_xxl platform_get_runtime_filename_win32(char* buf, uint32_xxl buf_len);

// 字符串/内存操作实现
uint32_xxl platform_memcpy_s_win32(void* dest, uint32_xxl dest_max_len, const void* src, uint32_xxl src_len);
uint32_xxl platform_memset_s_win32(void* dest, uint32_xxl dest_max_len, uint8_xxl value, uint32_xxl set_len);
uint32_xxl platform_strcpy_s_win32(char* dest, uint32_xxl dest_max_len, const char* src);
int32_xxl platform_strcmp_win32(const char* str1, const char* str2);
char* platform_strchr_win32(const char* str, int32_xxl ch);


//#endif  // WIN32

#endif  // PLATFORM_H
