#include <string>
#include "config.h"
#include "video.h"
#include "imageloader.h"
#include "textrenderer.h"
#include "video_int.h"
#include "me_time.h"
#include <iostream>
#define TIME_DELTA 2.6
#define FADE_IN(source) source->_set_alpha( 1.0*(dt/TIME_DELTA))
#define FADE_OUT(source) source->_set_alpha( 1.0 - 1.0*(dt/TIME_DELTA))

bool intro_oldnyps(Video_Interface *vid, std::string vid_name);
bool intro_name(Video_Interface *vid, std::string vid_name);

bool intro(Video_Interface *vid, std::string vid_name)
{
	if(!intro_name(vid, vid_name)) return false;
	if(!intro_oldnyps(vid, vid_name)) return false;
	return true;
}

bool intro_name(Video_Interface *vid, std::string vid_name)
{
	Text_Renderer *ttf=textrenderer_get ( "ttf" );
	Video_Sprite *author, *text, *aerton;
	VideoColor color(0xff, 0xff, 0xff, 0xff);
	Time timer;
	if(!ttf->render_text_line(vid_name, "MainFont", "MESSENGER OF DEATH", color, 55, "author", "intro", false))
	{
		std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
		return false;
	}
	if(!ttf->render_text_line(vid_name, "MainFont", "представляет", color, 24, "text", "intro", false))
	{
		std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
		return false;
	}

	if(!vid->_create_sprite("author", "intro", "author", "intro", true))
	{
		std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
		return false;
	}
	if(!vid->_create_sprite("text", "intro", "text", "intro", true))
	{
		std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
		return false;
	}
	
	if(!ttf->render_text_line(vid_name, "MainFont", "при помощи Aerton'а", color, 30, "aerton", "intro", false))
	{
		std::cerr<<"Cant render text:"<<ttf->_get_error()<<std::endl;
		return false;
	}
	if(!vid->_create_sprite("aerton", "intro", "aerton", "intro", true))
	{
		std::cerr<<"Cant create sprite:"<<vid->_get_error()<<std::endl;
		return false;
	}

	author = vid->_get_sprite( "author", "intro");
	author->_set_base_point(0.5, 0.0);
	author->_set_coord(0.0, 0.0);
	aerton = vid->_get_sprite( "aerton", "intro");
	aerton->_set_base_point(0.5, 0.0);
	aerton->_set_coord(0.0, -(double)(author->_get_height())/768.);
	text = vid->_get_sprite( "text", "intro");
	text->_set_base_point(0.5, 0.0);
	text->_set_coord(0.0, aerton->_get_y()-(double)(aerton->_get_height())/384.);
	timer.reset(); //TIME_DELTA
	float dt;
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_IN(author);
		FADE_IN(text);
		FADE_IN(aerton);
		if(!vid->_add_sprite("author", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return 1;
		}
		if(!vid->_add_sprite("text", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return 1;
		}
		if(!vid->_add_sprite("aerton", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return 1;
		}
		vid->_draw();
	} 
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_OUT(author);
		FADE_OUT(text);
		FADE_OUT(aerton);
		if(!vid->_add_sprite("author", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("text", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("aerton", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	} 
	timer.reset();
	
	
	return true;
}

bool intro_oldnyps(Video_Interface *vid, std::string vid_name)
{
	Text_Renderer *ttf=textrenderer_get ( "ttf" );
	Video_Sprite *nyp2004_text, *nyp2004_image;
	Video_Sprite *nyp2005_text, *nyp2005_image;
	Video_Sprite *nyp2006_text, *nyp2006_image;
	Video_Sprite *nyp2007_text, *nyp2007_image;
	Video_Sprite *nyp2008_text, *nyp2008_image;
	Video_Sprite *nyp2009_text, *nyp2009_image;
	Video_Sprite *nyp2010_text;
	VideoColor color(0xff, 0xff, 0xff, 0xff);
	Time timer;
	//Init text lines
	try
	{
		if(!ttf->render_text_line(vid_name, "MainFont", "NYP2004", color, 120, "nyp2004", "intro", false))throw(ttf->_get_error());
		if(!ttf->render_text_line(vid_name, "MainFont", "NYP2005", color, 120, "nyp2005", "intro", false))throw(ttf->_get_error());
		if(!ttf->render_text_line(vid_name, "MainFont", "NYP2006", color, 120, "nyp2006", "intro", false))throw(ttf->_get_error());
		if(!ttf->render_text_line(vid_name, "MainFont", "NYP2007", color, 120, "nyp2007", "intro", false))throw(ttf->_get_error());
		if(!ttf->render_text_line(vid_name, "MainFont", "NYP2008", color, 120, "nyp2008", "intro", false))throw(ttf->_get_error());
		if(!ttf->render_text_line(vid_name, "MainFont", "NYP2009", color, 120, "nyp2009", "intro", false))throw(ttf->_get_error());
		if(!ttf->render_text_line(vid_name, "MainFont", "NYP2010", color, 120, "nyp2010", "intro", false))throw(ttf->_get_error());
		if(!vid->_create_sprite("nyp2004", "intro", "nyp2004", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2005", "intro", "nyp2005", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2006", "intro", "nyp2006", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2007", "intro", "nyp2007", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2008", "intro", "nyp2008", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2009", "intro", "nyp2009", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2010", "intro", "nyp2010", "intro", true))throw(vid->_get_error());
		nyp2004_text = vid->_get_sprite( "nyp2004", "intro");
		nyp2005_text = vid->_get_sprite( "nyp2005", "intro");
		nyp2006_text = vid->_get_sprite( "nyp2006", "intro");
		nyp2007_text = vid->_get_sprite( "nyp2007", "intro");
		nyp2008_text = vid->_get_sprite( "nyp2008", "intro");
		nyp2009_text = vid->_get_sprite( "nyp2009", "intro");
		nyp2010_text = vid->_get_sprite( "nyp2010", "intro");
	}catch(std::string value)
	{
		std::cerr<<"Can't render text:"<<value<<std::endl;
		return false;
	}
	//Preload images
	try
	{
		if(!imageloader_loadimage ( "jpg", vid_name, "res/images/nyp2004.jpg", "intro", "nyp2004_img" ))imageloader_error();
		if(!imageloader_loadimage ( "jpg", vid_name, "res/images/nyp2005.jpg", "intro", "nyp2005_img" ))imageloader_error();
		if(!imageloader_loadimage ( "jpg", vid_name, "res/images/nyp2006.jpg", "intro", "nyp2006_img" ))imageloader_error();
		if(!imageloader_loadimage ( "jpg", vid_name, "res/images/nyp2007.jpg", "intro", "nyp2007_img" ))imageloader_error();
		if(!imageloader_loadimage ( "jpg", vid_name, "res/images/nyp2008.jpg", "intro", "nyp2008_img" ))imageloader_error();
		if(!imageloader_loadimage ( "jpg", vid_name, "res/images/nyp2009.jpg", "intro", "nyp2009_img" ))imageloader_error();
		if(!vid->_create_sprite("nyp2004_img", "intro", "nyp2004_img", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2005_img", "intro", "nyp2005_img", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2006_img", "intro", "nyp2006_img", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2007_img", "intro", "nyp2007_img", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2008_img", "intro", "nyp2008_img", "intro", true))throw(vid->_get_error());
		if(!vid->_create_sprite("nyp2009_img", "intro", "nyp2009_img", "intro", true))throw(vid->_get_error());
		nyp2004_image = vid->_get_sprite( "nyp2004_img", "intro");
		nyp2005_image = vid->_get_sprite( "nyp2005_img", "intro");
		nyp2006_image = vid->_get_sprite( "nyp2006_img", "intro");
		nyp2007_image = vid->_get_sprite( "nyp2007_img", "intro");
		nyp2008_image = vid->_get_sprite( "nyp2008_img", "intro");
		nyp2009_image = vid->_get_sprite( "nyp2009_img", "intro");
	}catch(std::string value)
	{
		std::cerr<<"Error loading image:"<<value<<std::endl;
		return false;
	}
	nyp2004_image->_set_base_point( 0.5, 0.5 );
	nyp2004_image->_set_coord( 0, 0 );
	nyp2004_text->_set_base_point( 0.5, 0.5 );
	nyp2004_text->_set_coord( 0, 0 );
	nyp2005_image->_set_base_point( 0.5, 0.5 );
	nyp2005_image->_set_coord( 0, 0 );
	nyp2005_text->_set_base_point( 0.5, 0.5 );
	nyp2005_text->_set_coord( 0, 0 );
	nyp2006_image->_set_base_point( 0.5, 0.5 );
	nyp2006_image->_set_coord( 0, 0 );
	nyp2006_text->_set_base_point( 0.5, 0.5 );
	nyp2006_text->_set_coord( 0, 0 );
	nyp2007_image->_set_base_point( 0.5, 0.5 );
	nyp2007_image->_set_coord( 0, 0 );
	nyp2007_text->_set_base_point( 0.5, 0.5 );
	nyp2007_text->_set_coord( 0, 0 );
	nyp2008_image->_set_base_point( 0.5, 0.5 );
	nyp2008_image->_set_coord( 0, 0 );
	nyp2008_text->_set_base_point( 0.5, 0.5 );
	nyp2008_text->_set_coord( 0, 0 );
	nyp2009_image->_set_base_point( 0.5, 0.5 );
	nyp2009_image->_set_coord( 0, 0 );
	nyp2009_text->_set_base_point( 0.5, 0.5 );
	nyp2009_text->_set_coord( 0, 0 );
	nyp2010_text->_set_base_point( 0.5, 0.5 );
	nyp2010_text->_set_coord( 0, 0 );
	timer.reset();
	float dt;
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_IN(nyp2004_image);
		if(!vid->_add_sprite("nyp2004_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_IN(nyp2004_text);
		if(!vid->_add_sprite("nyp2004_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2004", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_OUT(nyp2004_text);
		FADE_OUT(nyp2004_image);
		FADE_IN(nyp2005_image);
		if(!vid->_add_sprite("nyp2004_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2004", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2005_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_IN(nyp2005_text);
		if(!vid->_add_sprite("nyp2005_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2005", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_OUT(nyp2005_text);
		FADE_OUT(nyp2005_image);
		FADE_IN(nyp2006_image);
		if(!vid->_add_sprite("nyp2005_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2005", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2006_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}

	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_IN(nyp2006_text);
		if(!vid->_add_sprite("nyp2006_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2006", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_OUT(nyp2006_text);
		FADE_OUT(nyp2006_image);
		FADE_IN(nyp2007_image);
		if(!vid->_add_sprite("nyp2006_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2006", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2007_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}


	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_IN(nyp2007_text);
		if(!vid->_add_sprite("nyp2007_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2007", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_OUT(nyp2007_text);
		FADE_OUT(nyp2007_image);
		FADE_IN(nyp2008_image);
		if(!vid->_add_sprite("nyp2007_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2007", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2008_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_IN(nyp2008_text);
		if(!vid->_add_sprite("nyp2008_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2008", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_OUT(nyp2008_text);
		FADE_OUT(nyp2008_image);
		FADE_IN(nyp2009_image);
		if(!vid->_add_sprite("nyp2008_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2008", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2009_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_IN(nyp2009_text);
		if(!vid->_add_sprite("nyp2009_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2009", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_OUT(nyp2009_text);
		FADE_OUT(nyp2009_image);
		FADE_IN(nyp2010_text);
		if(!vid->_add_sprite("nyp2009_img", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2009", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		if(!vid->_add_sprite("nyp2010", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	timer.reset();
	while((dt=timer.get_elapsed_time_noreset())<TIME_DELTA)
	{
		FADE_OUT(nyp2010_text);
		if(!vid->_add_sprite("nyp2010", "intro"))
		{
			std::cerr<<"Error putting surface:"<<vid->_get_error()<<std::endl;
			return false;
		}
		vid->_draw();
	}
	
	return true;	
}
