#include "VideoSupplier.h"

VideoServiceProvider *VideoSupplier::pServiceProvider_ = 0;

VideoSupplier::VideoSupplier (const char *name, CORBA::Boolean store)
  : VideoCompany (name) //, ACE_TEXT ("Video Stream Supplier"))
{
  if (store)
    {
      pServiceProvider_->store (this, name);
    }
}

/*
 * @func Adds a video category
 * @parm | category | the given category
 */
void
VideoSupplier::addCategory (const char *category)
{
  StringArray *array;
  CORBA::Boolean found = 0;

  for (ASA_Iter itb = ASA_Iter (videos_); itb.next (array) != 0; itb.advance ())
    {
      if (!ACE_OS::strcmp (category, (*array)[0]))
        {
          found = 1;
          break;
        }
    }

  if (!found)
    {
      videos_.size (videos_.size () + 1);
      videos_[videos_.size () - 1].size (1);
      videos_[videos_.size () - 1][0] = (char*) category;
   }
}

/*
 * @func Adds a video title
 * @parm | category | the given video category
 * @parm | name | the given name of the video
 */
void
VideoSupplier::addVideo (const char *category, const char *name)
{
  CORBA::Boolean found = 0;
  int count= 0;
  StringArray *array;

  for (ASA_Iter itb = ASA_Iter (videos_); itb.next (array) != 0; itb.advance ())
    {
      if (!ACE_OS::strcmp (category, (*array)[0]))
        {
          found = 1;
           break;
        }
      ++count;
    }

  if (found)
    {
      videos_[count].size (videos_[count].size () + 1);
      videos_[count][videos_[count].size () - 1] = (char*) name;
   }
}

/*
 * @func IDL defined funtion that returns a list of video titles for the given
 * category
 * @parm | category | the given category
 * @rdesc unbounded sequence of strings
 */
Server::OfferList *
VideoSupplier::getOffers (const char *category)
{
  Server::OfferList *offers = 0;
  ACE_NEW_RETURN (offers,
                  Server::OfferList (),
                  0);
  Server::OfferList_var holder (offers);

  StringArray *array;
  char **offer;

  for (ASA_Iter itb = ASA_Iter (videos_); itb.next (array) != 0; itb.advance ())
    {
      if (!ACE_OS::strcmp (category, (*array)[0]))
        {
          ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Found offers for %s\n"), (*array)[0]));
          int videos = 0;
          offers->length (static_cast<CORBA::ULong> (array->size () - 1));

          for (StringArray::ITERATOR itb2 = StringArray::ITERATOR (*array);
               itb2.next (offer) != 0; itb2.advance ())
            {
              if (videos > 0)
                {
                  (*offers)[videos-1] = CORBA::string_dup (*offer);
                }
              ++videos;
            }
          break;
        }
    }

  return holder._retn ();
}
