/*
 * Stairs
 * Copyright 2017 (c) Mark van Renswoude
 *
 * https://git.x2software.net/pub/Stairs
*/
#ifndef __settingsconnection
#define __settingsconnection

#include <stdint.h>
#include <stdbool.h>
#include <IPAddress.h>
#include "./abstractjson.h"
#include "../charproperties.h"
#include "../config.h"


enum ConnectionSettingsFlags
{
  AccessPoint = 1,
  StationMode = 2,
  DHCP = 4
};


class ConnectionSettings : public AbstractJsonSettings
{
  private:
    char* mHostname = nullptr;
    uint8_t mFlags = AccessPoint | DHCP;
    char* mSSID = nullptr;
    char* mPassword = nullptr;
    IPAddress mIP = (uint32_t)0;
    IPAddress mSubnetMask = (uint32_t)0;
    IPAddress mGateway = (uint32_t)0;

  protected:
    virtual const char* getFilename() { return ConnectionSettingsFile; };
    virtual const char* getDebugPrefix() { return "ConnectionSettings"; };

  public:
    void toJson(Print &print);
    bool fromJson(char* data, bool* changed);


    char* hostname() { return mHostname; }
    void hostname(const char* value) { assignChar(&mHostname, value); }

    bool flag(ConnectionSettingsFlags flag) { return (mFlags & flag) != 0; }
    void flag(ConnectionSettingsFlags flag, bool enabled)
    {
      if (enabled)
        mFlags |= flag;
      else
        mFlags &= ~flag;
    }

    char* ssid() { return mSSID; }
    void ssid(const char* value) { assignChar(&mSSID, value); }

    char* password() { return mPassword; }
    void password(const char* value) { assignChar(&mPassword, value); }

    IPAddress ip() { return mIP; }
    void ip(IPAddress value) { mIP = value; }

    IPAddress subnetMask() { return mSubnetMask; }
    void subnetMask(IPAddress value) { mSubnetMask = value; }

    IPAddress gateway() { return mGateway; }
    void gateway(IPAddress value) { mGateway = value; }
};

#endif