#include <pebble.h>

enum {
  KEY_TIME    = 0,  // Inbound: current time string from companion app
  KEY_COMMAND = 1   // Outbound: button name sent to companion app
};

static Window *s_main_window;
static TextLayer *s_time_layer;
static TextLayer *s_hint_layer;

static char s_time_buffer[32] = "Waiting...";

static void send_key_to_phone(const char *key_str) {
  DictionaryIterator *iter;
  app_message_outbox_begin(&iter);
  if (iter == NULL) return;
  dict_write_cstring(iter, KEY_COMMAND, key_str);
  app_message_outbox_send();
}

static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
  Tuple *time_tuple = dict_find(iterator, KEY_TIME);
  if (time_tuple && time_tuple->type == TUPLE_CSTRING) {
    snprintf(s_time_buffer, sizeof(s_time_buffer), "%s", time_tuple->value->cstring);
    text_layer_set_text(s_time_layer, s_time_buffer);
    layer_mark_dirty(text_layer_get_layer(s_time_layer));
  }
}

static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
  send_key_to_phone("UP");
}

static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
  send_key_to_phone("DOWN");
}

static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
  send_key_to_phone("SELECT");
}

static void click_config_provider(void *context) {
  window_single_click_subscribe(BUTTON_ID_UP,     up_click_handler);
  window_single_click_subscribe(BUTTON_ID_DOWN,   down_click_handler);
  window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
}

static void main_window_load(Window *window) {
  Layer *window_layer = window_get_root_layer(window);
  GRect bounds = layer_get_bounds(window_layer);

  s_time_layer = text_layer_create(GRect(0, 55, bounds.size.w, 50));
  text_layer_set_text(s_time_layer, s_time_buffer);
  text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_30_BLACK));
  text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
  layer_add_child(window_layer, text_layer_get_layer(s_time_layer));

  s_hint_layer = text_layer_create(GRect(0, 130, bounds.size.w, 30));
  text_layer_set_text(s_hint_layer, "UP / DN / SEL");
  text_layer_set_font(s_hint_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14));
  text_layer_set_text_alignment(s_hint_layer, GTextAlignmentCenter);
  layer_add_child(window_layer, text_layer_get_layer(s_hint_layer));
}

static void main_window_unload(Window *window) {
  text_layer_destroy(s_time_layer);
  text_layer_destroy(s_hint_layer);
}

static void init(void) {
  s_main_window = window_create();
  window_set_click_config_provider(s_main_window, click_config_provider);
  window_set_window_handlers(s_main_window, (WindowHandlers) {
    .load   = main_window_load,
    .unload = main_window_unload
  });
  window_stack_push(s_main_window, true);

  app_message_register_inbox_received(inbox_received_callback);
  app_message_open(128, 64);
}

static void deinit(void) {
  window_destroy(s_main_window);
}

int main(void) {
  init();
  app_event_loop();
  deinit();
}
