<commit_before>/*
  Copyright 2016 <NAME>

  This file is part of Timerecording for Pebble
  http://www.github.com/BaaaZen/TimerecordingForPebble

  This Source Code Form is subject to the terms of the Mozilla Public
  License, v. 2.0. If a copy of the MPL was not distributed with this
  file, You can obtain one at http://mozilla.org/MPL/2.0/.

*/

#include <pebble.h>
#include "messenger.h"
#include "data_handler.h"

static void msg_inbox_received_callback(DictionaryIterator *iter, void *context) {
  // message received -> fetch and parse command in package
  Tuple *cmd_tuple = dict_find(iter, MESSAGE_KEY_CMD);
  Tuple *js_cmd_tuple = dict_find(iter, MESSAGE_JS_KEY_CMD);
  if(cmd_tuple) {
    uint8_t cmd = cmd_tuple->value->uint8;

    if(cmd == MESSAGE_CMD_STATUS_RESPONSE) {
      msg_parse_cmd_status_response(iter);
    }
  } else if(js_cmd_tuple) {
    uint8_t cmd = js_cmd_tuple->value->uint8;

    if(cmd == MESSAGE_JS_CMD_RESPONSE_TL_TOKEN) {
      msg_parse_js_cmd_response_tl_token(iter);
    }
  }
}

static GColor msg_parse_color(uint8_t c) {
  if(c == MESSAGE_COLOR_WHITE) return GColorWhite;
#ifdef PBL_COLOR
  /* only use color if pebble supports it */
  else if(c == MESSAGE_COLOR_RED) return GColorDarkCandyAppleRed;
  else if(c == MESSAGE_COLOR_GREEN) return GColorIslamicGreen;
  else if(c == MESSAGE_COLOR_BLUE) return GColorBlue;
  else if(c == MESSAGE_COLOR_YELLOW) return GColorYellow;
  else if(c == MESSAGE_COLOR_ORANGE) return GColorOrange;
  else if(c == MESSAGE_COLOR_DARKGRAY) return GColorDarkGray;
  else if(c == MESSAGE_COLOR_LIGHTGRAY) return GColorLightGray;
#endif
  else return GColorBlack;
}

static void msg_parse_cmd_status_response(DictionaryIterator *iter) {
  /* clear all data from data handler */
  Tuple *t_f_clearall = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_CLEARALL);
  if(t_f_clearall) {
    if(t_f_clearall->value->uint8 > 0) data_clear_display_cache();
  }

  /* checked in state */
  Tuple *t_ci = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_STATUS_CHECKED_IN);
  if(t_ci) {
    data_update_checked_in_state(t_ci->value->uint8 > 0);
  }

  /* common description */
  Tuple *t_d_s = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_STATUS_CONTENT_TEXT);
  Tuple *t_d_c = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_STATUS_CONTENT_COLOR);
  if(t_d_s && t_d_c) {
    data_update_title_cache(t_d_s->value->cstring, msg_parse_color(t_d_c->value->uint8));
  }

  /* face */
  Tuple *t_f_id = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_ID);
  Tuple *t_f_d_s = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TEXT);
  Tuple *t_f_d_c = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_COLOR);
  Tuple *t_f_t1_s = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TIME1_TEXT);
  Tuple *t_f_t1_c = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TIME1_COLOR);
  Tuple *t_f_t2_s = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TIME2_TEXT);
  Tuple *t_f_t2_c = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TIME2_COLOR);
  if(t_f_id && t_f_d_s && t_f_d_c && t_f_t1_s && t_f_t1_c && t_f_t2_s && t_f_t2_c) {
    data_update_display_cache(t_f_id->value->uint8, t_f_d_s->value->cstring, msg_parse_color(t_f_d_c->value->uint8),
      t_f_t1_s->value->cstring, msg_parse_color(t_f_t1_c->value->uint8),
      t_f_t2_s->value->cstring, msg_parse_color(t_f_t2_c->value->uint8));
  }
}

static void msg_parse_cmd_request_tl_token(DictionaryIterator *iter) {
  /* just forward request to js */
  msg_js_cmd_request_tl_token();
}

static void msg_parse_js_cmd_response_tl_token(DictionaryIterator *iter) {
  /* just forward response to android app */
  Tuple *token = dict_find(iter, MESSAGE_JS_KEY_TL_TOKEN);
  if(token) {
    msg_cmd_response_tl_token(token->value->cstring);
  }
}

static void msg_inbox_dropped_callback(AppMessageResult reason, void *context) {
  // A message was received, but had to be dropped
  APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped. Reason: %d", (int)reason);
}

static void msg_outbox_sent_callback(DictionaryIterator *iter, void *context) {
  // The message just sent has been successfully delivered

}

static void msg_outbox_failed_callback(DictionaryIterator *iter, AppMessageResult reason, void *context) {
  // The message just sent failed to be delivered
  APP_LOG(APP_LOG_LEVEL_ERROR, "Message send failed. Reason: %d", (int)reason);
}


void msg_cmd_fetch_status(void) {
  // Declare the dictionary's iterator
  DictionaryIterator *out_msg;<commit_msg>another bugfix for watchapp<commit_after>/*
  Copyright 2016 <NAME>

  This file is part of Timerecording for Pebble
  http://www.github.com/BaaaZen/TimerecordingForPebble

  This Source Code Form is subject to the terms of the Mozilla Public
  License, v. 2.0. If a copy of the MPL was not distributed with this
  file, You can obtain one at http://mozilla.org/MPL/2.0/.

*/

#include <pebble.h>
#include "messenger.h"
#include "data_handler.h"

static void msg_inbox_received_callback(DictionaryIterator *iter, void *context) {
  // message received -> fetch and parse command in package
  Tuple *cmd_tuple = dict_find(iter, MESSAGE_KEY_CMD);
  Tuple *js_cmd_tuple = dict_find(iter, MESSAGE_JS_KEY_CMD);
  if(cmd_tuple) {
    uint8_t cmd = cmd_tuple->value->uint8;

    if(cmd == MESSAGE_CMD_STATUS_RESPONSE) {
      msg_parse_cmd_status_response(iter);
    }
  } else if(js_cmd_tuple) {
    uint8_t cmd = js_cmd_tuple->value->uint8;

    if(cmd == MESSAGE_JS_CMD_RESPONSE_TL_TOKEN) {
      msg_parse_js_cmd_response_tl_token(iter);
    }
  }
}

static GColor msg_parse_color(uint8_t c) {
  if(c == MESSAGE_COLOR_WHITE) return GColorWhite;
#ifdef PBL_COLOR
  /* only use color if pebble supports it */
  else if(c == MESSAGE_COLOR_RED) return GColorDarkCandyAppleRed;
  else if(c == MESSAGE_COLOR_GREEN) return GColorIslamicGreen;
  else if(c == MESSAGE_COLOR_BLUE) return GColorBlue;
  else if(c == MESSAGE_COLOR_YELLOW) return GColorYellow;
  else if(c == MESSAGE_COLOR_ORANGE) return GColorOrange;
  else if(c == MESSAGE_COLOR_DARKGRAY) return GColorDarkGray;
  else if(c == MESSAGE_COLOR_LIGHTGRAY) return GColorLightGray;
#endif
  else return GColorBlack;
}

static void msg_parse_cmd_status_response(DictionaryIterator *iter) {
  /* clear all data from data handler */
  Tuple *t_f_clearall = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_CLEARALL);
  if(t_f_clearall) {
    if(t_f_clearall->value->uint8 > 0) data_clear_display_cache();
  }

  /* checked in state */
  Tuple *t_ci = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_STATUS_CHECKED_IN);
  if(t_ci) {
    data_update_checked_in_state(t_ci->value->uint8 > 0);
  }

  /* common description */
  Tuple *t_d_s = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_STATUS_CONTENT_TEXT);
  Tuple *t_d_c = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_STATUS_CONTENT_COLOR);
  if(t_d_s && t_d_c) {
    data_update_title_cache(t_d_s->value->cstring, msg_parse_color(t_d_c->value->uint8));
  }

  /* face */
  Tuple *t_f_id = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_ID);
  Tuple *t_f_d_s = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TEXT);
  Tuple *t_f_d_c = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_COLOR);
  Tuple *t_f_t1_s = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TIME1_TEXT);
  Tuple *t_f_t1_c = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TIME1_COLOR);
  Tuple *t_f_t2_s = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TIME2_TEXT);
  Tuple *t_f_t2_c = dict_find(iter, MESSAGE_KEY_STATUS_RESPONSE_FACE_TIME2_COLOR);
  if(t_f_id && t_f_d_s && t_f_d_c && t_f_t1_s && t_f_t1_c && t_f_t2_s && t_f_t2_c) {
    data_update_display_cache(t_f_id->value->uint8, t_f_d_s->value->cstring, msg_parse_color(t_f_d_c->value->uint8),
      t_f_t1_s->value->cstring, msg_parse_color(t_f_t1_c->value->uint8),
      t_f_t2_s->value->cstring, msg_parse_color(t_f_t2_c->value->uint8));
  }
}

static void msg_parse_js_cmd_response_tl_token(DictionaryIterator *iter) {
  /* just forward response to android app */
  Tuple *token = dict_find(iter, MESSAGE_JS_KEY_TL_TOKEN);
  if(token) {
    msg_cmd_response_tl_token(token->value->cstring);
  }
}

static void msg_inbox_dropped_callback(AppMessageResult reason, void *context) {
  // A message was received, but had to be dropped
  APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped. Reason: %d", (int)reason);
}

static void msg_outbox_sent_callback(DictionaryIterator *iter, void *context) {
  // The message just sent has been successfully delivered

}

static void msg_outbox_failed_callback(DictionaryIterator *iter, AppMessageResult reason, void *context) {
  // The message just sent failed to be delivered
  APP_LOG(APP_LOG_LEVEL_ERROR, "Message send failed. Reason: %d", (int)reason);
}


void msg_cmd_fetch_status(void) {
  // Declare the dictionary's iterator
  DictionaryIterator *out_msg;
{'fraction_non_alphanumeric': 0.09988597491448119, 'fraction_numerical': 0.008665906499429875, 'mean_word_length': 5.397520058351568, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7622399', 'n_tokens_mistral': 3496, 'n_tokens_neox': 3401, 'n_words': 638}