#pragma once

#include "quickjs.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

enum
{
	k_ssb_rpc_flag_binary = 0x0,
	k_ssb_rpc_flag_utf8 = 0x1,
	k_ssb_rpc_flag_json = 0x2,
	k_ssb_rpc_mask_type = 0x3,

	k_ssb_rpc_flag_end_error = 0x4,
	k_ssb_rpc_flag_stream = 0x8,

	k_ssb_blob_bytes_max = 5 * 1024 * 1024,
};

typedef enum _tf_ssb_change_t
{
	k_tf_ssb_change_create,
	k_tf_ssb_change_connect,
	k_tf_ssb_change_remove,
} tf_ssb_change_t;

typedef struct _tf_ssb_t tf_ssb_t;
typedef struct _tf_ssb_rpc_t tf_ssb_rpc_t;
typedef struct _tf_ssb_connection_t tf_ssb_connection_t;
typedef struct _tf_trace_t tf_trace_t;
typedef struct sqlite3 sqlite3;
typedef struct uv_loop_s uv_loop_t;
struct sockaddr_in;

enum {
	k_id_base64_len = 57,
	k_id_bin_len = 32,
	k_blob_id_len = 53,
};

typedef struct _tf_ssb_stats_t
{
	int connections;
	int broadcasts;
	int messages_stored;
	int blobs_stored;
	int rpc_in;
	int rpc_out;
	int request_count;
	struct
	{
		int rpc;
		int connections_changed;
		int message_added;
		int blob_want_added;
		int broadcasts_changed;
	} callbacks;
} tf_ssb_stats_t;

typedef struct _tf_ssb_blob_wants_t
{
	int32_t request_number;
	char last_id[k_blob_id_len];
	int wants_sent;
} tf_ssb_blob_wants_t;

tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, const char* db_path);
void tf_ssb_destroy(tf_ssb_t* ssb);

sqlite3* tf_ssb_acquire_db_reader(tf_ssb_t* ssb);
void tf_ssb_release_db_reader(tf_ssb_t* ssb, sqlite3* db);
sqlite3* tf_ssb_acquire_db_writer(tf_ssb_t* ssb);
void tf_ssb_release_db_writer(tf_ssb_t* ssb, sqlite3* db);
uv_loop_t* tf_ssb_get_loop(tf_ssb_t* ssb);

void tf_ssb_generate_keys(tf_ssb_t* ssb);
void tf_ssb_generate_keys_buffer(char* out_public, size_t public_size, char* out_private, size_t private_size);
void tf_ssb_get_private_key(tf_ssb_t* ssb, uint8_t* out_private, size_t private_size);

void tf_ssb_set_trace(tf_ssb_t* ssb, tf_trace_t* trace);
tf_trace_t* tf_ssb_get_trace(tf_ssb_t* ssb);

JSContext* tf_ssb_get_context(tf_ssb_t* ssb);

void tf_ssb_broadcast_listener_start(tf_ssb_t* ssb, bool linger);
void tf_ssb_broadcast_sender_start(tf_ssb_t* ssb);
void tf_ssb_run(tf_ssb_t* ssb);
void tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const uint8_t* private_key, JSValue message);
bool tf_ssb_whoami(tf_ssb_t* ssb, char* out_id, size_t out_id_size);

void tf_ssb_visit_broadcasts(tf_ssb_t* ssb, void (*callback)(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data), void* user_data);

const char** tf_ssb_get_connection_ids(tf_ssb_t* ssb);
int tf_ssb_get_connections(tf_ssb_t* ssb, tf_ssb_connection_t** out_connections, int out_connections_count);
void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key);
void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address);
void tf_ssb_server_open(tf_ssb_t* ssb, int port);
void tf_ssb_server_close(tf_ssb_t* ssb);

void tf_ssb_close_all(tf_ssb_t* ssb);
void tf_ssb_send_close(tf_ssb_t* ssb);

bool tf_ssb_id_str_to_bin(uint8_t* bin, const char* str);
bool tf_ssb_id_bin_to_str(char* str, size_t str_size, const uint8_t* bin);

bool tf_ssb_verify_and_strip_signature(JSContext* context, JSValue val, char* out_id, size_t out_id_size, char* out_signature, size_t out_signature_size, bool* out_sequence_before_author);
void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_id, size_t out_id_size);
bool tf_ssb_verify_strip_and_store_message(tf_ssb_t* ssb, JSValue value, bool* out_is_new);

bool tf_ssb_connection_is_client(tf_ssb_connection_t* connection);
const char* tf_ssb_connection_get_host(tf_ssb_connection_t* connection);
int tf_ssb_connection_get_port(tf_ssb_connection_t* connection);
tf_ssb_t* tf_ssb_connection_get_ssb(tf_ssb_connection_t* connection);
JSContext* tf_ssb_connection_get_context(tf_ssb_connection_t* connection);
sqlite3* tf_ssb_connection_get_db(tf_ssb_connection_t* connection);
void tf_ssb_connection_close(tf_ssb_connection_t* connect);
bool tf_ssb_connection_is_connected(tf_ssb_connection_t* connection);

int32_t tf_ssb_connection_next_request_number(tf_ssb_connection_t* connection);

tf_ssb_connection_t* tf_ssb_connection_get(tf_ssb_t* ssb, const char* id);
bool tf_ssb_connection_get_id(tf_ssb_connection_t* connection, char* out_id, size_t out_id_size);
JSValue tf_ssb_connection_get_object(tf_ssb_connection_t* connection);

/* Callbacks. */
typedef void (tf_ssb_callback_cleanup_t)(tf_ssb_t* ssb, void* user_data);
typedef void (tf_ssb_connections_changed_callback_t)(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data);
void tf_ssb_add_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_connections_changed_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data);
void tf_ssb_remove_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_connections_changed_callback_t* callback, void* user_data);

typedef void (tf_ssb_broadcasts_changed_callback_t)(tf_ssb_t* ssb, void* user_data);
void tf_ssb_add_broadcasts_changed_callback(tf_ssb_t* ssb, tf_ssb_broadcasts_changed_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data);
void tf_ssb_remove_broadcasts_changed_callback(tf_ssb_t* ssb, tf_ssb_broadcasts_changed_callback_t* callback, void* user_data);

typedef void (tf_ssb_message_added_callback_t)(tf_ssb_t* ssb, const char* id, void* user_data);
void tf_ssb_add_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data);
void tf_ssb_remove_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_callback_t* callback, void* user_data);
void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id);
void tf_ssb_notify_blob_stored(tf_ssb_t* ssb, const char* id);

typedef void (tf_ssb_blob_want_added_callback_t)(tf_ssb_t* ssb, const char* id, void* user_data);
void tf_ssb_add_blob_want_added_callback(tf_ssb_t* ssb, tf_ssb_blob_want_added_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data);
void tf_ssb_remove_blob_want_added_callback(tf_ssb_t* ssb, tf_ssb_blob_want_added_callback_t* callback, void* user_data);
void tf_ssb_notify_blob_want_added(tf_ssb_t* ssb, const char* id);

typedef void (tf_ssb_rpc_callback_t)(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data);
void tf_ssb_add_rpc_callback(tf_ssb_t* ssb, const char** name, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data);
void tf_ssb_remove_rpc_callback(tf_ssb_t* ssb, const char** name, tf_ssb_rpc_callback_t* callback, void* user_data);

void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const uint8_t* message, size_t size, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data);
void tf_ssb_connection_rpc_send_json(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue message, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data);
void tf_ssb_connection_rpc_send_error(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const char* error);
void tf_ssb_connection_rpc_send_error_method_not_allowed(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const char* name);
void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t request_number, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data, tf_ssb_connection_t* dependent_connection);
void tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, int32_t request_number);

typedef void (tf_ssb_scheduled_callback_t)(tf_ssb_connection_t* connection, void* user_data);
void tf_ssb_connection_schedule_idle(tf_ssb_connection_t* connection, tf_ssb_scheduled_callback_t* callback, void* user_data);

void tf_ssb_connection_add_new_message_request(tf_ssb_connection_t* connection, const char* author, int32_t request_number, bool keys);
void tf_ssb_connection_remove_new_message_request(tf_ssb_connection_t* connection, const char* author);

bool tf_ssb_connection_is_attendant(tf_ssb_connection_t* connection);
int32_t tf_ssb_connection_get_attendant_request_number(tf_ssb_connection_t* connection);
void tf_ssb_connection_set_attendant(tf_ssb_connection_t* connection, bool attendant, int request_number);
void tf_ssb_connection_clear_room_attendants(tf_ssb_connection_t* connection);
void tf_ssb_connection_add_room_attendant(tf_ssb_connection_t* connection, const char* id);
void tf_ssb_connection_remove_room_attendant(tf_ssb_connection_t* connection, const char* id);

tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_t* ssb, const char* portal_id, int32_t request_number, const char* target_id);

int32_t tf_ssb_connection_get_ebt_request_number(tf_ssb_connection_t* connection);
void tf_ssb_connection_set_ebt_request_number(tf_ssb_connection_t* connection, int32_t request_number);
JSValue tf_ssb_connection_get_ebt_send_clock(tf_ssb_connection_t* connection);
void tf_ssb_connection_set_ebt_send_clock(tf_ssb_connection_t* connection, JSValue send_clock);
bool tf_ssb_connection_get_sent_clock(tf_ssb_connection_t* connection);
void tf_ssb_connection_set_sent_clock(tf_ssb_connection_t* connection, bool sent_clock);

JSClassID tf_ssb_get_connection_class_id();

void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats);

tf_ssb_blob_wants_t* tf_ssb_connection_get_blob_wants_state(tf_ssb_connection_t* connection);

JSValue tf_ssb_get_disconnection_debug(tf_ssb_t* ssb, JSContext* context);

void tf_ssb_record_thread_time(tf_ssb_t* ssb, int64_t thread_id, uint64_t hrtime);
uint64_t tf_ssb_get_average_thread_time(tf_ssb_t* ssb);

void tf_ssb_set_hitch_callback(tf_ssb_t* ssb, void (*callback)(const char* name, uint64_t duration_ns, void* user_data), void* user_data);
