/*
 * klapacz.c
 *
 *  Created on: 07-04-2014
 *      Author: Krzysztof Jakubczyk
 */

#include "tdefs.h"
#include "misc.h"
#include <time.h>

#include "helper.h"
#include "prog_czas.h"

//volatile struct klapacz_args tkl;

int prog_czas_initlog(void *arguments, void *logic)
{
	struct prog_czas_args *args = (struct prog_czas_args *)arguments;
	struct prog_czas_logic *log = (struct prog_czas_logic *)logic;


	if(set_bit_ptr_struct(args->io.pon,&log->pon))
		return -1;
	if(set_bit_ptr_struct(args->io.wt,&log->wt))
		return -1;
	if(set_bit_ptr_struct(args->io.sr,&log->sr))
		return -1;
	if(set_bit_ptr_struct(args->io.czw,&log->czw))
		return -1;
	if(set_bit_ptr_struct(args->io.pt,&log->pt))
		return -1;
	if(set_bit_ptr_struct(args->io.sob,&log->sob))
		return -1;
	if(set_bit_ptr_struct(args->io.niedz,&log->niedz))
		return -1;

	return 0;
}

void prog_czas(void *arguments, void *logic)
{
//	struct prog_czas_args *args = (struct prog_czas_args *)arguments;
	struct prog_czas_logic *log = (struct prog_czas_logic *)logic;

	u8 day_of_week;

	day_of_week = (cur_time.tv_sec / 86400 + 4)%7;
	check_and_set_struct((day_of_week==0),&log->niedz);
	check_and_set_struct((day_of_week==1),&log->pon);
	check_and_set_struct((day_of_week==2),&log->wt);
	check_and_set_struct((day_of_week==3),&log->sr);
	check_and_set_struct((day_of_week==4),&log->czw);
	check_and_set_struct((day_of_week==5),&log->pt);
	check_and_set_struct((day_of_week==6),&log->sob);
	//check_and_set_struct((day_of_week==7),&log->niedz);

	/*
		local function GetMonth(seconds)
	    local dayduration,year = 3600*24
	    local days={31,0,31,30,31,30,31,31,30,31,30,31}
	    for i=1970,10000 do -- For some reason too lazy to use while
	        local yeardays = i%4 == 0 and i%100 ~= 0 and 366 or 365
	        local yearduration = dayduration * yeardays
	        if yearduration < seconds then
	            seconds = seconds - yearduration
	        else
	            year = i break
	        end
	    end
	    days[2]=(year%4==0) and 29 or 28
	    seconds = seconds%(365*24*3600)
	    for i=1,12 do
	        if seconds>days[i]*dayduration then
	            seconds=seconds-days[i]*dayduration
	        else
	            return --i + year*12  <-- If you want a unique ID
	        end
	    end
	end
	*/

	
}
