/* platform.C
 */


#include "platform.h"
#include "train.h"
#include "passenger.h"
#include "xy.h"
#include "station.h"

#ifdef GL
  #include <GL/gl.h>
#endif


// Add a passenger, p, to the platform

void Platform::addPassenger( Passenger *p )

{
  passengers.add( p );
}


// Count the number of passengers who will board this train

int Platform::countBoardingPassengers( Train *t )

{
  int passengerCount = 0;
  int remainingSpace = t->remainingSpace();

  // Loop through the passenger list.  If there's space and if the
  // passenger wants to enter the train, count them.

  for (int i=0; i<passengers.size(); i++)
    if (passengerCount < remainingSpace && passengers[i]->wantsToEnterTrain( t ))
      passengerCount++;

  return passengerCount;
}


// Signal to all passengers on the platform that a particular train,
// t, has arrived.  Those who want to enter the train are transferred
// to the train if it has space for more passengers.

void Platform::signalTrainArrival( Train *t )

{
  // Loop through the passenger list.  If there's space and if the
  // passenger wants to enter the train, transfer the passenger to the
  // train; otherwise, transfer the passenger to the list of remaining
  // passengers.

  if (outputEvents)
    cout << currentTime << ": " << *t << " loads";

  for (int i=0; i<passengers.size(); i++)

    if (t->spaceLeft() && passengers[i]->wantsToEnterTrain( t )) {

      if (outputEvents)
	cout << " " << *passengers[i] << "(to " << *passengers[i]->destStation() << ")";

      t->addPassenger( passengers[i] );
      passengers.remove(i);
      i--;			// i-- so that we don't skip a passenger
    }

  if (outputEvents)
    cout << endl;
}

/* Draw the platform
 */

void
Platform::draw()

{
#ifdef GL
  xyCoord front, rear, centre, forward, right, v1, v2, v3, v4, temp;
  Section *s = section;
  float dx, dy, fractionFull;

  if (s->reversed) {
    front.x = s->x0;
    front.y = s->y0;
    rear.x = s->x1;
    rear.y = s->y1;
  } else {
    front.x = s->x1;
    front.y = s->y1;
    rear.x = s->x0;
    rear.y = s->y0;
  }

  centre = (front + rear) * 0.5;
  forward = (front - rear);
  forward.normalize();

  right.x = forward.y;
  right.y = -forward.x;
  right.normalize();

  dx = 7.5;
  dy = 85;

  // Fill platform according to number of people

  fractionFull = passengers.size() / (float) PLATFORM_CAPACITY;
  if (fractionFull > 1)
    fractionFull = 1;

  v1 = centre + forward * dy + right * 35;
  v2 = centre + forward * dy + right * (35 - fractionFull * (35-15));
  v3 = centre - forward * dy + right * (35 - fractionFull * (35-15));
  v4 = centre - forward * dy + right * 35;

  glColor3f( 0.9, 0.1, 0.1 );
  glBegin( GL_POLYGON );  
  glVertex2f( v1.x, v1.y );
  glVertex2f( v2.x, v2.y );
  glVertex2f( v3.x, v3.y );
  glVertex2f( v4.x, v4.y );
  glEnd();

  // Outline platform 

  v1 = centre + forward * dy + right * 35;
  v2 = centre + forward * dy + right * 15;
  v3 = centre - forward * dy + right * 15;
  v4 = centre - forward * dy + right * 35;

  glColor3f( 0.9, 0.9, 0.9 );
  glBegin( GL_LINE_LOOP );  
  glVertex2f( v1.x, v1.y );
  glVertex2f( v2.x, v2.y );
  glVertex2f( v3.x, v3.y );
  glVertex2f( v4.x, v4.y );
  glEnd();

#endif  
}




void
Platform::printPassengers()

{
  for (int i=0; i<passengers.size(); i++)
    cout << *passengers[i]
	 << ": source " << *passengers[i]->sourceStation()
	 << ", dest " << *passengers[i]->destStation()
	 << ", next " << *passengers[i]->nextStation << "\n";
}
