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

#include <Arduino.h>
#include <TimeLib.h>
#include <Dusk2Dawn.h>
#include "../../config.h"
#include "../abstractjson.h"

enum DayOfWeek
{
  Monday = 1,
  Tuesday = 2,
  Wednesday = 4,
  Thursday = 8,
  Friday = 16,
  Saturday = 32,
  Sunday = 64
};


extern timeDayOfWeek_t toTimeDayOfWeek(DayOfWeek day);
extern DayOfWeek toDayOfWeek(timeDayOfWeek_t timeDay);


enum TimeTriggerType
{
  FixedTime = 0,
  RelativeToSunrise = 1,
  RelativeToSunset = 2
};


struct TimeTrigger
{
  int16_t time;
  uint8_t daysOfWeek;
  uint8_t brightness;
  TimeTriggerType triggerType;
  bool enabled;
};


class TimeTriggerSettings : public AbstractJsonSettings
{
  private:
    bool mEnabled = false;
    uint16_t mTransitionTime = 0;
    uint8_t mTriggerCount = 0;
    TimeTrigger* mTriggers = nullptr;

  protected:
    virtual const char* getFilename() { return TimeTriggerSettingsFile; };
    virtual const char* getDebugPrefix() { return "TimeTriggerSettings"; };

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


    TimeTrigger* getActiveTrigger(tmElements_t &time);


    bool enabled() { return mEnabled; }
    void enabled(bool value) { mEnabled = value; }

    uint16_t transitionTime() { return mTransitionTime; }
    void transitionTime(uint16_t value) { mTransitionTime = value; }

    uint8_t triggerCount() { return mTriggerCount; }
    TimeTrigger* trigger(uint8_t index) { return &mTriggers[index]; }
};

#endif