#include "Wumbo-Socket.h"
#include "TestProgram.h"
Wumbo::Program *WUMBOFRAMEWORK_GETNEWPROGRAM()
{
	return new TestProgram();
}

#include "Wumbo-Header.h"
#include "Wumbo-Scene.h"
#include "Wumbo-InputManager.h"
#include "Wumbo-KeyboardInput.h"
#include "Wumbo-Renderer.h"
#include "Wumbo-RenderTarget.h"
#include "Wumbo-Sprite.h"
#include "Wumbo-Functions.h"
#include "Wumbo-KeyboardInput.h"
#include "Wumbo-BGM.h"
#include "Wumbo-Sound.h"
//#include "scEditor.h"
#include "scLevel.h"
#include "GM.h"
#include "GM-Sprite.h"
#include "GM-Texture.h"
#include "GM-Ini.h"

int texThings = -1;
int sprDudeGuy;
#include "Wumbo-Shader.h"
#include "Wumbo-ShaderBuilder.h"
Wumbo::Shader *megaShader;



#include "Wumbo-Platforms.h"
Wumbo::TCPListener *tcp;
Wumbo::TCPSocket *client;
extern std::string superexternstring;
#include "Wumbo-Buffer.h"

#if PLATFORM_PC
bool pc = true;
#else
bool pc = false;
#endif

void TestProgram::begin() 
{
	renderer = new Wumbo::Renderer(this);
	Wumbo::Buffer *buff = new Wumbo::Buffer();
	buff->setmode(Wumbo::Buffer::WRITE);
	buff->writeuint32(123456789);
	buff->writefloat(3.141592653f);
	buff->writedouble(3.1415926535897932385);
	buff->writestring("hello i am a moose hehehe");
	buff->writebytes("HELLO\0", 6);
	buff->writetofile(Wumbo::DocumentResource("potatoes.bin"));
	delete buff;
	
	
	buff = new Wumbo::Buffer();
	buff->readfromfile(Wumbo::DocumentResource("potatoes.bin"));
	buff->setmode(Wumbo::Buffer::READ);
	unsigned int ui = buff->readuint32();
	float f = buff->readfloat();
	double d  = buff->readdouble();
	std::string s = buff->readstring();
	char b[6] = "";
	buff->readbytes(b,6);
	printf("BUFFER CONTAINS:\n%u\n%f\n%lf\n%s\n%s\n",ui,f,d,s.c_str(),b);
	
	DEBUGMSG("Hey. I'm right ehre!");
	Wumbo::ShaderBuilder builder;
	builder.loadVertexShader(Wumbo::BundleResource("Shader.vsh"));
	builder.loadFragmentShader(Wumbo::BundleResource("Shader.fsh"));
	builder.addAttrib(Wumbo::WumboVertexAttribPosition, "position");
	builder.addAttrib(Wumbo::WumboVertexAttribColor, "color");
	builder.addAttrib(Wumbo::WumboVertexAttribTexCoord0, "texcoord0");
	builder.addUniform("supermatrix");
	megaShader = builder.generate();
	if (megaShader == NULL)
	{
		megaShader = new Wumbo::Shader();
		printf("megashader is null");
	}
	program = megaShader->getProgramID();
	printf("program is %i\n",program);
	inputManager = new Wumbo::InputManager(this);
	inputManager->initialize();
	inputManager->setReceiveKeyboardUpdates(true);
	inputManager->setReceiveXbox360Updates(true);
	inputManager->setReceiveTouchUpdates(true);
	inputManager->setReceiveAccelerometerUpdates(true);
	
	
	renderer->setRotation(Wumbo::ScreenRotation_DOWN);
	//renderer->setDisplaySize(768,768);
	renderer->resetVirtualRenderTarget();
	
	//GM::ini_open("bob.ini");
	
	//GM::ini_close();

	setAssignedFrameRate(60);
	/*VirtualRenderTarget target;
	target.x = 64;
	target.y = 192;
	target.width = 256;
	target.height = 128;
	renderer->setVirtualRenderTarget(target);*/
	renderer->setTitle("Poop");
	Wumbo::Program::begin();

	
	GM::gm_wumbo_init(this);
	
	
	texThings = GM::texture_load(Wumbo::BundleResource("things.png"));
	sprDudeGuy = GM::sprite_create_with_texture(texThings);
	GM::sprite_set_subimage(sprDudeGuy, 0, 0, 0, 8, 8);
	GM::sprite_set_size(sprDudeGuy,16,16);
	
	scene = new scLevel(this);
	scene->begin();
	
	if (pc)
	{
		tcp = new Wumbo::TCPListener();
		tcp->listen(12345);
	}
	else
	{
		client = new Wumbo::TCPSocket();
		client->connect("192.168.0.3", 12345);
	}

	Wumbo::Sound *snd = new Wumbo::Sound();
	snd->load(Wumbo::BundleResource("xssspace_burial.wav"),1);
	snd->play();
	/*Wumbo::BGM *bgm = new Wumbo::BGM();
	bgm->load(Wumbo::BundleResource("word_birds.mp3"));
	bgm->play();*/
	/*window->setSize(640,480);
	window->setTitle("Test Title");*/
}
Wumbo::TCPSocket *clientSocket;

bool magicSuperMode;

void TestProgram::render()
{
#if USE_VBOS
    glUseProgram(megaShader->getProgramID());
#else
	glUseProgram(0);
#endif
	if (pc)
	{
		if (clientSocket == NULL)
		{
			clientSocket = tcp->accept();
			if (clientSocket != NULL)
			{
				Wumbo::Buffer *buff = new Wumbo::Buffer();
				buff->setbytes("i am a moose!",13);
				if (clientSocket->send(buff))
					printf("Sent successfully\n");
				else
					printf("Send failed :C\n");
				delete buff;
				//sock->close();
			}
		}
#if PLATFORM_PC
		if (clientSocket != NULL)
		if (inputManager->getKeyboardState()->isKeyPressed(sf::Keyboard::Space))
		{
			printf("BOOGLE BOO!");
			Wumbo::Buffer *buff = new Wumbo::Buffer();
			buff->setbytes("L1",2);
			clientSocket->send(buff);
			delete buff;
		}
#endif
	}
	else
	{
		if (client->connected())
		{
			superexternstring = "WOOOOO";
			Wumbo::Buffer *buff = client->receive();
			if (buff != NULL)
			{
				printf("HOST SAYS %s\n",buff->getbytes());
				if (std::string(buff->getbytes()) == "L1")
					magicSuperMode = !magicSuperMode;
				delete buff;
			}
		}
	}
	
	
	static int r = 0;
	r++;
	//Wumbo::Program::render();

	glUseProgram(program);
	glClearColor(0.f,1.f,0.f,1.f);// CLEAR BLUE
	glClear(GL_COLOR_BUFFER_BIT);

	scene->render();
	
}

void TestProgram::update()
{  
	//Wumbo::Program::update();
	
	/*if (inputManager->getXbox360State()->isConnected(0))
	{
		spr->setX(spr->getX() + 8*inputManager->getXbox360State()->getLeftThumb(0).x);
		spr->setY(spr->getY() + 8*inputManager->getXbox360State()->getLeftThumb(0).y);
		spr->recalculateVertices();
	}
 
	spr->setX(spr->getX() + 8*(inputManager->getKeyboardState()->isKeyDown(sf::Keyboard::Right)-inputManager->getKeyboardState()->isKeyDown(sf::Keyboard::Left)));
	spr->setY(spr->getY() + 8*(inputManager->getKeyboardState()->isKeyDown(sf::Keyboard::Down)-inputManager->getKeyboardState()->isKeyDown(sf::Keyboard::Up)));
	spr->recalculateVertices();*/
	/*renderer->setVirtualRenderTarget(VirtualRenderTarget(0,0,400,600));
	glClearColor(1.f,1.f,1.f,1.f);
	glClear(GL_COLOR_BUFFER_BIT);
	
	renderer->setVirtualRenderTarget(VirtualRenderTarget(64,128,256,384));
	glClearColor(0.f,0.f,0.f,1.f);
	glViewport(0,0,100,100);
	glClear(GL_COLOR_BUFFER_BIT);


	

	renderer->setVirtualRenderTarget(VirtualRenderTarget(400,0,400,600));
	glClearColor(1.f,0.f,1.f,1.f);
	glClear(GL_COLOR_BUFFER_BIT);*/
}

void TestProgram::end()
{
	Wumbo::Program::end();
#if PLATFORM_PC
	delete sfmlwindow;
#endif
}