/*
 * Copyright 2018 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Some functions mocked from MGOS, so we can run unit tests standalone.
 */

#include "mgos_mock.h"
#include <unistd.h>

int _mgos_timers = 0;

int log_print_prefix(enum cs_log_level l, const char *func, const char *file) {
  char   ll_str[6];
  char   ll_file[31];
  char   ll_func[41];
  size_t offset = 0;

  switch (l) {
  case LL_ERROR:
    strncpy(ll_str, "ERROR", sizeof(ll_str));
    break;

  case LL_WARN:
    strncpy(ll_str, "WARN", sizeof(ll_str));
    break;

  case LL_INFO:
    strncpy(ll_str, "INFO", sizeof(ll_str));
    break;

  case LL_DEBUG:
    strncpy(ll_str, "DEBUG", sizeof(ll_str));
    break;

  case LL_VERBOSE_DEBUG:
    strncpy(ll_str, "VERB", sizeof(ll_str));
    break;

  default:                       // LL_NONE
    return 0;
  }

  offset = 0;
  memset(ll_file, 0, sizeof(ll_file));
  if (strlen(file) >= sizeof(ll_file)) {
    offset = strlen(file) - sizeof(ll_file) + 1;
  }
  strncpy(ll_file, file + offset, sizeof(ll_file) - 1);

  offset = 0;
  memset(ll_func, 0, sizeof(ll_func));
  if (strlen(func) >= sizeof(ll_func)) {
    offset = strlen(func) - sizeof(ll_func) + 1;
  }
  strncpy(ll_func, func + offset, sizeof(ll_func) - 1);

  printf("%-5s %-30s %-40s| ", ll_str, ll_file, ll_func);
  return 1;
}

double mg_time(void) {
  struct timespec ts;
  double          ret;

  clock_gettime(CLOCK_REALTIME_COARSE, &ts);

  ret  = (double)ts.tv_sec;
  ret += ((double)ts.tv_nsec) / 1000000000;

  return ret;
}

void mgos_usleep(uint32_t usecs) {
  usleep(usecs);
}

bool mgos_gpio_set_int_handler(int pin, enum mgos_gpio_int_mode mode, mgos_gpio_int_handler_f cb, void *arg) {
  LOG(LL_INFO, ("Not implemented."));
  return true;

  (void)pin;
  (void)mode;
  (void)cb;
  (void)arg;
}

bool mgos_gpio_enable_int(int pin) {
  LOG(LL_INFO, ("Not implemented."));
  return true;

  (void)pin;
}

bool mgos_gpio_disable_int(int pin) {
  LOG(LL_INFO, ("Not implemented."));
  return true;

  (void)pin;
}

void mgos_gpio_clear_int(int pin) {
  LOG(LL_INFO, ("Not implemented."));
  return;

  (void)pin;
}
