#include "config.h"
#include "video.h"
#include "imageloader.h"
#include "textrenderer.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <ctime>


#include "defines.h"
void deinit()
{
	config_remove("GameConfig");
	textrenderer_remove("ttf");
	imageloader_remove("jpg");
	imageloader_remove("png");
}
bool init_font()
{
	Config_Interface *intf = config_get_interface ( "GameConfig" );
	if ( !intf )
	{
		LOG_ERROR << "Error with config"<<std::endl;
		return false;
	}
	std::string font = intf->_get_value ( "Font->MainFont", "res/fonts/font.ttf" );

	Text_Renderer* render_ttf=textrenderer_get ( "ttf" );
	if ( !render_ttf )
	{
		LOG_ERROR<<"Error while getting ttf renderer"<<std::endl;
		return false;
	}
	if ( !render_ttf->init_font ( "MainFont", font ) )
	{
		LOG_ERROR<<"Error while loading font:"<<render_ttf->_get_error() <<std::endl;
		return false;
	}
	return true;
}

bool init_loaders()
{
	if ( !imageloader_create ( "jpg", "libloader_jpg", true ) )
	{
		LOG_ERROR<<"Failed to load JPEG loader \"libloader_jpg\""<<imageloader_error() <<std::endl;
		return false;
	}
	if ( !imageloader_create ( "png", "libloader_png", true ) )
	{
		LOG_ERROR<<"Failed to load PNG loader \"libloader_png\""<<imageloader_error() <<std::endl;
		return false;
	}
	if ( !textrenderer_create ( "ttf", "libttf_renderer", true ) )
	{
		LOG_ERROR<<"Failed to load TTF renderer \"libttf_renderer\""<<textrenderer_error() <<std::endl;
		return false;
	}
	return init_font();
}
bool init()
{
	std::string mas[ 1 ];
	std::string params[ 1 ];
	params[ 0 ] = "Config";
	mas[ 0 ] = "cfg/Game.cfg";

	if ( !config_create ( "GameConfig", "libtextconfig", params, 1 ) )
	{
		LOG_ERROR<< "Error while trying to create config from libtextconfig: " << config_error() << std::endl;
		return false;
	}
	Config_Interface *intf = config_get_interface ( "GameConfig" );
	if ( !intf )
	{
		LOG_ERROR<< "Erro with config";
		return false;
	}
	intf->_load ( mas, 1 );

	if ( !init_loaders() ) return false;;

	if ( !config_create ( "VideoConfig", "libtextconfig", params, 1 ) )
	{
		LOG_ERROR<< "Error while trying to create config from libtextconfig: " << config_error() << std::endl;
		return false;
	}
	intf = config_get_interface ( "VideoConfig" );
	if ( !intf )
	{
		LOG_ERROR << "Erro with config"<<std::endl;
		return false;
	}
	mas[ 0 ] = "cfg/Video.cfg";
	intf->_load ( mas, 1 );
	std::string drv = intf->_get_value ( "Video->Driver", "libGL_mogul" );
	LOG_INFO << "Video driver: " << drv << std::endl;

	std::string params1[ 3 ];
	params1[ 0 ] = "VideoConfig";
	params1[ 1 ] = "Video->";
	params1[ 2 ] = "ME_Video";

	if ( !video_create ( "Video", drv, params1, 3 ) )
	{
		LOG_ERROR << "Error while trying to create video from " << drv << ":" << video_error() << std::endl;
		return false;
	}
	Video_Interface *vid = video_get_interface ( "Video" );
	if ( !vid ) return 1;
	if ( !vid->_init() )
	{
		LOG_ERROR << "Error while trying to initialyze video: " << vid->_get_error() << std::endl;
		return false;
	}
	return true;
}
bool run_demo(std::string video_name);
int main ( int argc, char *argv[] )
{
	if ( !init() ) return 1;
	Video_Interface *vid = video_get_interface ( "Video" );
	
	run_demo("Video");
	vid->_deinit();
	
	return 0;
}

