#include "MogCommon.h"

SDL_Surface *screen;
Sprite2D sprites[50];
Sprite2D **sprind;
char *gfxFiles[] = {
	"gfx/Mog - Front.gif",
	"gfx/Mog - Back.gif",
    "gfx/Mog - Left.gif",
	"gfx/Mog - Right.gif",
	"gfx/Mog - Walk Front.gif",
	"gfx/Mog - Walk Back.gif",
    "gfx/Mog - Walk Left.gif",
	"gfx/Mog - Walk Right.gif",
    "gfx/Deceased - Down.gif",
	"gfx/Deceased - Up.gif",
	"gfx/Deceased - Left.gif",
	"gfx/Deceased - Right.gif",
    "gfx/Deceased - Walk Front.gif",
	"gfx/Deceased - Walk Back.gif",
	"gfx/Deceased - Walk Left.gif",
	"gfx/Deceased - Walk Right.gif",
	"gfx/Deceased - Attack Front.gif",
	"gfx/Deceased - Attack Rear.gif",
	"gfx/Deceased - Dead.gif"
};



int main(int argc, char **argv)
{
	bool running = true;
    SDL_Event nextEvent;
	int i = 0;

    if ( SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		cout << "Couldn't init SDL!!\n";
		cout << SDL_GetError();
		exit(-1);
	}
	screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (!screen) {
		cout << "Couldn't get screen!\n";
		cout << SDL_GetError();
		exit(-1);
	}
    SDL_WM_SetCaption("KUPO! : Mog's Adventure (debug proof pre-0.1a)", NULL);

	/* everything from here down is just a proof for now */
    
	SDL_Rect s = {0, 0, 640, 480};
    
	// do the stills
	for (i = 0; i < 4 ; i++ ) {
        sprites[i].init(gfxFiles[i], 45, 0, 32, 44);
	}
    // do the walkers
	for (i = 4; i < 8 ; i++ ) {
        sprites[i].init(gfxFiles[i], 45, 4, 32, 44);
	}

	// do the stills
	for (i = 8; i < 12 ; i++ ) {
        sprites[i].init(gfxFiles[i], 45, 0, 68, 60);
	}
    // do the walkers
	for (i = 12; i < 16 ; i++ ) {
        sprites[i].init(gfxFiles[i], 45, 4, 68, 60);
	}

    Sprite2D theBackground("gfx/samplebg.jpg", 0, 1, 640, 480);
	
    Actor2D theActor(&sprites[SPR_STILLDOWN], 0.0f);
    theActor.move(90, 90);
    theActor.setVelocity(4, 4);
    theActor.setStateSprite(&sprites[SPR_MOVEDOWN],
							SPR_MOVEDOWN);
	theActor.setStateSprite(&sprites[SPR_MOVELEFT],
							SPR_MOVELEFT);
	theActor.setStateSprite(&sprites[SPR_MOVERIGHT],
							SPR_MOVERIGHT);
	theActor.setStateSprite(&sprites[SPR_MOVEUP],
							SPR_MOVEUP);
	theActor.setStateSprite(&sprites[SPR_STILLDOWN],
							SPR_STILLDOWN);
	theActor.setStateSprite(&sprites[SPR_STILLLEFT],
							SPR_STILLLEFT);
	theActor.setStateSprite(&sprites[SPR_STILLRIGHT],
							SPR_STILLRIGHT);
	theActor.command(ACTOR_SET_TYPE, ACTOR_TYPE_PLAYER, NULL);

	
	AIActor theBaddie(&sprites[8+SPR_STILLDOWN], 0.0f, ACTOR_TYPE_PLAYER, 500);
	theBaddie.move(50,50);
	theBaddie.setStateSprite(&sprites[8+SPR_MOVEDOWN],
							SPR_MOVEDOWN);
	theBaddie.setStateSprite(&sprites[8+SPR_MOVELEFT],
							SPR_MOVELEFT);
	theBaddie.setStateSprite(&sprites[8+SPR_MOVERIGHT],
							SPR_MOVERIGHT);
	theBaddie.setStateSprite(&sprites[8+SPR_MOVEUP],
							SPR_MOVEUP);
	theBaddie.setStateSprite(&sprites[8+SPR_STILLDOWN],
							SPR_STILLDOWN);
	theBaddie.setStateSprite(&sprites[8+SPR_STILLLEFT],
							SPR_STILLLEFT);
	theBaddie.setStateSprite(&sprites[8+SPR_STILLRIGHT],
							SPR_STILLRIGHT);
	theBaddie.setVelocity(3, 3);
	theBaddie.command(ACTOR_SET_TYPE, ACTOR_TYPE_BADDIE, NULL);
	theBaddie.command(ACTOR_IDLE, 0, NULL);    

	std::vector<Actor2D *> actorList;
	actorList.clear();
	actorList.push_back(&theActor);
	actorList.push_back(&theBaddie);

	while ( running == true ) {
        theBackground.drawFrame(screen, 0, 0, 0, 0);
        theActor.render(screen);
		theActor.update();

		theBaddie.render(screen);
		theBaddie.updateAI(actorList);
		theBaddie.update();

        SDL_Flip(screen);
		SDL_FillRect(screen, &s, 0);
		SDL_PumpEvents();

        if ( SDL_PollEvent(&nextEvent) > 0 ) {
			switch (nextEvent.type) {
			case SDL_KEYDOWN:
			case SDL_KEYUP:
				switch (nextEvent.key.state) {
				case SDL_PRESSED:
					if ( nextEvent.key.keysym.sym == SDLK_UP ) {
						theActor.command(ACTOR_MOVEUP, 0, NULL);
					}
					else if ( nextEvent.key.keysym.sym == SDLK_DOWN) {
						theActor.command(ACTOR_MOVEDOWN, 0, NULL);
					}
					else if ( nextEvent.key.keysym.sym == SDLK_RIGHT ) {
						theActor.command(ACTOR_MOVERIGHT, 0, NULL);
					}
					else if ( nextEvent.key.keysym.sym == SDLK_LEFT) {
						theActor.command(ACTOR_MOVELEFT, 0, NULL);
					}
					else if ( nextEvent.key.keysym.sym == SDLK_ESCAPE) {
						running = false;
					}
					break;
				case SDL_RELEASED:
                    if ( nextEvent.key.keysym.sym == SDLK_DOWN
						 && theActor.hasState(ACTOR_MOVEDOWN)) {
						theActor.command(ACTOR_IDLE, ACTOR_MOVEDOWN, NULL);
					}
					if ( nextEvent.key.keysym.sym == SDLK_UP
						 && theActor.hasState(ACTOR_MOVEUP) ) {
						theActor.command(ACTOR_IDLE, ACTOR_MOVEUP, NULL);
					}
					if ( nextEvent.key.keysym.sym == SDLK_LEFT
						 && theActor.hasState(ACTOR_MOVELEFT) ) {
						theActor.command(ACTOR_IDLE, ACTOR_MOVELEFT, NULL);
					}
					if ( nextEvent.key.keysym.sym == SDLK_RIGHT 
						 && theActor.hasState(ACTOR_MOVERIGHT) ) {
						theActor.command(ACTOR_IDLE, ACTOR_MOVERIGHT, NULL);
					}
					break;
				}
				break;
			case SDL_QUIT:
                SDL_Quit();
				return 0;
			}
		}
	}
}
