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

#include "tdefs.h"
#include "misc.h"

#include "helper.h"
#include "dec_month.h"
#include <time.h>
//volatile struct klapacz_args tkl;

int dec_month_initlog(void *arguments, void *logic)
{
	struct dec_month_args *args = (struct dec_month_args *)arguments;
	struct dec_month_logic *log = (struct dec_month_logic *)logic;

	if(set_bit_ptr_struct(args->io.sty,&log->mon[0]))
		return -1;
	if(set_bit_ptr_struct(args->io.lut,&log->mon[1]))
		return -1;
	if(set_bit_ptr_struct(args->io.marz,&log->mon[2]))
		return -1;
	if(set_bit_ptr_struct(args->io.kwie,&log->mon[3]))
		return -1;
	if(set_bit_ptr_struct(args->io.maj,&log->mon[4]))
		return -1;
	if(set_bit_ptr_struct(args->io.czer,&log->mon[5]))
		return -1;
	if(set_bit_ptr_struct(args->io.lip,&log->mon[6]))
		return -1;
	if(set_bit_ptr_struct(args->io.sier,&log->mon[7]))
		return -1;
	if(set_bit_ptr_struct(args->io.wrz,&log->mon[8]))
		return -1;
	if(set_bit_ptr_struct(args->io.paz,&log->mon[9]))
		return -1;
	if(set_bit_ptr_struct(args->io.lis,&log->mon[10]))
		return -1;
	if(set_bit_ptr_struct(args->io.gru,&log->mon[11]))
		return -1;
	if(set_bit_ptr_struct(args->io.zima,&log->zima))
		return -1;
	if(set_bit_ptr_struct(args->io.lato,&log->lato))
		return -1;

	return 0;
}

extern void my_gmtime(const uint32_t *timer, struct tm *timep);
extern uint8_t isindst(struct tm *tb, uint8_t cest_cet_n);

void dec_month(void *arguments, void *logic)
{
//	struct dec_month_args *args = (struct dec_month_args *)arguments;
	struct dec_month_logic *log = (struct dec_month_logic *)logic;
    struct tm tim;
    u8 i;

	my_gmtime((const uint32_t*)&cur_time.tv_sec,&tim);

	for(i=0;i<12;i++)
		check_and_set_struct(tim.tm_mon==i,&log->mon[i]);

	if(isindst(&tim,0))
	{
		set_struct(&log->lato);
		clear_struct(&log->zima);
	}
	else
	{
		set_struct(&log->zima);
		clear_struct(&log->lato);
	}
	/*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
	*/

	
}
