#ifndef Names_h
#define Names_h

#include "Arduino.h"
#include "FS.h"
#include <ArduinoJson.h>
#include "LinkedList.h"
#include "language.h"

#define NAME_LIST_SIZE 25
#define NAME_MAX_LENGTH 16

extern void checkFile(String path, String data);
extern JsonVariant parseJSONFile(String path, DynamicJsonBuffer &jsonBuffer);
extern bool writeFile(String path, String &buf);
extern bool appendFile(String path, String &buf);
extern bool strToMac(String macStr, uint8_t* mac);
extern String searchVendor(uint8_t* mac);
extern String fixUtf8(String str);
extern String buildString(String left, String right, int maxLen);
extern String escape(String str);

class Names {
  public:
    Names();
    
    void load();
    void load(String filepath);
    void save(bool force);
    void save(bool force,String filepath);
    void sort();
    
    String find(uint8_t* mac);
    int findID(uint8_t* mac);

    void print(int num);
    void print(int num, bool header, bool footer);
    void select(int num);
    void select(String name);
    void deselect(int num);
    void deselect(String name);
    void add(uint8_t* mac, String name, uint8_t* bssid, uint8_t ch, bool selected, bool force);
    void add(String macStr, String name, String bssidStr, uint8_t ch, bool selected, bool force);
    void replace(int num, String macStr, String name, String bssidStr, uint8_t ch, bool selected);
    void remove(int num);
    
    void printAll();
    void printSelected();
    void selectAll();
    void deselectAll();
    void removeAll();
    
    uint8_t* getMac(int num);
    uint8_t* getBssid(int num);
    String getMacStr(int num);
    String getBssidStr(int num);
    String getName(int num);
    String getVendorStr(int num);
    String getSelectedStr(int num);
    uint8_t getCh(int num);
    bool getSelected(int num);
    bool isStation(int num);

    void setName(int num, String name);
    void setMac(int num, String macStr);
    void setCh(int num, uint8_t ch);
    void setBSSID(int num, String bssidStr);
    
    int count();
    int selected();

    bool check(int num);
  private:
    String FILE_PATH = "/names.json";
    bool changed = false;
  
    struct Device{
      uint8_t* mac;     // mac address
      char* name;       // name of saved device
      uint8_t* apBssid; // mac address of AP (if saved device is a station)
      uint8_t ch;       // Wi-Fi channel of Device
      bool selected;    // select for attacking
    };
    
    LinkedList<Device>* list;
    
    int binSearch(uint8_t* searchBytes, int lowerEnd, int upperEnd);
    bool internal_check(int num);
    void internal_select(int num);
    void internal_deselect(int num);
    void internal_add(uint8_t* mac, String name, uint8_t* bssid, uint8_t ch, bool selected);
    void internal_add(String macStr, String name, String bssidStr, uint8_t ch, bool selected);
    void internal_remove(int num);
    void internal_removeAll();
};

#endif



