#define SFML_STATIC
#define THISISTHEMAIN_CPP
#include <cmath>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "cGame.h"
#include "cPlayer.h"
#include "MediaVars.h"

#pragma comment(lib,"sfml-system-s.lib")
#pragma comment(lib,"sfml-window-s.lib")
#pragma comment(lib,"sfml-graphics-s.lib")
#pragma comment(lib,"sfml-audio-s.lib")

sf::RenderWindow App;

double point_distance(double x1, double y1, double x2, double y2)
{
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double point_direction(double x1, double y1, double x2, double y2)
{
	return atan2(y2-y1,x2-x1);// sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}


void draw_text(double x, double y, std::string str, double scale)
{
	double xn, yn;
	sf::Sprite spr;
	spr.SetImage(imgFont);
	spr.SetScale((float)scale,(float)scale);
	xn = yn = 0;
	for(unsigned int i=0;i<str.length();i++)
	{
		int subx, suby, subpos;
		subx = suby = subpos = 0;
		if (str[i] == '\n')
		{
			xn = 0;
			yn += 32*scale;
			continue;
		}
		else if (str[i] == ' ')
		{
			xn += 32*scale;
			continue;
		}
		else if (str[i] == '.' || str[i] == '!' || str[i] == '-' || str[i] == '>' || str[i] == ':' || str[i] == '/' || str[i] == ',' || str[i] == 39 /* ' */ || str[i] == '?')
		{ 
			suby = 256-32;
			if (str[i] == '.')
				subx = 0;
			else if (str[i] == '!')
				subx = 32;
			else if (str[i] == '-')
				subx = 64;
			else if (str[i] == '>')
				subx = 96;
			else if (str[i] == '/')
				subx = 128;
			else if (str[i] == ':')
				subx = 160;
			else if (str[i] == ',')
				subx = 192;
			else if (str[i] == 39)
				subx = 224;
			else if (str[i] == '?')
			{
				subx = 224;
				suby = 256-64;
			}
			spr.SetSubRect(sf::IntRect(subx,suby,subx+32,suby+32));
		}
		else if (str[i] >= '0' && str[i] <= '9')
		{
			subpos = str[i]-'0';
			subx = subpos % 8;
			suby = subpos / 8;
			spr.SetSubRect(sf::IntRect(32*subx,32*suby,32*subx+32,32*suby+32));
		}
		else if (str[i] >= 'A' && str[i] <= 'Z')
		{
			subpos = (str[i]-'A')+10;
			subx = subpos % 8;
			suby = subpos / 8;
			spr.SetSubRect(sf::IntRect(32*subx,32*suby,32*subx+32,32*suby+32));
		}
		else if (str[i] >= 'a' && str[i] <= 'z')
		{
			subpos = (str[i]-'a')+10;
			subx = subpos % 8;
			suby = subpos / 8;
			spr.SetSubRect(sf::IntRect(32*subx,32*suby,32*subx+32,32*suby+32));
		}
		//spr.SetSubRect(sf::IntRect(0,0,32,32));
		spr.SetPosition(x+xn,y+yn);
		App.Draw(spr);
		xn += 32*scale;
	}
}



sf::Sprite *LoadSprite(string filename, sf::Image &img)
{
#ifdef WIN32
	filename = "./media/gfx/"+filename;
#endif
	if (!img.LoadFromFile(filename))
		exit(42);
	img.SetSmooth(false);
	sf::Sprite *spr = new sf::Sprite();
	spr->SetImage(img);
	return spr;
}

sf::Sound *LoadSound(string filename, sf::SoundBuffer &bfr)
{
#ifdef WIN32
	filename = "./media/sfx/"+filename;
#endif
	if (!bfr.LoadFromFile(filename))
		exit(42);
	sf::Sound *snd = new sf::Sound();
	snd->SetBuffer(bfr);
	return snd;
}

#ifndef WIN32
int main()
#endif
#ifdef WIN32
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
#endif
{
	soundIsMuted = false;
	// Sound
	sndChase = LoadSound("chase.wav",bfrChase);
	sndFall = LoadSound("fall.wav",bfrFall);
	sndGetHeart = LoadSound("getheart.wav",bfrGetHeart);
	sndGetBrain = LoadSound("getbrain.wav",bfrGetBrain);
	sndHurt = LoadSound("hurt.wav",bfrHurt);
	sndDestroy = LoadSound("destroy.wav",bfrDestroy);
	// Graphics
	sprZombieGhost = LoadSprite("zombie_ghost.png",imgZombieGhost);
	sprPlayer0 = LoadSprite("player0.png",imgPlayer0);
	sprPlayerWithBrain0 = LoadSprite("player_hasbrain0.png",imgPlayerWithBrain0);
	sprPlayer1 = LoadSprite("player1.png",imgPlayer1);
	sprPlayerWithBrain1 = LoadSprite("player_hasbrain1.png",imgPlayerWithBrain1);
	sprPlayer2 = LoadSprite("player2.png",imgPlayer2);
	sprPlayerWithBrain2 = LoadSprite("player_hasbrain2.png",imgPlayerWithBrain2);
	sprBrick = LoadSprite("brick.png",imgBrick);
	sprUnderBrick = LoadSprite("underbrick.png",imgUnderBrick);
	sprBrain = LoadSprite("brain.png",imgBrain);
	sprShadow = LoadSprite("shadow.png",imgShadow);
	sprHole = LoadSprite("hole.png",imgHole);
	sprUnderHole = LoadSprite("underhole.png",imgUnderHole);
	sprExclaim = LoadSprite("exclaim.png",imgExclaim);
	sprQuestion = LoadSprite("question.png",imgQuestion);
	sprHeart = LoadSprite("heart.png",imgHeart);
	sprHeartFade = LoadSprite("heart_fade.png",imgHeartFade);
	sprGrass = LoadSprite("grass.png",imgGrass);
	sprUnderGrass = LoadSprite("undergrass.png",imgUnderGrass);
	sprFont = LoadSprite("font.png",imgFont);
	sprSign = LoadSprite("sign.png",imgSign);
	sprDoor = LoadSprite("door.png",imgDoor);
	sprBrainBubble = LoadSprite("brainbubble.png",imgBrainBubble);
	sprLose = LoadSprite("lose.png",imgLose);
	sprTitle = LoadSprite("title.png",imgTitle);
	sprWin = LoadSprite("win.png",imgWin);
	sprWinHasBrain = LoadSprite("win_hasbrain.png",imgWinHasBrain);
	sprStory1 = LoadSprite("story1.png",imgStory1);
	sprStory2 = LoadSprite("story2.png",imgStory2);
	sprWinStory = LoadSprite("gamefinished.png",imgWinStory);
	sprSoundOn = LoadSprite("sound_on.png",imgSoundOn);
	sprSoundOff = LoadSprite("sound_off.png",imgSoundOff);
	bool exitgame = false;
	bool isWindowActive = false;
	App.Create(sf::VideoMode(640,480,2), "Organs and Marriage", sf::Style::Close);
	cGame *game = new cGame(&App);
	App.SetFramerateLimit(60);
	game->Start();
	while(!exitgame)
	{
		sf::Event event;
		while (App.GetEvent(event))
		{
			if ((event.Type == sf::Event::Closed) || ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape)))
			{
				exitgame = true;
				break;
			}
			if (event.Type == sf::Event::GainedFocus)
				isWindowActive = true;
			if (event.Type == sf::Event::LostFocus)
				isWindowActive = false;
		}
		App.Clear(sf::Color(192,192,192));
		game->Do();
		App.Display();
		if (game->IsGameRestarted())
		{
			delete game;
			game = new cGame(&App);
			game->Start();
		}
	}
	game->End();
	App.Close();
	return EXIT_SUCCESS;
	}