#ifndef TAILSCALE_H
#define TAILSCALE_H

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

/*
 * c8-tailscale: the tailnet as the tailscale command reports it, and
 * the things the app and its tile do with it. Everything goes through
 * the command line client (status --json, debug prefs, set, up, down,
 * login, ...): the local API it speaks is not a stable interface, the
 * commands are.
 */

#define APP_ID "org.cont8nuum.Tailscale"
#define TILE_WIDGET_ID "tailscale" /* the id of the tile made from tailscale.widget */

enum ts_state {
	TS_NOT_INSTALLED, // no tailscale command
	TS_NO_DAEMON, // the command couldn't reach tailscaled
	TS_NEEDS_LOGIN,
	TS_NEEDS_MACHINE_AUTH, // an admin has to approve this device
	TS_STOPPED,
	TS_STARTING,
	TS_RUNNING,
};

struct ts_peer {
	char *id; // the stable node id
	char *name; // what the tailnet calls it: the first label of dns
	char *host; // the name its OS has
	char *dns; // the MagicDNS name, without the trailing dot
	char *os;
	char *owner; // who it belongs to, or "Tagged" devices' tag line
	char *ip4, *ip6;
	char *tags; // "tag:a, tag:b" or NULL
	char *relay; // the DERP region it reaches us through
	char *cur_addr; // the direct address in use, or NULL
	bool online, active;
	bool exit_node; // the one in use
	bool exit_option; // offers to be one
	bool mullvad; // one of the Mullvad exit nodes: not a device of ours
	char *country, *country_code, *city; // Mullvad nodes, else NULL
	int64_t last_seen; // unix seconds, 0 for never
	int64_t rx, tx; // bytes
};

struct ts_account {
	char *id, *tailnet, *account;
	bool selected;
};

struct ts_status {
	enum ts_state state;
	char *error; // why there is no daemon, when state is TS_NO_DAEMON
	char *version;
	char *auth_url; // where to log in, while a login is pending
	char **health; // warnings, NULL-terminated
	/* This device */
	char *self_name, *self_dns, *self_os, *self_ip4, *self_ip6, *self_relay;
	char *self_id;
	/* The account */
	char *user_login, *user_name, *tailnet, *magic_dns;
	bool can_mullvad; // the tailnet has Mullvad exit nodes
	/* Devices, online first then by name; Mullvad nodes at the end */
	GPtrArray *peers; // struct ts_peer
	/* The exit node in use, or NULL */
	char *exit_node_id;
	bool exit_node_online;
	/* Preferences */
	bool accept_routes, accept_dns, allow_lan, ssh, shields_up;
	bool advertise_exit;
	char *hostname; // the one set by hand, or NULL
	char *operator; // the user allowed to control tailscaled without root
	bool is_operator; // that's us (root is one too)
};

extern struct ts_status ts;

/* status.c */

/* Read everything again; fn runs when it's there (several asks while one
 * runs are one read) */
typedef void (*ts_change_fn)(void *data);
void ts_on_change(ts_change_fn fn, void *data);
void ts_refresh(void);
/* Read again every seconds (0 stops) */
void ts_poll(int seconds);

/* A command finished: out is its output, or what it said went wrong when
 * not ok (never NULL) */
typedef void (*ts_done_fn)(bool ok, const char *out, void *data);
/* Run the command; false if it couldn't be started (fn is not called) */
bool ts_run(const char *const *argv, ts_done_fn fn, void *data);
/* Run a tailscale subcommand that changes things: as ourselves when we
 * are the operator, else through pkexec. args is NULL-terminated after
 * the subcommand. Refreshes when done, then calls fn (may be NULL). */
bool ts_control(const char *const *args, ts_done_fn fn, void *data);
/* tailscale set --FLAG=VALUE */
bool ts_set(const char *flag, const char *value, ts_done_fn fn, void *data);
bool ts_set_bool(const char *flag, bool on, ts_done_fn fn, void *data);
/* Starts a login: the auth URL comes in the status (auth_url) */
bool ts_login(ts_done_fn fn, void *data);
bool ts_login_pending(void);
/* Lets the current user control tailscaled: pkexec tailscale set
 * --operator=USER */
bool ts_become_operator(ts_done_fn fn, void *data);

struct ts_peer *ts_peer_find(const char *id);
/* The exit node in use, or NULL */
struct ts_peer *ts_exit_node(void);
/* Devices of ours (not Mullvad nodes) online, and all of them */
void ts_count(int *online, int *total);
/* "just now", "5 min ago", "3 d ago"; "" for 0 */
char *ts_age(int64_t when);
/* "1.5 MB" */
char *ts_bytes(int64_t bytes);
/* The accounts logged in on this device; fn gets a GPtrArray of
 * struct ts_account (free it with ts_accounts_free), or NULL */
typedef void (*ts_accounts_fn)(GPtrArray *accounts, void *data);
bool ts_accounts(ts_accounts_fn fn, void *data);
void ts_accounts_free(GPtrArray *accounts);

/* shown.c: what is drawn of the above. In stealth mode (c8-base's
 * stealth.h) names, addresses, logins and tailnets are made-up ones,
 * kept for the program's life; off, the real ones. Never for commands. */
const char *ts_shown_self(void); // this device's name
const char *ts_shown_name(const struct ts_peer *p);
const char *ts_shown_host(const struct ts_peer *p);
const char *ts_shown_dns(const char *dns); // a MagicDNS name
const char *ts_shown_suffix(const char *suffix); // the MagicDNS suffix
const char *ts_shown_ip(const char *ip);
const char *ts_shown_endpoint(const char *addr); // "ip:port"
const char *ts_shown_owner(const char *who); // a display name or login
const char *ts_shown_login(const char *login); // an email-like login
const char *ts_shown_tailnet(const char *tailnet);
/* Command output and messages with all of the above swapped (g_free) */
char *ts_shown_text(const char *s);

/* tile.c: the live tile over org.cont8nuum.Tiles1 */
void ts_tile_update(const char *tile_id);
/* The next update is sent even if nothing changed */
void ts_tile_forget(void);

#endif
