#ifndef ANET_COMMON_H
#define ANET_COMMON_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#ifndef _WIN32
#include <strings.h>
#endif

#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <conio.h>
#pragma comment(lib, "ws2_32.lib")
typedef SOCKET socket_t;
#define CLOSESOCK closesocket
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
typedef int socket_t;
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#define CLOSESOCK close
#endif

#define ANET_VERSION "CleanSource 1.0"
#define IRC_RECV_BUF 8192
#define IRC_LINE_MAX 1024
#define UI_MAX_LINES 1200
#define UI_MAX_USERS 256
#define UI_MAX_INPUT 512
#define CFG_MAX_BOOKMARKS 64

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
#endif

enum {
    KEY_NONE = 0,
    KEY_ESC = 27,
    KEY_ENTER = 1001,
    KEY_UP,
    KEY_DOWN,
    KEY_LEFT,
    KEY_RIGHT,
    KEY_PGUP,
    KEY_PGDN,
    KEY_BACKSPACE,
    KEY_DELETE,
    KEY_TAB,
    KEY_F1,
    KEY_F2,
    KEY_F3,
    KEY_F4
};

typedef struct {
    int width;
    int height;
} screen_size_t;

void msleep(int ms);
void trim(char *s);
void safe_strcpy(char *dst, const char *src, size_t n);
int starts_with(const char *s, const char *prefix);
void format_time(char *out, size_t n, time_t t);
int file_exists(const char *path);
screen_size_t get_screen_size(void);
int init_platform(void);
void shutdown_platform(void);

#endif
