#pragma comment (lib, "fltk.lib")

#define _CRT_SECURE_NO_WARNINGS

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/x.H>
#include <FL/fl_ask.H>

#include <iostream>
#include <fstream>
#include <string>
#include <locale.h>
#include <windows.h>

#include "../core/ge_iface.h"
#include "../core/ge_d3d9c.h"
#include "../core/ie_iface.h"
#include "../core/ie_dinput8.h"
#include "../core/se_iface.h"
#include "../core/se_dsound.h"
#include "../core/time_util.h"
#include "../core/threads.h"
#include "../core/log.h"

#include "../indirect/indirect_iface.h"

#include "ge_adapter.h"

Fl_Thread _thread;
ge::GraphicEngineD3D9c * GE=0;
ge::ZGraphicEngineAdapter * GE_a = 0;
ie::InputEngineDInput8 * IE=0;
se::ZSoundEngineDSound * SE=0;
bool fullscreen=false;

//se::XSoundEngine * race::ZSnd::_se = 0;

//indirect::ZLevel * lvl = 0;
indirect::ZIndirectGame * game = 0;

void exit_cb(Fl_Widget * w, void * v)
{
	if (GE) delete GE;
	if (IE) delete IE;
	if (SE) delete SE;
	std::cout << "goodbye\n";

	exit(0);
}

void load_gfx()
{
	GE->load_texture(indirect::TEXTURE_LOGO,	"../data/logo.bmp",		"Null");
	GE->load_texture(indirect::TEXTURE_IGDC,	"../data/igdc.bmp",		"Null");
	GE->load_texture(indirect::TEXTURE_FIELD,	"../data/field.bmp",	"Auto");
	GE->load_texture(indirect::TEXTURE_BUTTON,	"../data/button.bmp",	"Auto");
	GE->load_texture(indirect::TEXTURE_UNIT,	"../data/unit.bmp",		"Auto");
}

void do_frame()
{
	GE_a->clear_scene(0.f, 0.f, 0.f);

	//lvl->do_input( IE );
	//lvl->do_frame();
	//lvl->draw( GE_a, IE );

	if (!game->do_input( IE ))
		exit_cb(0,0);
	game->draw( GE_a, IE );

	//GE_a->DrawString( 40, 450, "v1.1", 128, 128, 160, 128 );
	//GE_a->write( 160, 0, ((string)("fps=")+time_util::get_really_fps_s()).c_str() );
}

void * render_scene(void * p)
{
	Fl_Window * wnd = (Fl_Window *)p;
	HWND main_hWnd = fl_xid(wnd);
	GE = new ge::GraphicEngineD3D9c(main_hWnd, 640, 480, !fullscreen);
	GE_a = new ge::ZGraphicEngineAdapter( GE );

	GE_a->begin_scene();
	GE_a->clear_scene();
	GE_a->write(50,0,"Загрузка...");
	GE_a->render_scene();

	IE = new ie::InputEngineDInput8(main_hWnd);
	SE = new se::ZSoundEngineDSound(main_hWnd);

	load_gfx();
	//load_sfx();

	SE->load_music( 0, "../music/main.mp3" );

	time_util::set_fps(60);

	game = new indirect::ZIndirectGame();

	SE->run_music( 0, 1.0f, true );

	while(1)
	{
		while (!time_util::is_next_frame()) Sleep(1);

		//if (GE->DeviceIsLost()) restore_device(main_hWnd);

		Fl::lock();
		time_util::tick_frame();
		IE->read_states();
		if (Fl::focus()!=wnd) IE->clear_states();
		GE_a->begin_scene();
		SE->check_time();
		
		do_frame();

		GE_a->render_scene();
		((Fl_Window *)p)->redraw();
		Fl::unlock();
		Fl::awake(p);
	}
	return 0L;
}

int main(int argc, char **argv)
{
	setlocale(LC_ALL, "rus");
	setlocale(LC_NUMERIC, "eng");
	system("color 0A");
	system("title Консоль");

	std::cout << "Great Permutator\n";
	std::cout << "Ripatti software (c) 2012\n";
	std::cout << "Специально для igdc.ru\n";

	Fl::scheme("plastic");

	if (fl_ask("Fullscreen?")) fullscreen = true;

	Fl_Window * window = new Fl_Window(0,10,640,480,"Great Permutator");
	window->box(FL_NO_BOX);
	window->end();
	window->show(argc, argv);
	window->position(20, 20);
	window->callback(exit_cb, 0);

	Fl::lock();

	fl_create_thread(_thread, render_scene, window);

	Fl::run();

	exit_cb(0,0);

	return 0;
}