#include "my_buttons.h"
#include "global.h"
#include "BLE_UpdateService.h"

static const char* tag = "button";
OneButton *boardButtons[3];

void Init_ButtonEvents(int8_t (&pin)[3]){ 


  if (pin[0] >= 0) {
    if (boardButtons[0] == nullptr) {
      boardButtons[0] = new OneButton(pin[0], true, true);
      ESP_LOGI(tag, "Button1 Events, pin=%d", pin[0]);
    } else {
      ESP_LOGW(tag, "Button1 already initialized (pin=%d)", pin[0]);
    }
  }
  if (pin[1] >= 0) {
    if (boardButtons[1] == nullptr) {
      boardButtons[1] = new OneButton(pin[1], true, true);
      ESP_LOGI(tag, "Button2 Events, pin=%d", pin[1]);
    } else {
      ESP_LOGW(tag, "Button2 already initialized (pin=%d)", pin[1]);
    }
  }
  if (pin[2] >= 0) {
    if (boardButtons[2] == nullptr) {
      boardButtons[2] = new OneButton(pin[2], true, false);
      ESP_LOGI(tag, "Button3 Events, pin=%d", pin[2]);
    } else {
      ESP_LOGW(tag, "Button3 already initialized (pin=%d)", pin[2]);
    }
  }

  /*
  if(boardButtons[0] != NULL){
    //boardButtons[0]->setDebounceTicks(DEBOUCE_TIME); // just below the update period to guarantee 1 sample delay
    boardButtons[0]->attachClick(btn1_click);
    boardButtons[0]->attachDoubleClick(btn1_doubleClick);
    boardButtons[0]->attachLongPressStart(btn1_LongPressStart);
    boardButtons[0]->attachLongPressStop(btn1_LongPressStop);
    boardButtons[0]->attachDuringLongPress(btn1_DuringLongPress);
  }
  else {
    ESP_LOGD(tag, "Button1 Not Initialized");
  }

  if(boardButtons[1] != NULL){
    //boardButtons[1]->setDebounceTicks(DEBOUCE_TIME);
    boardButtons[1]->attachClick(btn2_click);
    boardButtons[1]->attachDoubleClick(btn2_doubleClick);
    boardButtons[1]->attachLongPressStart(btn2_LongPressStart);
    boardButtons[1]->attachLongPressStop(btn2_LongPressStop);
    boardButtons[1]->attachDuringLongPress(btn2_DuringLongPress);
  }
  else {
    ESP_LOGD(tag, "Button2 Not Initialized");
  }
*/

  if(boardButtons[2] != NULL){
    //boardButtons[2]->setDebounceTicks(DEBOUCE_TIME);
    boardButtons[2]->attachClick(btn3_click);
    boardButtons[2]->attachDoubleClick(btn3_doubleClick);
    boardButtons[2]->attachLongPressStart(btn3_LongPressStart);
    boardButtons[2]->attachLongPressStop(btn3_LongPressStop);
    boardButtons[2]->attachDuringLongPress(btn3_DuringLongPress);
    ESP_LOGI(tag, "Button3 Events Initialized");
  }
  else {
    ESP_LOGW(tag, "Button3 Not Initialized");
  }

}

void Update_Buttons() {
  for (int i = 0; i < 3; ++i) {
    if (boardButtons[i] != nullptr) {
      boardButtons[i]->tick();
    }
  }
}

void btn1_click() {
  //IncrementEventIndex();
  //Pulse_LED_Status(150);
  ESP_LOGD(tag, "btn1 1x");
}

void btn1_doubleClick() {
  ESP_LOGD(tag, "btn1 2x");
}

void btn1_LongPressStart(){
  ESP_LOGD(tag, "btn1 long press start");
}

void btn1_LongPressStop(){
  ESP_LOGD(tag, "btn1 long press stop");
}

void btn1_DuringLongPress(){
  ESP_LOGD(tag, "btn1 long press");
}


/**************************/

void btn2_click() {
  //Pulse_LED_Status(150);
  //Buzzer_Beep(150);
  // send packet
  ESP_LOGD(tag, "btn2 1x");
}

void btn2_doubleClick() {
  ESP_LOGD(tag, "btn2 2x");
}

void btn2_LongPressStart(){
  ESP_LOGD(tag, "btn2 long press start");
}

void btn2_LongPressStop(){
  ESP_LOGD(tag, "btn2 long press stop");
}

void btn2_DuringLongPress(){
  ESP_LOGD(tag, "btn2 long press");
}


/**************************/

void btn3_click() {
  //Pulse_LED_Status(150);
  ESP_LOGD(tag, "btn3 1x");
}

void btn3_doubleClick() {
  bleUpgrade_send_message("Hello....3");
  ESP_LOGD(tag, "btn3 2x");
}

void btn3_LongPressStart(){
  ESP_LOGD(tag, "btn3 long press start");
}

void btn3_LongPressStop(){
  ESP_LOGD(tag, "btn3 long press stop");
}

void btn3_DuringLongPress(){
  ESP_LOGD(tag, "btn3 long press");
}

