/// © MiroZ 2024

#ifndef __SETTINGS_H__
#define __SETTINGS_H__

#include <stdint.h>

#pragma pack(push, 1)

struct SETTINGS_LEGACY
{
	char WIFI_SSID[32];
	char WIFI_PASS[32];
	uint8_t ID[3];
	uint16_t GROUP_ID;
	uint16_t THR_UP_PRESSURE;
	uint16_t THR_UP_RADAR;
	uint16_t THR_UP_LIGHT;
	uint16_t THR_UP_TEMPERATURE;
	uint16_t THR_UP_HUMIMDITY;
	uint16_t THR_UP_CO2;
	uint16_t THR_UP_VOC;
	uint16_t THR_DN_PRESSURE;
	uint16_t THR_DN_RADAR;
	uint16_t THR_DN_LIGHT;
	uint16_t THR_DN_TEMPERATURE;
	uint16_t THR_DN_HUMIMDITY;
	uint16_t THR_DN_CO2;
	uint16_t THR_DN_VOC;
	uint8_t LPF_PRESSURE;
	uint8_t LPF_RADAR;
	uint8_t LPF_LIGHT;
	uint8_t LPF_TEMPERATURE;
	uint8_t LPF_HUMIMDITY;
	uint8_t LPF_CO2;
	uint8_t LPF_VOC;
	uint8_t KLPRN_PRESSURE;
	uint8_t KLPRN_RADAR;
	uint8_t KLPRN_LIGHT;
	uint8_t KLPRN_TEMPERATURE;
	uint8_t KLPRN_HUMIMDITY;
	uint8_t KLPRN_CO2;
	uint8_t KLPRN_VOC;
	uint8_t KLSN_PRESSURE;
	uint8_t KLSN_RADAR;
	uint8_t KLSN_LIGHT;
	uint8_t KLSN_TEMPERATURE;
	uint8_t KLSN_HUMIMDITY;
	uint8_t KLSN_CO2;
	uint8_t KLSN_VOC;
	uint8_t KLEE_PRESSURE;
	uint8_t KLEE_RADAR;
	uint8_t KLEE_LIGHT;
	uint8_t KLEE_TEMPERATURE;
	uint8_t KLEE_HUMIMDITY;
	uint8_t KLEE_CO2;
	uint8_t KLEE_VOC;
	uint8_t FS_RATIO;
	uint8_t NC_REP_PERIOD;
	uint16_t BT_SCN_EVERY;
	uint8_t BT_SCN_TIME_LENGTH;
	uint8_t ENC_KEY[16];
	uint8_t LED_SCHEMA[10];
	uint8_t MQTT_ADDR[4];
	uint16_t MQTT_PORT;
	uint8_t REPORTING_PERIOD;
	uint8_t QUEUE[512];
	char WIFI_SSID2[32];
	char WIFI_PASS2[32];
	uint32_t TEMP_OFFSET;
};

#define SETTINGS_VERSION			3	// cannot be 0
#define SETTINGS_NUM_WIFI_ENTRIES	8

// wifi definitions

struct WIFI_ENTRY
{
	char ssid[33];
	char pwd[33];
};

struct WIFI
{
	uint8_t num;
	uint8_t always_scan_before_connect;
	struct WIFI_ENTRY entry[SETTINGS_NUM_WIFI_ENTRIES];
	uint8_t selected;

	// network stuff
	uint32_t subnet_mask;
	uint32_t dns_primary;
	uint32_t dns_secondary;
};

//

struct LED
{
	uint8_t brightness;
};

//

// general device level settings
struct DEVICE
{
	uint32_t id;
	uint32_t group_id;
};

//

struct NV_DATA
{
	uint32_t version;
	struct LED led;
	struct WIFI wifi;
	struct DEVICE device;
};

#pragma pack(pop)

class Settings
{
protected:
	const char * m_namespace = "nv_data";
	const char * m_dataname = "data_01";

	struct NV_DATA * m_data;
	void setDefaults();

private:
	Settings();
	
public:
	static Settings & getInstance();

public:
	inline struct NV_DATA & data() {return * m_data;};
	uint32_t loadData();
	uint32_t saveData();
};

#define SETTINGS Settings::getInstance().data()
#define SETTINGS_SAVE Settings::getInstance().saveData()

#endif /* __SETTINGS_H__ */