#pragma once
#include "includes.h"
#include "scene_parse.h"
#include "scene_elements.h"

class Scene {
	public:
		Parser* parser;

		Camera* camera;
		Image* image;
		map<string,Material*>* materials;
		list<Object*>* objects;
		map<string,Light*>* lights;

		bool valid;
		string error;

		double background_color[3];
	public:
		Scene(const char* path) {
			//printf("Creating parser.\n");
			parser = new Parser(path);

			camera = new Camera();
			image = new Image();
			materials = new map<string,Material*>();
			objects = new list<Object*>();
			lights = new map<string,Light*>();

			Light::init();

			valid = true;
			error = scene_parse(parser, camera,image,materials,objects,lights,background_color);
			if (error!="") valid = false;

			for (map<string,Material*>::const_iterator iter=materials->begin();iter!=materials->end();++iter) {
				iter->second->init();
			}
		}
		~Scene() {
			delete parser;

			delete camera;
			delete image;

			while (materials->size()>0) {
				delete materials->begin()->second;
				materials->erase(materials->begin());
			}
			delete materials;

			while (objects->size()>0) {
				delete objects->front();
				objects->pop_front();
			}
			delete objects;

			while (lights->size()>0) {
				delete lights->begin()->second;
				lights->erase(lights->begin());
			}
			delete lights;
		}
};