#include "..\core\Engine.h"
#include "..\core\Snd.h"
#include "Objects.h"
#include "Gameplay.h"
#include <fstream>

enum E_SCENE_TYPE {ST_TITLE, ST_MENU, ST_INTRO, ST_GAME, ST_ENDING};

E_SCENE_TYPE scene_type = ST_TITLE;

const char* story_txt[] = {"Long time ago...", "In a virgin beautiful world...", "Under the blue sky, lived...", "Pixel...", "and Texel.", "They are in love!", "So beautiful!",
	"But...", "Not so far away lived...", "Evil Voxel!", "And one day...", "Voxel kidnapped Texel!", "So, our story starts here..."};

const char* tutorial_txt[] = {"Use \"Arrows\" to move.", "Use \"Space\" to shoot.", "Kill enemies to earn score.", "Each shot costs score!", "So, try shoot less.", "Good luck!"};

const float water_coord[] = {-0.1, -0.24, -0.1, -0.3, 0.27, 0.06, -0.15, 0.11, 0.02, -0.24, 0.3, 0.2};

const float romashki[] = {0.5, -0.3, 0.4, -0.55, 0.f, -0.55, 0.25, -0.35, 0.8, -0.5};

unsigned int counter = 0, cerdce_counter = 0, blink = 0;

int bestscore = 0;

extern int lives, score, voxel_counter;
extern unsigned int tmp_counter;
extern unsigned int move;

Snd *pShot = NULL, *pBang = NULL, *pBoom = NULL, *pDie = NULL, *pPixel = NULL, *pTexel = NULL, *pVoxel = NULL, *pVopite = NULL;

void SaveBestScore()
{
	if(score > bestscore)
	{
		bestscore = score;
		std::ofstream a("score.dat");
		if(a.is_open())
		{
			a.write((char*)&bestscore, sizeof(int));
			a.close();
		}
	}
}

void StartScene(E_SCENE_TYPE type)
{
	scene_type = type;
	counter = 0;
	cerdce_counter = 0;
	blink = 0;
	move = 0;

	if(scene_type == ST_ENDING)
		SaveBestScore();

	if(scene_type == ST_GAME)
		StartGameplay();
}

bool Init()
{
	std::ifstream a("score.dat");
	if(a.is_open())
	{
		a.read((char*)&bestscore, sizeof(int));
		a.close();
	}

	PlayMusic();

	if(InitializeOpenAL())
	{
		pShot = new Snd("Data\\shot.wav");
		pBang = new Snd("Data\\bang.wav");
		pBoom = new Snd("Data\\boom.wav");
		pDie = new Snd("Data\\die.wav");
		pPixel = new Snd("Data\\pixel.wav");
		pTexel = new Snd("Data\\texel.wav");
		pVoxel = new Snd("Data\\voxel.wav");
		pVopite = new Snd("Data\\vopite.wav");
	}

	StartScene(scene_type);

	return true;
}

bool Free()
{
	CloseMusic();

	if(pShot) delete pShot;
	if(pBang) delete pBang;
	if(pBoom) delete pBoom;
	if(pDie) delete pDie;
	if(pPixel) delete pPixel;
	if(pTexel) delete pTexel;
	if(pVoxel) delete pVoxel;
	if(pVopite) delete pVopite;

	DestroyOpenAL();
	return true;
}

bool Process(bool keys[256])
{
	switch(scene_type)
	{
	case ST_TITLE:
		if((counter > 100 && keys[KEY_ACTION]) || counter > 500) StartScene(ST_MENU);
		break;
	case ST_MENU:
		if(counter > 500 && keys[KEY_ACTION]) StartScene(ST_INTRO);
		if(pVopite && counter == 500) pVopite->Play();
		break;
	case ST_INTRO:
		if(counter == 900 && pPixel) pPixel->Play();
		if(counter == 1200 && pTexel) pTexel->Play();
		if(counter == 2700 && pVoxel) pVoxel->Play();

		if((counter > 100 && keys[KEY_ACTION]) || counter == 4300) StartScene(ST_GAME);
		if(counter == 3000 || counter == 1700) cerdce_counter = 1;
		if(cerdce_counter != 0) cerdce_counter++;
		if(cerdce_counter > 500) cerdce_counter = 0;
		if(counter == 100 || counter == 200 || counter == 300 || counter == 500 || /*counter == 700 ||*/ counter == 900 || counter == 1200 
			|| counter == 1800 || counter == 2700)
			blink = 80;
		break;
	case ST_GAME:
		
		if(voxel_counter == 150) StartScene(ST_ENDING);

		ProcessGameplay(keys);
		if(lives == -1 && ((keys[KEY_ACTION] && counter - tmp_counter > 100)|| counter%1000 == 0)) StartScene(ST_MENU);

		break;
	case ST_ENDING:
		break;
	}

	if(blink > 0) blink--;

	counter++;
	return true;
}

bool Render()
{
	glColor4f(1.f, 1.f, 1.f, 1.f);

	if(scene_type != ST_TITLE)
	{
		glPushMatrix();
		glTranslatef(0, 0, -2.f);
		glScalef(10.f, 1.8f, 1.f);
		glBegin(GL_QUADS);
		glColor3ub(107, 223, 255);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glColor3ub(61, 142, 255);
		glVertex2f(0.5, -0.5);
		glVertex2f(-0.5, -0.5);
		glEnd();
		glPopMatrix();
		glColor4f(1.f, 1.f, 1.f, 1.f);
		if(scene_type == ST_INTRO)
		glClear(GL_DEPTH_BUFFER_BIT);
	}

	int i;

	switch(scene_type)
	{
	case ST_TITLE:
		glEnable(GL_BLEND);
		ÂrawIGDCLogo();
		glDisable(GL_BLEND);

		if(counter > 100)
		{
			glPushMatrix();
			glTranslatef(-0.55, -0.4, -1.f);
			scale(0.015);
			DrawText("Press \"Space\" to skip.",2);
			glPopMatrix();
		}
		break;
	case ST_MENU:

		glEnable(GL_BLEND);
		DrawMoveingLogo();
		glDisable(GL_BLEND);
		
		glPushMatrix();
		glTranslatef(-0.4f, 0.2f + sin(counter/100.f)*0.02, -1.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(-0.1f, 0.3f + sin(counter/200.f)*0.02, -1.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.4f, 0.35f + cos(counter/100.f)*0.02, -1.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.2, 0.15 + cos(counter/200.f)*0.02, -1.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.1f, 0.15, -0.5);
		DrawCloud();
		glPopMatrix();

		if(counter > 500)
		{
			glPushMatrix();
			glTranslatef(-0.375, -0.2, -1.f);
			scale(0.025);
			DrawText("Press \"Space\" to start!", 2);
			glPopMatrix();
		}

		glPushMatrix();
		glTranslatef(-0.25, -0.4, -1.f);
		scale(0.01);
		DrawText("Made by DRON http://dron.deeprosoft.com", 1);
		glTranslatef(0.f, 6.f, 0.f);
		scale(2.f);
		DrawText(("Best score:" + IntToStr(bestscore)).c_str(), 3);
		glPopMatrix();

		break;
	case ST_INTRO:

		glPushMatrix();
		glTranslatef(0, 0, -1.f);
		
		if(counter > 4050 && counter < 4150)
			glRotatef((counter - 4050)/2.f, 0.f, 1.f, 0.f);
		else
			if(counter >= 4150)
				glRotatef(50.f, 0.f, 1.f, 0.f);

		glLineWidth(3.f);

		//Sky
		
		if(counter > 100)
		{
			glPushMatrix();
			glTranslatef(-0.8f, 0.55f, -0.9f);
			glColor3ub(234, 255, 0);
			glRotatef(sin(counter/200.f)*30.f, 0.f, 0.f, 1.f);
			scale(0.2 + cos(counter/50.f)*0.02f);
			DawSun();
			glPopMatrix();
		}

		if(counter > 300)
		{
			glColor3ub(255, 255, 255);
			glPushMatrix();
			glTranslatef(-0.65f, 0.65f + sin(counter/100.f)*0.02, -0.8f);
			scale(3.f);
			DrawCloud();
			glPopMatrix();
			glPushMatrix();
			glTranslatef(0.2f, 0.5f + cos(counter/100.f)*0.02, -0.8f);
			scale(2.f);
			DrawCloud();
			glPopMatrix();
		}
		
		//birds
		if(counter > 1800)
		{
			glColor3ub(0, 0, 0);
			glPushMatrix();
			glTranslatef(-0.1 + sin(counter/150.f)*0.05, 0.4f + sin(counter/50.f)*0.01, -0.9f);
			scale(0.02);
			DrawBird();
			glPopMatrix();
		}

		//Sea
		if(counter > 200)
		{
			glPushMatrix();
			glTranslatef(10.f, -0.4, -0.8f);
			glScalef(30.f, 0.7f, 1.f);
			glBegin(GL_QUADS);
			glColor3ub(44, 34, 242);
			glVertex2f(-0.5, 0.5);
			glVertex2f(0.5, 0.5);
			glColor3ub(13, 89, 255);
			glVertex2f(0.5, -0.5);
			glVertex2f(-0.5, -0.5);
			glVertex2f(0.5, -0.5);
			glVertex2f(-0.5, -0.5);
			glVertex2f(0.5, -15.5);
			glVertex2f(-0.5, -15.5);
			glEnd();
			glPopMatrix();

			glColor3ub(255,255,255);

			for(i = 0; i < 24; i++)
			{
				glPushMatrix();
				glTranslatef((i >= 12 ? water_coord[i-12]*2.8f : water_coord[i])+sin(counter/3000.f*i)*0.1, -0.15 - (i >= 12 ? 0.08*(i-12) : 0.08*i)+cos(counter/4000.f*(10-i))*0.075, -0.7f);
				glBegin(GL_LINES);
				glVertex2f(-0.025, 0.f);
				glVertex2f(0.025, 0.f);
				glEnd();
				glPopMatrix();
			}
		}
		if(counter > 500)
		{
			//Hill
			glPushMatrix();
			glTranslatef(-0.4, -0.8, -0.6);
			glColor3ub(0, 255, 0);
			glScalef(1.4f, 0.65, 1.f);
			DrawHill();
			glColor3ub(0, 0, 0);
			glTranslatef(0.f, 0.f, 0.005);
			DrawHill(true);
			glPopMatrix();

			if(counter > 700)
			{
				glPushMatrix();
				glTranslatef(0.6, -0.125, -0.5);
				scale(0.2);
				DrawTree();
				glPopMatrix();
			}
			
			if(counter > 1800)
			{
				for(i = 0; i < 5; i++)
				{
					glPushMatrix();
					glTranslatef(romashki[i*2], romashki[i*2+1], -0.5);
					scale(0.05);
					DrawRomashka(sin(counter/200.f)*45.f);
					glPopMatrix();
				}
			}
		}

		glLineWidth(2.f);

		//Draw pixel and Texel

		if(counter > 900 && counter < 3925)
		{
			glPushMatrix();
			if(counter > 3625)
				glTranslatef(0.255 + sin(counter/200.f)*0.01, -0.1- (counter - 3625)/700.f, 0.f);
			else
				glTranslatef(0.255 + sin(counter/200.f)*0.01, -0.1, 0.f);
			scale(0.05);
			DrawPixel(counter > 3420);
			glPopMatrix();
		}
	
		if(counter > 1200 && counter < 3420)
		{
			glPushMatrix();
			glTranslatef(0.32 + cos(counter/200.f)*0.01, -0.1, 0.f);
			scale(0.05);
			DrawTexel();
			glPopMatrix();
		}

		//Serdce
		if(cerdce_counter != 0)
		{
			glPushMatrix();
			glColor4ub(255, 5, 66, 255 - cerdce_counter/4);
			glTranslatef(0.27,-0.065,0.f);
			scale(0.0002*cerdce_counter);
			glBegin(GL_POLYGON);
			glVertex2f(0.5, 0.0);
			glVertex2f(0.0, 0.5);
			glVertex2f(0.0, 1.0);
			glVertex2f(0.25, 1.0);
			glVertex2f(0.5, 0.8);
			glEnd();
			glBegin(GL_POLYGON);
			glVertex2f(0.5, 0.8);
			glVertex2f(0.75, 1.0);
			glVertex2f(1.0, 1.0);
			glVertex2f(1.0, 0.5);
			glVertex2f(0.5, 0.0);
			glEnd();
			glPopMatrix();
		}

		//Voxel
		if(counter > 2700 && counter < 3625)
		{
			glPushMatrix();
			if(counter > 3200)
				glTranslatef(-0.15 + (counter - 3200)/500.f, 0.3 + sin(counter/2.f)*0.01 - (counter - 3200)/500.f, 0.f);
			else
				glTranslatef(-0.15, 0.3 + sin(counter/2.f)*0.01, 0.f);
			scale(0.1);
			DrawVoxel();
			glPopMatrix();
		}

		glLineWidth(1.f);

		if(counter > 3925)
		{
			glPushMatrix();
			glTranslatef(-1.2f + (counter - 3925)/100.f, 0.1f, 0.f);
			scale(0.1);
			DrawPlayer(true);
			glPopMatrix();
		}

		glPopMatrix();

		//Story
		glDisable(GL_DEPTH_TEST);
		glColor4f(1.f, 1.f, 1.f, 1.f);

		if(int(counter / 300) < 13)
		{
			glPushMatrix();
			glTranslatef(-0.5, 0.13 , -1.f);
			scale(0.025);
			if(counter%300 < 90)
				glRotatef(90.f - counter%300, 1.f, 0.f, 0.f);
			else
				if(counter%300 > 210)
					glRotatef(-(counter%300 - 210.f), 1.f, 0.f, 0.f);
			DrawText(story_txt[int(counter / 300)], 4);
			glPopMatrix();
		}

		if(counter > 100)
		{
			glPushMatrix();
			glTranslatef(-0.55, -0.4, -1.f);
			scale(0.015);
			DrawText("Press \"Space\" to skip.",2);
			glPopMatrix();
		}

		glEnable(GL_DEPTH_TEST);

		break;
	case ST_GAME:
		
		DrawGameplay();

		glDisable(GL_DEPTH_TEST);
		glColor4f(1.f, 1.f, 1.f, 1.f);

		if(int(counter / 300) < 6)
		{
			glPushMatrix();
			glTranslatef(-0.5, -0.2 , -1.f);
			scale(0.025);
			if(counter%300 < 90)
				glRotatef(90.f - counter%300, 1.f, 0.f, 0.f);
			else
				if(counter%300 > 210)
					glRotatef(-(counter%300 - 210.f), 1.f, 0.f, 0.f);
			DrawText(tutorial_txt[int(counter / 300)], 4);
			glPopMatrix();
		}

		if(lives == -1)
		{
			glPushMatrix();
			glTranslatef(-0.3, 0.f , -1.f);
			scale(0.05);
			DrawText("GAME OVER", 4);
			glPopMatrix();

			glPushMatrix();
			glTranslatef(-0.3, -0.1 , -1.f);
			scale(0.025);
			DrawText(("Final Score:" + IntToStr(score)).c_str(), 4);
			glPopMatrix();

			if(counter - tmp_counter > 100)
				{
					glPushMatrix();
					glTranslatef(-0.36, -0.2, -1.f);
					scale(0.025);
					DrawText("Press \"Space\" to skip.",2);
					glPopMatrix();
				}
		}

		glPushMatrix();
		glTranslatef(-0.55, 0.4 , -1.f);
		scale(0.01);
		DrawText(("Score:" + IntToStr(score)).c_str(), 2);
		glTranslatef(0.f, -1.5 , 0.f);
		if(lives < 0)
			DrawText("Lives:0", 2);
		else
			DrawText(("Lives:" + IntToStr(lives)).c_str(), 2);
		glPopMatrix();


		if(counter < GAMEPLAY_PART_3)
		{
			glColor3f(1.f, 1.f, 1.f);
			glPushMatrix();
			glTranslatef(-0.055, 0.43, -1.1f);
			glScalef(0.35, 0.015, 1.f);
			DrawQuad();
			glPopMatrix();

			glEnable(GL_DEPTH_TEST);

			glPushMatrix();
			glTranslatef(-0.2 + (0.3/GAMEPLAY_PART_3)*counter, 0.4, -1.f);
			scale(0.015);
			DrawPixel();
			glPopMatrix();
			glPushMatrix();
			glTranslatef(0.1, 0.4, -1.f);
			scale(0.015);
			DrawVoxel();
			glPopMatrix();
		}
		else
			glEnable(GL_DEPTH_TEST);

		break;
	case ST_ENDING:
		glPushMatrix();
		glTranslatef(0, 0, -1.f);

		glPushMatrix();
		glTranslatef(-0.8f, 0.55f, -0.9f);
		glColor3ub(234, 255, 0);
		glRotatef(sin(counter/200.f)*30.f, 0.f, 0.f, 1.f);
		scale(0.2 + cos(counter/50.f)*0.02f);
		DawSun();
		glPopMatrix();

		glColor3ub(255, 255, 255);
		glPushMatrix();
		glTranslatef(-0.65f, 0.65f + sin(counter/100.f)*0.02, -0.8f);
		scale(3.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.2f, 0.5f + cos(counter/100.f)*0.02, -0.8f);
		scale(2.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(-0.4f, 0.2f + sin(counter/100.f)*0.02, -1.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(-0.1f, 0.3f + sin(counter/200.f)*0.02, -1.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.8f, 0.5f + cos(counter/100.f)*0.02, -1.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.5, -0.15 + cos(counter/200.f)*0.02, -1.f);
		DrawCloud();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.1f, 0.15, -0.5);
		DrawCloud();
		glPopMatrix();

		glColor3ub(0, 0, 0);
		glPushMatrix();
		glTranslatef(-0.1 + sin(counter/150.f)*0.05, 0.4f + sin(counter/50.f)*0.01, -0.9f);
		scale(0.02);
		DrawBird();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.4 + sin(counter/150.f)*0.05, 0.3f + sin(counter/50.f)*0.01, -0.9f);
		scale(0.02);
		DrawBird();
		glPopMatrix();
		glPushMatrix();
		glTranslatef(0.3 + sin(counter/150.f)*0.05, 0.2f + sin(counter/50.f)*0.01, -0.9f);
		scale(0.02);
		DrawBird();
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.3f- sin(counter/100.f)*0.015, -0.1f + sin(counter/100.f)*0.015, 0.f);
		scale(0.2);
		DrawPlayer();
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.1- sin(counter/100.f)*0.015, -0.05 + sin(counter/10.f)*0.005+ sin(counter/100.f)*0.015, -0.05);
		glScalef(-0.12, 0.12, 0.12);
		DrawTexel();
		glPopMatrix();

		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.1, -0.35 , -1.f);
		scale(0.025);
		DrawText("Love is beautiful!", 4);
		glTranslatef(0.f, 3.f , 0.f);
		DrawText(("Final Score:" + IntToStr(score)).c_str(), 4);
		glPopMatrix();

		break;
	}

	if(counter < 127 || blink > 0)
	{
		glEnable(GL_BLEND);
		if(blink > 0)
			glColor4ub(255, 255, 255, blink*3);
		else
			glColor4ub(255, 255, 255, 255 - counter*2);
		glTranslatef(0.f, 0.f, -0.1);
		scale(2);
		DrawQuad();
		glDisable(GL_BLEND);
	}

	return true;
}
