/***************************************************************************
 *   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 "text.h"
#include <fstream>
#include "video_int.h"
#include "textrenderer.h"
#include <iostream>
#include <stdlib.h>

text::text()
		: effects()
{}


text::~text()
{}

int MAGIC=0;
bool text::prepare ( std::string video_name, Video_Interface* vid )
{
	std::ifstream in( "res/text/data.txt" );
	if ( !in.seekg( MAGIC ) ) return false;
	int a;
	for ( a = 0;std::getline( in, strings[ a ] );a++ )
	{
//		strings[a][strings[a].length()-1]=0;
	}
	str_num = a;
	cur_num = 0;
	in.close();
	VideoColor color(0x83, 0x1d, 0x8f, 0xff);
	Text_Renderer* ttf=textrenderer_get ( "ttf" );
	if(!ttf)return false;	
	if(!ttf->render_text_line("Video", "MainFont", strings[cur_num], color, 40, "Text1", "maintext", true))
	{
		std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
		return false;
	}

	if(!vid->_create_sprite("Text1", "maintext", "Text1", "maintext", true))
	{
		std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
		return false;
	}
	if(!ttf->render_text_line("Video", "MainFont", strings[cur_num+1], color, 40, "Text2", "maintext", true))
	{
		std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
		return false;
	}

	if(!vid->_create_sprite("Text2", "maintext", "Text2", "maintext", true))
	{
		std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
		return false;
	}
	if(!ttf->render_text_line("Video", "MainFont", strings[cur_num+2], color, 40, "Text3", "maintext", true))
	{
		std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
		return false;
	}
	if(!vid->_create_sprite("Text3", "maintext", "Text3", "maintext", true))
	{
		std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
		return false;
	}
	if(!ttf->render_text_line("Video", "MainFont", strings[cur_num+3], color, 40, "Text4", "maintext", true))
	{
		std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
		return false;
	}
	if(!vid->_create_sprite("Text4", "maintext", "Text4", "maintext", true))
	{
		std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
		return false;
	}
	timer.reset();
	return true;

}

bool text::run ( int frame_no, Video_Interface* vid, float )
{
	Video_Sprite *spr;
	static double x=(double)(-500+(rand()%400))/512.;
	static double y=(double)(-200+rand()%558)/384.;
	double dy;
	VideoColor color(0x83, 0x1d, 0x8f, 0xff);
	Text_Renderer* ttf=textrenderer_get ( "ttf" );
	
	float dt = timer.get_elapsed_time_noreset();
	if(dt<1.5)
	{
		int a1 = 255*(dt/1.5);
		int z = a1*2;
		int z1 = ( z > 10 ) ? z - 10 : 0;
		int z2 = ( z > 20 ) ? z - 20 : 0;
		int z3 = ( z > 30 ) ? z - 30 : 0;
		spr=vid->_get_sprite("Text1", "maintext");
		spr->_set_coord(x, y);
		spr->_set_alpha((float)z/255.);
		vid->_add_sprite("Text1", "maintext");
		dy = (double)(spr->_get_height())/384.;

		spr=vid->_get_sprite("Text2", "maintext");
		spr->_set_coord(x, y-dy);
		spr->_set_alpha((float)z1/255.);
		vid->_add_sprite("Text2", "maintext");
		dy += (double)(spr->_get_height())/384.;

		
		spr=vid->_get_sprite("Text3", "maintext");
		spr->_set_coord(x, y-dy);
		spr->_set_alpha((float)z2/255.);
		vid->_add_sprite("Text3", "maintext");
		dy += (double)(spr->_get_height())/384.;
		
		spr=vid->_get_sprite("Text4", "maintext");
		spr->_set_coord(x, y-dy);
		spr->_set_alpha((float)z3/255.);
		vid->_add_sprite("Text4", "maintext");
		dy += (double)(spr->_get_height())/384.;
	}
	else if(dt<4.0)
	{
		spr=vid->_get_sprite("Text1", "maintext");
		spr->_set_coord(x, y);
		vid->_add_sprite("Text1", "maintext");
		dy = (double)(spr->_get_height())/384.;

		spr=vid->_get_sprite("Text2", "maintext");
		spr->_set_coord(x, y-dy);
		vid->_add_sprite("Text2", "maintext");
		dy += (double)(spr->_get_height())/384.;

		
		spr=vid->_get_sprite("Text3", "maintext");
		spr->_set_coord(x, y-dy);
		vid->_add_sprite("Text3", "maintext");
		dy += (double)(spr->_get_height())/384.;
		
		spr=vid->_get_sprite("Text4", "maintext");
		spr->_set_coord(x, y-dy);
		vid->_add_sprite("Text4", "maintext");
		dy += (double)(spr->_get_height())/384.;
	}
	else if(dt<5.0)
	{
		int a1 = 255*(5.0-dt);
		int z = a1*2;
		int z1 = ( z > 10 ) ? z - 10 : 0;
		int z2 = ( z > 20 ) ? z - 20 : 0;
		int z3 = ( z > 30 ) ? z - 30 : 0;
		spr=vid->_get_sprite("Text1", "maintext");
		spr->_set_coord(x, y);
		spr->_set_alpha((float)z/255.);
		vid->_add_sprite("Text1", "maintext");
		dy = (double)(spr->_get_height())/384.;

		spr=vid->_get_sprite("Text2", "maintext");
		spr->_set_coord(x, y-dy);
		spr->_set_alpha((float)z1/255.);
		vid->_add_sprite("Text2", "maintext");
		dy += (double)(spr->_get_height())/384.;
		
		spr=vid->_get_sprite("Text3", "maintext");
		spr->_set_coord(x, y-dy);
		spr->_set_alpha((float)z2/255.);
		vid->_add_sprite("Text3", "maintext");
		dy += (double)(spr->_get_height())/384.;
		
		spr=vid->_get_sprite("Text4", "maintext");
		spr->_set_coord(x, y-dy);
		spr->_set_alpha((float)z3/255.);
		vid->_add_sprite("Text4", "maintext");		
		dy += (double)(spr->_get_height())/384.;
	}
	else
	{
		x=(double)(-500+(rand()%400))/512.;
		y=(double)(-200+rand()%558)/384.;
		cur_num+=4;
		if(cur_num+3>str_num)cur_num=0;
		if(!ttf->render_text_line("Video", "MainFont", strings[cur_num], color, 40, "Text1", "maintext", true))
		{
			std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
			return false;
		}

		if(!vid->_create_sprite("Text1", "maintext", "Text1", "maintext", true))
		{
			std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!ttf->render_text_line("Video", "MainFont", strings[cur_num+1], color, 40, "Text2", "maintext", true))
		{
			std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
			return false;
		}

		if(!vid->_create_sprite("Text2", "maintext", "Text2", "maintext", true))
		{
			std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!ttf->render_text_line("Video", "MainFont", strings[cur_num+2], color, 40, "Text3", "maintext", true))
		{
			std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_create_sprite("Text3", "maintext", "Text3", "maintext", true))
		{
			std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!ttf->render_text_line("Video", "MainFont", strings[cur_num+3], color, 40, "Text4", "maintext", true))
		{
			std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_create_sprite("Text4", "maintext", "Text4", "maintext", true))
		{
			std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
			return false;
		}
		timer.reset();
	}
	return true;
}

