#ifndef __CLI_H
#define __CLI_H

#include <stdint.h>
#include <stdbool.h>
#include "FreeRTOS.h"
#include "fr_timers.h"
#include "settings_api.h"
#include "log.h"

#if defined TELNET_ENABLE || defined SSH_ENABLE
#define CLI_ENABLE
#endif

/* Dimensions the buffer into which input characters are placed. */
#define cmdMAX_INPUT_SIZE			144
#define MAX_SESSIONS	5

#define array_len(x) (sizeof(x)/sizeof(x[0]))

typedef enum{
	CLI_AUTH = 0,
	CLI_AUTH_PASSW,
	CLI_CMD,
	CLI_CHANGE_PWD,
	CLI_CHANGE_PWD_ACK
} input_state_t;

typedef enum {
	STATE_UNUSED,
	STATE_NORMAL,
	STATE_CLOSE,
} conn_state_t;

/**
 * A cli connection structure.
 */
typedef struct {
	TimerHandle_t RepeatSensorInfoTimer;
	conn_state_t state;
	char buf[cmdMAX_INPUT_SIZE];
	uint_fast8_t bufptr;
	char prev_cmd[cmdMAX_INPUT_SIZE];
	uint8_t optlen;
	input_state_t input_state;
	user_level_t user_id;
	user_level_t id_change_pwd;
	char login[MAX_WEB_LOGIN_LEN];
	uint8_t login_err;	// the number of failed password entry attempts
	bool flag_telnet_ip_option;	// wtf is this
	void (*send)(intptr_t fd, const char *str, unsigned len);
	intptr_t num_connect;	// fd
} cli_state_t;

extern cli_state_t cli_states[MAX_SESSIONS];
extern const char pcWarningMessage[];
extern const unsigned pcWarningMessageLen;

void cli_init(void);
void cli_getchar(cli_state_t *s, char incoming_char, bool echo_enabled);
cli_state_t *alloc_state(void);
void free_state(cli_state_t *state);
void cli_hello(cli_state_t *cli_state);
void cli_save_config(cli_state_t *cli_state);
user_level_t cli_auth_user(const char *user, const char *password, log_type_t log_entry_type);
void cli_close_connections(void);

#endif
