#ifndef CONFIG_HEADER
#define CONFIG_HEADER
#include "stdint.h"
#include "string.h"
#include "meshcore/packetstructs.h"

#define FLASH_USER_PAGE_ADDR ((const void *)(0x08077F00))

#define ChannelCount 8
//#define CONTACT_COUNT 100
#define CONTACT_COUNT 16

#define VERSION "v0.0.1 - BRN Systems RISC-V"

#define BOARD "WCH CH32V307"

typedef struct LoRaSettings {
    int8_t txPowerInDbm;
    uint32_t frequencyInHz;
    uint8_t spreadingFactor;
    uint8_t bandwidth;
    uint8_t codingRate;
    uint16_t preambleLength;
    uint16_t tcxoVoltage;
} LoRaSettings;

extern LoRaSettings currentLoRaSettings;

typedef struct {
    uint32_t magic;       // e.g. 0xDEADBEEF

    uint8_t privkey[32];  // Ed25519 private
    uint8_t pubkey[32];   // Ed25519 public

    uint8_t nodeType;
    char nodeName[32];

    int32_t latitude;
    int32_t longitude;
    int32_t altitude;

    LoRaSettings loraSettings;

    Channel channels[ChannelCount];
    NodeEntry contacts[CONTACT_COUNT];

    uint8_t password[16];
    uint8_t guestPassword[16];

    uint8_t doRepeat;
    uint8_t allowReadOnly;
    uint16_t adcMultiplier;
    uint16_t airtimeFactor;
    uint32_t crc32;  // integrity check
} PersistentData_t;

extern PersistentData_t persistent;

void saveConfig();
void loadConfig();

void printNodeDB();

NodeEntry *getNextNode();

NodeEntry *getNode (uint8_t hash);

Channel *getChannel (uint8_t hash, uint8_t ignoreCount);

void addChannel (char *name, const uint8_t *key);

NodeEntry *getNodePrefix (const uint8_t *hash);

const char *getStringRole (uint8_t role);

void populateDefaults();

void LoraApply();

#endif