#include "stddef.h"
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/wc_port.h>
#include <wolfssl/wolfcrypt/random.h>
#include "lwip/sockets.h"
#include "kernel.h"
#include "itron.h"
#ifdef __RX
#include "sys/time.h"
#endif
#include "time.h"

#ifdef __RX
_Ctype_t _Touptab;

char *getenv(const char *name)
{
	syslog(LOG_NOTICE, name);
	return "";
}
#else
struct tm *gmtime_r(const time_t *pt, struct tm* tm);
#endif

/* NTPサーバーのグローバル時間 */
static time_t sys_time_offset = 1453334400LL;

time_t sys_time(time_t *t)
{
	if (t == NULL) {
		time_t temp;
		return time(&temp);
	}

	SYSTIM tim;
	get_tim(&tim);
	*t = (time_t)(tim / 1000) + sys_time_offset;
	return *t;
}

void sys_set_system_time(time_t t)
{
	SYSTIM tim;
	get_tim(&tim);
	sys_time_offset = t - (time_t)(tim / 1000);

	struct tm tm;
	gmtime_r(&t, &tm);
	syslog(LOG_NOTICE, "%d/%d/%d %d:%d:%d", 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
		tm.tm_hour, tm.tm_min, tm.tm_sec);
}

/* musl-1.1.12/src/time/__secs_to_tm.c */
/* musl as a whole is licensed under the following standard MIT license:
 * 
 * ----------------------------------------------------------------------
 * Copyright (C) 2005-2014 Rich Felker, et al.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * ----------------------------------------------------------------------*/

/* 2000-03-01 (mod 400 year, immediately after feb29 */
#define LEAPOCH (946684800LL + 86400*(31+29))

#define DAYS_PER_400Y (365*400 + 97)
#define DAYS_PER_100Y (365*100 + 24)
#define DAYS_PER_4Y   (365*4   + 1)

struct tm *gmtime_r(const time_t *pt, struct tm* tm)
{
	time_t t = *pt;
	time_t days, secs, years;
	int remdays, remsecs, remyears;
	int qc_cycles, c_cycles, q_cycles;
	int months;
	int wday, yday, leap;
	static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};

	/* Reject time_t values whose year would overflow int */
	if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
		return NULL;

	secs = t - LEAPOCH;
	days = secs / 86400;
	remsecs = secs % 86400;
	if (remsecs < 0) {
		remsecs += 86400;
		days--;
	}

	wday = (3 + days) % 7;
	if (wday < 0) wday += 7;

	qc_cycles = days / DAYS_PER_400Y;
	remdays = days % DAYS_PER_400Y;
	if (remdays < 0) {
		remdays += DAYS_PER_400Y;
		qc_cycles--;
	}

	c_cycles = remdays / DAYS_PER_100Y;
	if (c_cycles == 4) c_cycles--;
	remdays -= c_cycles * DAYS_PER_100Y;

	q_cycles = remdays / DAYS_PER_4Y;
	if (q_cycles == 25) q_cycles--;
	remdays -= q_cycles * DAYS_PER_4Y;

	remyears = remdays / 365;
	if (remyears == 4) remyears--;
	remdays -= remyears * 365;

	leap = !remyears && (q_cycles || !c_cycles);
	yday = remdays + 31 + 28 + leap;
	if (yday >= 365 + leap) yday -= 365 + leap;

	years = remyears + 4 * q_cycles + 100 * c_cycles + 400LL * qc_cycles;

	for (months = 0; days_in_month[months] <= remdays; months++)
		remdays -= days_in_month[months];

	if (years + 100 > INT_MAX || years + 100 < INT_MIN)
		return NULL;

	tm->tm_year = years + 100;
	tm->tm_mon = months + 2;
	if (tm->tm_mon >= 12) {
		tm->tm_mon -= 12;
		tm->tm_year++;
	}
	tm->tm_mday = remdays + 1;
	tm->tm_wday = wday;
	tm->tm_yday = yday;

	tm->tm_hour = remsecs / 3600;
	tm->tm_min = remsecs / 60 % 60;
	tm->tm_sec = remsecs % 60;

	return tm;
}

struct tm *sys_gmtime(const time_t *pt)
{
	static struct tm tm;
	return gmtime_r(pt, &tm);
}

DWORD get_fattime(void)
{
	time_t temp;
	struct tm _tm;

	time(&temp);
	gmtime_r(&temp, &_tm);

	return	  ((DWORD)(_tm.tm_year - 1980) << 25)
		| ((DWORD)_tm.tm_mon << 21)
		| ((DWORD)_tm.tm_mday << 16)
		| ((DWORD)_tm.tm_hour << 11)
		| ((DWORD)_tm.tm_min << 5)
		| ((DWORD)_tm.tm_sec >> 1);
}

FIL *sys_stderr = (FIL *)1;
FIL *sys_stdout = (FIL *)2;
FIL *sys_stdin = (FIL *)3;

long sys_fprintf(FIL *fp, const char *str, ...)
{
	long res = 0;
	va_list ap;

	if (fp == sys_stdin)
		return res;

	va_start(ap, str);

	if (fp == sys_stderr) {
		vsyslog(LOG_ERROR, str, ap);
	}
	else if (fp == sys_stdout) {
		vsyslog(LOG_NOTICE, str, ap);
	}
	else {
		res = f_vprintf(fp, str, ap);
	}

	va_end(ap);

	return res;
}

int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
	word32 i;
	for (i = 0; i < sz; i++)
		output[i] = rand();

	(void)os;

	return 0;
}
