/*==== Call.h ===============================================
  Class Call models phone calls.
=============================================================*/

#include "RandomInt.h"
#include <cassert>
using namespace std;

class Call
{
/***** Function Members *****/
public:
/* Default Constructor
 *  Postcondition: All data members initialized to 0
 **************************************************************/
Call()
{ timeOfArrival = completionTime = 0; }

/* Explicit-Value Constructor
 *  Receive:  The countdown timer t and simulation
 *            values sim
 *  Postcondition: timeOfArrival initialized to elapsed
 *                 time specified by t and serviceTime
 *                 is set randomly using sim's values
 **************************************************************/
Call(const Timer & t, const SimulationValues & sim);
  
/* Begin service of the call
 *  Receive:  The countdown timer t and simulation
 *            values sim
 *  Postcondition: completionTime is set and
 *                 totalWaitingTime in sim is updated
 **************************************************************/
void BeginService(const Timer & t, SimulationValues & sim)
{
  sim.totalWaitingTime += timeOfArrival - t.TimeRemaining();
  completionTime = t.TimeRemaining() - serviceTime;
}

/* Check if service completed
 *  Receive: The countdown timer t
 *  Return:  True if countdown timer has reached
 *           completionTime; false otherwise
 **************************************************************/
bool ServiceCompleted(const Timer & t)
{ return t.TimeRemaining() <= completionTime; }

/***** Data Members *****/
private:
  int timeOfArrival;
  int serviceTime;
  int completionTime;

};  // end of class declaration

/***** Definition of Constructor *****/
Call::Call(const Timer & t, const SimulationValues & sim)
{ 
  // record call's time of arrival
  timeOfArrival = t.TimeRemaining();

  // generate a service time for it
  RandomInt r;
  r.Generate(0, 100);
  serviceTime = 0;
  while (r > sim.servicePercent[serviceTime])
    serviceTime++;
}
