#include "assert.h"

#include "light.h"
#include "ray.h"

RTLights::RTLights() {

  _lights = new _LightInfo[MAX_LIGHTS]; 
  _size = MAX_LIGHTS;
  _num_lights = 0;

  _ambient.Set (0., 0., 0.);

  //make headlight and disable it.
  AddPointLight (Vec4 (0, 0,0), Vec4(1., 1., 1.), .8);
  _lights[0]._active = 0; 
}

RTLights::~RTLights() {
  delete [] _lights;
}

RTLights& RTLights::CopyFrom(const RTLights &L) {

  if ((!_lights) || (_size != L._size)) {
    delete [] _lights;
    _lights = new _LightInfo[_size = L._size];
    _num_lights = L._num_lights;
  }

  for (int i=0; i<_num_lights; i++) {
    _lights[i]._direction = L._lights[i]._direction;
    _lights[i]._color = L._lights[i]._color;
  }

  return *this;
}

void RTLights::SetAmbientLight(const Vec4 &color) {

  _ambient = color;
}

int RTLights::AddDirectionalLight(const Vec4 &d, const Vec4 &color, real intensity) {

  if (_num_lights >= _size) {
    cerr << "Error in RTLights::AddDirectionalLight(): "
         << "cannot add another light" << endl;
    return -1;
  }

  _lights[_num_lights]._type = Directional;
  _lights[_num_lights]._direction = d;
  _lights[_num_lights]._direction.MakeUnit();
  _lights[_num_lights]._color = color;
  _lights[_num_lights]._active = 1;
  _lights[_num_lights]._intensity = intensity;

  return _num_lights++;
}

int RTLights::AddPointLight(const Vec4 &d, const Vec4 &color, real intensity) {

  if (_num_lights >= _size) {
    cerr << "Error in RTLights::AddDirectionalLight(): "
         << "cannot add another light" << endl;
    return -1;
  }

  _lights[_num_lights]._type = Point;
  _lights[_num_lights]._direction = d;
  _lights[_num_lights]._color = color;
  _lights[_num_lights]._active = 1;
  _lights[_num_lights]._intensity = intensity;

  return _num_lights++;
}

