#include "user_config.h"
#include "user_event.h"
#include "user_gpio.h"
#include "user_player.h"
#include "user_pwm.h"
#include "user_timer.h"
#include "user_uart.h"

#define TAG "auto_gpio"

#define UART_SEND_MAX      16

typedef struct {
  char  data[UART_SEND_MAX];
  int   len;
}uart_data_t;

const uart_data_t g_uart_buf[] = {
  {{0x01, 0x01, 0x0A}, 3}, //TurnOn
  {{0x01, 0x02, 0x0A}, 3}, //TurnOff
  {{0x03, 0x02, 0x0A}, 3}, //DimmingPoint
  {{0x15, 0x01, 0x0A}, 3}, //WDGuests
  {{0x20, 0x01, 0x0A}, 3}, //AddTea
  {{0x20, 0x02, 0x0A}, 3}, //CallTheWaiter
  {{0x17, 0x01, 0x0A}, 3}, //PlayMusic
  {{0x03, 0x01, 0x0A}, 3}, //Highlight
  {{0x04, 0x02, 0x0A}, 3}, //Darkest
  {{0x05, 0x01, 0x0A}, 3}, //OnSpotlight
  {{0x05, 0x02, 0x0A}, 3}, //OffSpotlight
  {{0x06, 0x01, 0x0A}, 3}, //OnDeskLamp
  {{0x06, 0x02, 0x0A}, 3}, //OffDeskLamp
  {{0x07, 0x01, 0x0A}, 3}, //OnLightBelt
  {{0x07, 0x02, 0x0A}, 3}, //OffLightBelt
  {{0x08, 0x01, 0x0A}, 3}, //OnWallLamp
  {{0x08, 0x02, 0x0A}, 3}, //OffWallLamp
  {{0x09, 0x01, 0x0A}, 3}, //OnDownlight
  {{0x09, 0x02, 0x0A}, 3}, //OffDownlight
  {{0x10, 0x01, 0x0A}, 3}, //OnBacklight
  {{0x10, 0x02, 0x0A}, 3}, //OffBacklight
  {{0x12, 0x01, 0x0A}, 3}, //OnAChandelier
  {{0x12, 0x02, 0x0A}, 3}, //OffAChandelier
  {{0x13, 0x01, 0x0A}, 3}, //OnToiletLight
  {{0x13, 0x02, 0x0A}, 3}, //OffToiletLight
  {{0x16, 0x01, 0x0A}, 3}, //StandardMode
  {{0x16, 0x03, 0x0A}, 3}, //QuietMode
  {{0x16, 0x04, 0x0A}, 3}, //SleepMode
  {{0x16, 0x02, 0x0A}, 3}, //UnattendedMode
  {{0x14, 0x01, 0x0A}, 3}, //OnAC
  {{0x16, 0x05, 0x0A}, 3}, //CleaningMode
  {{0x21, 0x01, 0x0A}, 3}, //OnPrivacyGlass
  {{0x14, 0x02, 0x0A}, 3}, //OffAC
  {{0x14, 0x03, 0x0A}, 3}, //HAVolume
  {{0x14, 0x04, 0x0A}, 3}, //SAVolume
  {{0x14, 0x05, 0x0A}, 3}, //LAVolume
  {{0x14, 0x06, 0x0A}, 3}, //IAVolume
  {{0x21, 0x02, 0x0A}, 3}, //PrivacyGlass
  {{0x14, 0x07, 0x0A}, 3}, //RAVolume
  {{0x14, 0x08, 0x0A}, 3}, //ROneDegree
  {{0x14, 0x09, 0x0A}, 3}, //LOneDegree
  {{0x17, 0x03, 0x0A}, 3}, //VolumeUp
  {{0x17, 0x04, 0x0A}, 3}, //VolumeDown
  {{0x17, 0x02, 0x0A}, 3}, //StopPlaying
  {{0x17, 0x05, 0x0A}, 3}, //VolumeUpMusic
  {{0x17, 0x06, 0x0A}, 3}, //VolumeDownMusic
  {{0x20, 0x03, 0x0A}, 3}, //CheckOut
  {{0x20, 0x04, 0x0A}, 3}, //MHurryUp
  {{0x20, 0x05, 0x0A}, 3}, //HABOfWater
  {{0x20, 0x06, 0x0A}, 3}, //BSTToEat
  {{0x20, 0x08, 0x0A}, 3}, //FruitPlate
  {{0x18, 0x01, 0x0A}, 3}, //OpenCurtains
  {{0x18, 0x02, 0x0A}, 3}, //CloseCurtains
  {{0x18, 0x03, 0x0A}, 3}, //PauseCurtains
  {{0x14, 0x18, 0x0A}, 3}, //To18Degrees
  {{0x14, 0x19, 0x0A}, 3}, //To19Degrees
  {{0x14, 0x20, 0x0A}, 3}, //To20Degrees
  {{0x14, 0x21, 0x0A}, 3}, //To21Degrees
  {{0x14, 0x22, 0x0A}, 3}, //To22Degrees
  {{0x14, 0x23, 0x0A}, 3}, //To23Degrees
  {{0x14, 0x24, 0x0A}, 3}, //To24Degrees
  {{0x14, 0x25, 0x0A}, 3}, //To25Degrees
  {{0x14, 0x26, 0x0A}, 3}, //To26Degrees
  {{0x14, 0x27, 0x0A}, 3}, //To27Degrees
  {{0x14, 0x28, 0x0A}, 3}, //To28Degrees
  {{0x14, 0x29, 0x0A}, 3}, //To29Degrees
  {{0x14, 0x30, 0x0A}, 3}, //To30Degrees
  {{0x20, 0x11, 0x0A}, 3}, //ConAmount
  {{0x20, 0x12, 0x0A}, 3}, //AskAMasseur
  {{0x20, 0x13, 0x0A}, 3}, //CheckTB
  {{0x20, 0x14, 0x0A}, 3}, //VoiceChat
  {{0x20, 0x15, 0x0A}, 3}, //CurrentTime
  {{0x20, 0x17, 0x0A}, 3}, //ALLOFF
  {{0x20, 0x18, 0x0A}, 3}, //CheckTime
  {{0x21, 0x18, 0x0A}, 3}, //FormAFine
  {{0x21, 0x20, 0x0A}, 3}, //ChangeRoom
  {{0x21, 0x21, 0x0A}, 3}, //ChangeItems
  {{0x21, 0x22, 0x0A}, 3}, //AddItems
  {{0x21, 0x23, 0x0A}, 3}, //Processed
  {{0x21, 0x24, 0x0A}, 3}, //ChangePerson
  {{0x21, 0x25, 0x0A}, 3}, //CCompleted
  {{0x21, 0x26, 0x0A}, 3}, //PowerOnVideo
  {{0x21, 0x27, 0x0A}, 3}, //PowerOffVideo
  {{0x21, 0x27, 0x0A}, 3}, //CoolingMode
  {{0x21, 0x28, 0x0A}, 3}, //HeatingMode
  {{0x21, 0x29, 0x0A}, 3}, //CallCustomerSer
  {{0x21, 0x30, 0x0A}, 3}, //VoiceCallFD
  {{0x21, 0x31, 0x0A}, 3}, //VoiceCallAtt
  {{0x21, 0x32, 0x0A}, 3}, //VoiceCallCle
};

static void _custom_setting_cb(USER_EVENT_TYPE event,
                               user_event_context_t *context) {
  event_custom_setting_t *setting = NULL;
  if (context) {
    setting = &context->custom_setting;
    LOGT(TAG, "user command: %s", setting->cmd);
    if (0 == uni_strcmp(setting->cmd, "TurnOn")) {
      user_uart_send(g_uart_buf[0].data, g_uart_buf[0].len);
    } else if (0 == uni_strcmp(setting->cmd, "TurnOff")) {
      user_uart_send(g_uart_buf[1].data, g_uart_buf[1].len);
    } else if (0 == uni_strcmp(setting->cmd, "DimmingPoint")) {
      user_uart_send(g_uart_buf[2].data, g_uart_buf[2].len);
    } else if (0 == uni_strcmp(setting->cmd, "WDGuests")) {
      user_uart_send(g_uart_buf[3].data, g_uart_buf[3].len);
    } else if (0 == uni_strcmp(setting->cmd, "AddTea")) {
      user_uart_send(g_uart_buf[4].data, g_uart_buf[4].len);
    } else if (0 == uni_strcmp(setting->cmd, "CallTheWaiter")) {
      user_uart_send(g_uart_buf[5].data, g_uart_buf[5].len);
    } else if (0 == uni_strcmp(setting->cmd, "PlayMusic")) {
      user_uart_send(g_uart_buf[6].data, g_uart_buf[6].len);
    } else if (0 == uni_strcmp(setting->cmd, "Highlight")) {
      user_uart_send(g_uart_buf[7].data, g_uart_buf[7].len);
    } else if (0 == uni_strcmp(setting->cmd, "Darkest")) {
      user_uart_send(g_uart_buf[8].data, g_uart_buf[8].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnSpotlight")) {
      user_uart_send(g_uart_buf[9].data, g_uart_buf[9].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffSpotlight")) {
      user_uart_send(g_uart_buf[10].data, g_uart_buf[10].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnDeskLamp")) {
      user_uart_send(g_uart_buf[11].data, g_uart_buf[11].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffDeskLamp")) {
      user_uart_send(g_uart_buf[12].data, g_uart_buf[12].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnLightBelt")) {
      user_uart_send(g_uart_buf[13].data, g_uart_buf[13].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffLightBelt")) {
      user_uart_send(g_uart_buf[14].data, g_uart_buf[14].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnWallLamp")) {
      user_uart_send(g_uart_buf[15].data, g_uart_buf[15].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffWallLamp")) {
      user_uart_send(g_uart_buf[16].data, g_uart_buf[16].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnDownlight")) {
      user_uart_send(g_uart_buf[17].data, g_uart_buf[17].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffDownlight")) {
      user_uart_send(g_uart_buf[18].data, g_uart_buf[18].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnBacklight")) {
      user_uart_send(g_uart_buf[19].data, g_uart_buf[19].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffBacklight")) {
      user_uart_send(g_uart_buf[20].data, g_uart_buf[20].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnAChandelier")) {
      user_uart_send(g_uart_buf[21].data, g_uart_buf[21].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffAChandelier")) {
      user_uart_send(g_uart_buf[22].data, g_uart_buf[22].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnToiletLight")) {
      user_uart_send(g_uart_buf[23].data, g_uart_buf[23].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffToiletLight")) {
      user_uart_send(g_uart_buf[24].data, g_uart_buf[24].len);
    } else if (0 == uni_strcmp(setting->cmd, "StandardMode")) {
      user_uart_send(g_uart_buf[25].data, g_uart_buf[25].len);
    } else if (0 == uni_strcmp(setting->cmd, "QuietMode")) {
      user_uart_send(g_uart_buf[26].data, g_uart_buf[26].len);
    } else if (0 == uni_strcmp(setting->cmd, "SleepMode")) {
      user_uart_send(g_uart_buf[27].data, g_uart_buf[27].len);
    } else if (0 == uni_strcmp(setting->cmd, "UnattendedMode")) {
      user_uart_send(g_uart_buf[28].data, g_uart_buf[28].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnAC")) {
      user_uart_send(g_uart_buf[29].data, g_uart_buf[29].len);
    } else if (0 == uni_strcmp(setting->cmd, "CleaningMode")) {
      user_uart_send(g_uart_buf[30].data, g_uart_buf[30].len);
    } else if (0 == uni_strcmp(setting->cmd, "OnPrivacyGlass")) {
      user_uart_send(g_uart_buf[31].data, g_uart_buf[31].len);
    } else if (0 == uni_strcmp(setting->cmd, "OffAC")) {
      user_uart_send(g_uart_buf[32].data, g_uart_buf[32].len);
    } else if (0 == uni_strcmp(setting->cmd, "HAVolume")) {
      user_uart_send(g_uart_buf[33].data, g_uart_buf[33].len);
    } else if (0 == uni_strcmp(setting->cmd, "SAVolume")) {
      user_uart_send(g_uart_buf[34].data, g_uart_buf[34].len);
    } else if (0 == uni_strcmp(setting->cmd, "LAVolume")) {
      user_uart_send(g_uart_buf[35].data, g_uart_buf[35].len);
    } else if (0 == uni_strcmp(setting->cmd, "IAVolume")) {
      user_uart_send(g_uart_buf[36].data, g_uart_buf[36].len);
    } else if (0 == uni_strcmp(setting->cmd, "PrivacyGlass")) {
      user_uart_send(g_uart_buf[37].data, g_uart_buf[37].len);
    } else if (0 == uni_strcmp(setting->cmd, "RAVolume")) {
      user_uart_send(g_uart_buf[38].data, g_uart_buf[38].len);
    } else if (0 == uni_strcmp(setting->cmd, "ROneDegree")) {
      user_uart_send(g_uart_buf[39].data, g_uart_buf[39].len);
    } else if (0 == uni_strcmp(setting->cmd, "LOneDegree")) {
      user_uart_send(g_uart_buf[40].data, g_uart_buf[40].len);
    } else if (0 == uni_strcmp(setting->cmd, "VolumeUp")) {
      user_uart_send(g_uart_buf[41].data, g_uart_buf[41].len);
    } else if (0 == uni_strcmp(setting->cmd, "VolumeDown")) {
      user_uart_send(g_uart_buf[42].data, g_uart_buf[42].len);
    } else if (0 == uni_strcmp(setting->cmd, "StopPlaying")) {
      user_uart_send(g_uart_buf[43].data, g_uart_buf[43].len);
    } else if (0 == uni_strcmp(setting->cmd, "VolumeUpMusic")) {
      user_uart_send(g_uart_buf[44].data, g_uart_buf[44].len);
    } else if (0 == uni_strcmp(setting->cmd, "VolumeDownMusic")) {
      user_uart_send(g_uart_buf[45].data, g_uart_buf[45].len);
    } else if (0 == uni_strcmp(setting->cmd, "CheckOut")) {
      user_uart_send(g_uart_buf[46].data, g_uart_buf[46].len);
    } else if (0 == uni_strcmp(setting->cmd, "MHurryUp")) {
      user_uart_send(g_uart_buf[47].data, g_uart_buf[47].len);
    } else if (0 == uni_strcmp(setting->cmd, "HABOfWater")) {
      user_uart_send(g_uart_buf[48].data, g_uart_buf[48].len);
    } else if (0 == uni_strcmp(setting->cmd, "BSTToEat")) {
      user_uart_send(g_uart_buf[49].data, g_uart_buf[49].len);
    } else if (0 == uni_strcmp(setting->cmd, "FruitPlate")) {
      user_uart_send(g_uart_buf[50].data, g_uart_buf[50].len);
    } else if (0 == uni_strcmp(setting->cmd, "OpenCurtains")) {
      user_uart_send(g_uart_buf[51].data, g_uart_buf[51].len);
    } else if (0 == uni_strcmp(setting->cmd, "CloseCurtains")) {
      user_uart_send(g_uart_buf[52].data, g_uart_buf[52].len);
    } else if (0 == uni_strcmp(setting->cmd, "PauseCurtains")) {
      user_uart_send(g_uart_buf[53].data, g_uart_buf[53].len);
    } else if (0 == uni_strcmp(setting->cmd, "To18Degrees")) {
      user_uart_send(g_uart_buf[54].data, g_uart_buf[54].len);
    } else if (0 == uni_strcmp(setting->cmd, "To19Degrees")) {
      user_uart_send(g_uart_buf[55].data, g_uart_buf[55].len);
    } else if (0 == uni_strcmp(setting->cmd, "To20Degrees")) {
      user_uart_send(g_uart_buf[56].data, g_uart_buf[56].len);
    } else if (0 == uni_strcmp(setting->cmd, "To21Degrees")) {
      user_uart_send(g_uart_buf[57].data, g_uart_buf[57].len);
    } else if (0 == uni_strcmp(setting->cmd, "To22Degrees")) {
      user_uart_send(g_uart_buf[58].data, g_uart_buf[58].len);
    } else if (0 == uni_strcmp(setting->cmd, "To23Degrees")) {
      user_uart_send(g_uart_buf[59].data, g_uart_buf[59].len);
    } else if (0 == uni_strcmp(setting->cmd, "To24Degrees")) {
      user_uart_send(g_uart_buf[60].data, g_uart_buf[60].len);
    } else if (0 == uni_strcmp(setting->cmd, "To25Degrees")) {
      user_uart_send(g_uart_buf[61].data, g_uart_buf[61].len);
    } else if (0 == uni_strcmp(setting->cmd, "To26Degrees")) {
      user_uart_send(g_uart_buf[62].data, g_uart_buf[62].len);
    } else if (0 == uni_strcmp(setting->cmd, "To27Degrees")) {
      user_uart_send(g_uart_buf[63].data, g_uart_buf[63].len);
    } else if (0 == uni_strcmp(setting->cmd, "To28Degrees")) {
      user_uart_send(g_uart_buf[64].data, g_uart_buf[64].len);
    } else if (0 == uni_strcmp(setting->cmd, "To29Degrees")) {
      user_uart_send(g_uart_buf[65].data, g_uart_buf[65].len);
    } else if (0 == uni_strcmp(setting->cmd, "To30Degrees")) {
      user_uart_send(g_uart_buf[66].data, g_uart_buf[66].len);
    } else if (0 == uni_strcmp(setting->cmd, "ConAmount")) {
      user_uart_send(g_uart_buf[67].data, g_uart_buf[67].len);
    } else if (0 == uni_strcmp(setting->cmd, "AskAMasseur")) {
      user_uart_send(g_uart_buf[68].data, g_uart_buf[68].len);
    } else if (0 == uni_strcmp(setting->cmd, "CheckTB")) {
      user_uart_send(g_uart_buf[69].data, g_uart_buf[69].len);
    } else if (0 == uni_strcmp(setting->cmd, "VoiceChat")) {
      user_uart_send(g_uart_buf[70].data, g_uart_buf[70].len);
    } else if (0 == uni_strcmp(setting->cmd, "CurrentTime")) {
      user_uart_send(g_uart_buf[71].data, g_uart_buf[71].len);
    } else if (0 == uni_strcmp(setting->cmd, "ALLOFF")) {
      user_uart_send(g_uart_buf[72].data, g_uart_buf[72].len);
    } else if (0 == uni_strcmp(setting->cmd, "CheckTime")) {
      user_uart_send(g_uart_buf[73].data, g_uart_buf[73].len);
    } else if (0 == uni_strcmp(setting->cmd, "FormAFine")) {
      user_uart_send(g_uart_buf[74].data, g_uart_buf[74].len);
    } else if (0 == uni_strcmp(setting->cmd, "ChangeRoom")) {
      user_uart_send(g_uart_buf[75].data, g_uart_buf[75].len);
    } else if (0 == uni_strcmp(setting->cmd, "ChangeItems")) {
      user_uart_send(g_uart_buf[76].data, g_uart_buf[76].len);
    } else if (0 == uni_strcmp(setting->cmd, "AddItems")) {
      user_uart_send(g_uart_buf[77].data, g_uart_buf[77].len);
    } else if (0 == uni_strcmp(setting->cmd, "Processed")) {
      user_uart_send(g_uart_buf[78].data, g_uart_buf[78].len);
    } else if (0 == uni_strcmp(setting->cmd, "ChangePerson")) {
      user_uart_send(g_uart_buf[79].data, g_uart_buf[79].len);
    } else if (0 == uni_strcmp(setting->cmd, "CCompleted")) {
      user_uart_send(g_uart_buf[80].data, g_uart_buf[80].len);
    } else if (0 == uni_strcmp(setting->cmd, "PowerOnVideo")) {
      user_uart_send(g_uart_buf[81].data, g_uart_buf[81].len);
    } else if (0 == uni_strcmp(setting->cmd, "PowerOffVideo")) {
      user_uart_send(g_uart_buf[82].data, g_uart_buf[82].len);
    } else if (0 == uni_strcmp(setting->cmd, "CoolingMode")) {
      user_uart_send(g_uart_buf[83].data, g_uart_buf[83].len);
    } else if (0 == uni_strcmp(setting->cmd, "HeatingMode")) {
      user_uart_send(g_uart_buf[84].data, g_uart_buf[84].len);
    } else if (0 == uni_strcmp(setting->cmd, "CallCustomerSer")) {
      user_uart_send(g_uart_buf[85].data, g_uart_buf[85].len);
    } else if (0 == uni_strcmp(setting->cmd, "VoiceCallFD")) {
      user_uart_send(g_uart_buf[86].data, g_uart_buf[86].len);
    } else if (0 == uni_strcmp(setting->cmd, "VoiceCallAtt")) {
      user_uart_send(g_uart_buf[87].data, g_uart_buf[87].len);
    } else if (0 == uni_strcmp(setting->cmd, "VoiceCallCle")) {
      user_uart_send(g_uart_buf[88].data, g_uart_buf[88].len);
    } else {
      LOGT(TAG, "Unconcerned command: %s", setting->cmd);
    }
    user_player_reply_list_random(setting->reply_files);
  }
}

static void _register_event_callback(void) {
  user_event_subscribe_event(USER_CUSTOM_SETTING, _custom_setting_cb);
}

int hb_auto_gpio(void) {
  user_gpio_init();
  user_gpio_set_mode(GPIO_NUM_A25, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_A25, 0);
  user_gpio_set_mode(GPIO_NUM_A26, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_A26, 0);
  user_gpio_set_mode(GPIO_NUM_A27, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_A27, 0);
  user_gpio_set_mode(GPIO_NUM_A28, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_A28, 0);
  user_gpio_set_mode(GPIO_NUM_B2, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_B2, 0);
  user_gpio_set_mode(GPIO_NUM_B3, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_B3, 0);
  user_uart_init(NULL);
  _register_event_callback();
  return 0;
}

