// front_impl.cpp - This source code is the implmentation of the
//             object interface FrontOffice.
//
#include "front.h"

class FrontOffice_impl : public virtual _sk_FrontOffice {
	char * m_name;
	CORBA::ULong m_numberOfSeats;
	CORBA::Char m_divide;
	Price m_highPrice;
	Price m_lowPrice;

	const unsigned short m_rows;
	const unsigned long m_seatsPerRow;
	unsigned char** m_avail;

public:
	FrontOffice_impl (const char* theName,
				   const CORBA::ULong theNumberOfRows,
                   const CORBA::ULong theNumberofSeatsPerRow,
                   const CORBA::Char theDivide,
                   const Price theHighPrice,
                   const Price theLowPrice);
	virtual ~FrontOffice_impl();

    char* name();
    CORBA::ULong numberOfSeats();
    Price getPrice(const Place& chosenPlace);
    CORBA::Boolean bookSingleSeat( const Place& chosenPlace, const char* creditCard );
};

FrontOffice_impl::FrontOffice_impl (const char* theName,
                                 const CORBA::ULong theNumberOfRows,
                                 const CORBA::ULong theNumberofSeatsPerRow,
                                 const CORBA::Char theDivide,
                                 const Price theHighPrice,
                                 const Price theLowPrice):
  m_rows(theNumberOfRows),
  m_seatsPerRow(theNumberofSeatsPerRow),
  m_numberOfSeats(m_rows*m_seatsPerRow),
  m_divide(theDivide),
  m_highPrice(theHighPrice),
  m_lowPrice(theLowPrice)
{
  m_name = new char [strlen(theName)+1];
  strcpy(m_name, theName);
  unsigned long i,j;
  m_avail = new unsigned char* [m_rows];
  for (i=0;i<m_rows;i++) {
    m_avail[i] = new unsigned char [m_seatsPerRow];
    for (j=0;j<m_seatsPerRow;j++)
      m_avail[i][j] = 1;
  }
}


FrontOffice_impl::~FrontOffice_impl() {
  delete[] m_name;
  unsigned long i;
  for (i=0;i<m_rows;i++)
    delete [] m_avail[i];
  delete[] m_avail;
}

// Implementation for interface FrontOffice
char* FrontOffice_impl::name()
{
  return CORBA::string_dup(m_name); 
}

CORBA::ULong FrontOffice_impl::numberOfSeats()
{
  return (m_rows * m_seatsPerRow);
}


Price
FrontOffice_impl::getPrice( const Place& chosenPlace )
{
  if (chosenPlace.row < m_divide)
    return m_lowPrice;
  else
    return m_highPrice;

}


CORBA::Boolean
FrontOffice_impl::bookSingleSeat( const Place& chosenPlace, const char* creditCard )
{
  unsigned long rowIndex = chosenPlace.row - 'A';
  if (m_avail[rowIndex][chosenPlace.seat]) {
    m_avail[rowIndex][chosenPlace.seat] = 0;
    return 1;
  }
  else
	return 0;
}