// dragonfly_alpha.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
//windows.h is a good thing, don't hate
#include <Windows.h>
//game engine includes
#include "Position.h"
#include "Box.h"
#include "Clock.h"
#include "GameObject.h"
#include "GameObjectList.h"
#include "GameObjectListIterator.h"
#include "Manager.h"
#include "Event.h"
#include "LogManager.h"
#include "WorldManager.h"
#include "GameManager.h"
#include "ResourceManager.h"

#include "DebugObject.h"
#include "Hero.h"
#include "SpikeBall.h"
#include "Brick.h"
#include "Coin.h"
#include "Spring.h"
#include "MovingPlatform.h"
#include "HUD.h"
#include "ExitBlocker.h"
#include "Exit.h"
#include "Checkpoint.h"
#include <curses.h>
#include "LevelLoader.h"

using namespace std;

//prototypes
void doTitle();
void doWin();

int main(int argc, char* argv[])
{
	//to load the next level, run dragonfly with commandline arguments for the next level
	cout << "press esc to end the game" << endl;
	Clock c=Clock(GetTickCount());
	GameManager::getInstance().startUp(true,true);
	LogManager::getInstance().writeLog("DRAGONFLY LOGFILE STARTED \n");
	//set the actual playfield size
	WorldManager::getInstance().setBoundary(Box(Position(0,0),128,96));
	short bgCol=11;
	WINDOW* win;
	win=initscr();
	curs_set(0);
	//load level here
	LevelLoader ll=LevelLoader();
	WorldManager::getInstance().setView(Box(Position(0,0),64,48));
	resize_term(48,64);
	start_color();
	//hardcode color pairs
	for(int i=0;i<256;i++){
		init_pair(i,i%16,(int)i/16);
	}
	//if no commandline args, load level 1, else load from commandline
	//if no commandline, display the main menu/intro/something
	if(argc>1){
		string a=argv[1];
		if(a!="win"){
			bgCol=ll.loadLevel(argv[1]);
		}
		else{
			doWin();
			getch();
			exit(0);
		}
	}
	else{
		//do title screen 
		doTitle();
		getch();
		bgCol=ll.loadLevel("level1.txt");
	}
	
	wbkgd(win,COLOR_PAIR(16*ll.bgCol));
	GameManager::getInstance().run(25);
	LogManager::getInstance().writeLog("Dragonfly ran for %d ms",c.delta());
	endwin();
	return 0;
}

void doTitle(){
	ifstream title;
	title.open("title.txt");
	string line;
	int lineNum=0;
	if (title.is_open())
	{
		while (title.good())
		{
			getline(title,line);
			attron(COLOR_PAIR(0x1F));
			mvaddstr(4+lineNum,4,line.c_str());
			attroff(COLOR_PAIR(0x1F));
			lineNum++;
		}
		title.close();
	}
	mvaddstr(32,4,"Press A and D to move left and right. Press W to jump.");
	mvaddstr(33,4,"Collect all the coins to open the way to the next level.");
	mvaddstr(34,4,"Press escape at any time to end the game.");
	mvaddstr(35,4,"Press any key to begin");
	refresh();
}

void doWin(){
	ifstream title;
	title.open("win.txt");
	string line;
	int lineNum=0;
	if (title.is_open())
	{
		while (title.good())
		{
			getline(title,line);
			attron(COLOR_PAIR(0x1F));
			mvaddstr(12+lineNum,12,line.c_str());
			attroff(COLOR_PAIR(0x1F));
			lineNum++;
		}
		title.close();
	}
	mvaddstr(32,12,"You have won! Press any key to exit.");
	refresh();
}