#include "util/Session.h"
#include "state/StateHandler.h"
#include "graphics/Renderer.h"
#include "graphics/shippingHandler.h"
#include "ruleset/Rules.h"

#include <stdio.h>

#define NULL_INDEX 0

Session session;

StateHandler * gs;

//prototypes
bool programStartup ();
void programShutdown ();

bool programStartup ()
{
  //start new objects, set session to them
  session.setStateHandler(new StateHandler());
  session.setShippingHandler(new shippingHandler ());
  session.setRenderer(new Renderer());
  session.setRules(new Rules());

  session.getStateHandler()->setSession(&session);
  session.getRenderer()->setSession(&session);
  session.getShippingHandler()->setSession(&session);
  session.getRules()->setSession(&session);

  if (!session.getStateHandler()->initialize ())
    return false;
  return true;
}

void programShutdown ()
{
  delete session.getStateHandler();
  session.setStateHandler(NULL);

  delete session.getShippingHandler();
  session.setShippingHandler(NULL);

  delete session.getRenderer();
  session.setRenderer(NULL);

  delete session.getRules();
  session.setRules(NULL);
}

int main (int argc, char *argv[])
{
  if (programStartup () == false)
    {
      programShutdown ();
      return 1;
    }

  //session.getStateHandler()->debugTest(0);

  while (!session.getStateHandler()->getWantsToQuit())
    {
      session.getStateHandler()->go ();
    }
  programShutdown ();
  return 0;
}
