

#include "buttons.h"

std::vector<c_button *> buttonlist;

c_button button1(SWITCH1, YELLOW, 1);
c_button button2(SWITCH2, RED, 2);
c_button button3(SWITCH3, GREEN, 3);
c_button button4(SWITCH12, YELLOW2, 4);
c_button button5(SWITCH22, RED2, 5);
c_button button6(SWITCH32, GREEN2, 6);

void buttonbegin(c_button *thisbutton)
{
  thisbutton->begin();
  buttonlist.push_back(thisbutton);
}

void initButtons(void)
{
  buttonbegin(&button1);
  buttonbegin(&button2);
  buttonbegin(&button3);
  buttonbegin(&button4);
  buttonbegin(&button5);
  buttonbegin(&button6);
}

void handleButtons(void)
{
  for (auto &&i : buttonlist)
  {
    i->read();
  }
}

bool anybutton(void)
{
  handleButtons();
  for (auto &&i : buttonlist)
  {
    if (i->isPressed())
    {
      return true;
    }
  }
  return false;
}

bool anyButtonChanged(void)
{
  handleButtons();
  for (auto &&i : buttonlist)
  {
    if (i->isChanged())
    {
      return true;
    }
  }
  return false;
}


bool buttonIsPressed(e_ledcolor index)
{
  c_button *thisbutton = getButton(index);
  if (thisbutton == NULL)
  {
    return false;
  }
  else
  {
    return thisbutton->isPressed();
  }
}

bool buttonIsPressed(uint16_t index)
{
  c_button *thisbutton = getButton(index);
  if (thisbutton == NULL)
  {
    return false;
  }
  else
  {
    return thisbutton->isPressed();
  }
}

bool buttonWasPressed(e_ledcolor index)
{
  c_button *thisbutton = getButton(index);
  if (thisbutton == NULL)
  {
    return false;
  }
  else
  {
    return thisbutton->wasPressed();
  }
}

c_button *getButton(unsigned int index)
{
  if (index > buttonlist.size())
  {
    return NULL;
  }
  return buttonlist[index - 1];
}

c_button *getButton(e_ledcolor color)
{
  for (auto &&button : buttonlist)
  {
    if (button->getColor() == color)
    {
      return button;
    }
  }
  return NULL;
}

std::vector<c_button *> *getButtonlist(void)
{
  return &buttonlist;
}