/***************************************************************************
*   Copyright (C) 2007 by Denis A. Birukov   *
*   Denis A. Birukov@gmail.com   *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "countdown.h"
#include <time.h>
#include "video_int.h"
#include "textrenderer.h"
#include <iostream>
#include <ctime>
#include <stdio.h>
countdown::countdown()
		: effects()
{
	std::tm dat;
	dat.tm_sec = 0;
	dat.tm_min = 0;
	dat.tm_hour = 1;
	dat.tm_mday = 1;
	dat.tm_mon = 0;
	dat.tm_year = 110;
	NYT = mktime ( &dat );
}


countdown::~countdown()
{}


bool countdown::run ( int frame_no, Video_Interface* vid, float )
{
	Text_Renderer * ttf = textrenderer_get ( "ttf" );
	Video_Sprite *spr;

	static Uint32 last_time = 0;
	Uint32 t = std::time(0);
	if ( last_time == t )
	{
		vid->_add_sprite( "countdown", "maintext" );
		return true;
	}
	last_time = t;
	char str[ 200 ];
	Sint32 ds;
	VideoColor color(0x83, 0x1d, 0x8f, 0xff);
	ds = NYT - std::time ( NULL );
	if ( ds > 0 )
	{
		int days = ds / 86400;
		ds -= days * 86400;
		int hours = ds / 3600;
		ds -= hours * 3600;
		int minutes = ds / 60;
		ds -= minutes * 60;
		sprintf ( str, "До начала Нового Года: %02u:%02u:%02u !", days*24+hours, minutes, ds );
	}
	else
	{
		ds = -ds;
		int days = ds / 86400;
		ds -= days * 86400;
		int hours = ds / 3600;
		ds -= hours * 3600;
		int minutes = ds / 60;
		ds -= minutes * 60;
		sprintf ( str, "С начала 2009 года прошло: %02u:%02u:%02u !", days*24+hours, minutes, ds );
	}
	if ( !ttf ) return false;
	if ( !ttf->render_text_line( "Video", "MainFont", str, color, 25, "countdown", "maintext", true ) )
	{
		std::cerr << "Cant render text:" << ttf->_get_error() << std::endl;
		return false;
	}
	if ( !vid->_create_sprite ( "countdown", "maintext", "countdown", "maintext", true ) )
	{
		std::cerr << "Cant create sprite:" << vid->_get_error() << std::endl;
		return false;
	}

	spr = vid->_get_sprite( "countdown", "maintext" );
	if ( !spr )
	{
		std::cerr << "Cant get sprite:" << vid->_get_error() << std::endl;
		return false;
	}
	spr->_set_base_point( 0.0, 0.0 );
	spr->_set_coord( -0.97, -0.97 );
	vid->_add_sprite( "countdown", "maintext" );
	return true;
}

