/*
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * Author:
 *         Roman Krenicky
 *
 * $Id: blockentry.cpp,v 1.4 2005/11/23 17:51:35 kutzner Exp $
 *
 */

/*
  blockentry.cpp
  created: 6.3.2005
  last change: 12.3.2005
*/
/*! @file
    an entry describing (identifying) an EPG block; to be used in containers
*/

#include "globalheader.h"

#include <channels.h>
#include "blockentry.h"



/*! @fn blockentry::blockentry (void)

    new object will have an empty channel an a start time of 0
*/
//blockentry::blockentry (void);


/*! @fn blockentry::blockentry (const epgchannel &init_channel, time_t init_start_time)
    @param init_channel  the channel, this EPG block belongs to
    @param init_start_time  the start time of this EPG block
*/
// blockentry::blockentry (const epgchannel &init_channel, time_t init_start\_time);


/*! @fn bool blockentry::operator== (const blockentry &other)  const
    @param other  object to compare this one with

    two @c blockentry-s are equal iff they equal in start_time and the
    channelIDs of their channels
*/
bool blockentry::operator== (const blockentry &other)  const
{
   if (this->start_time == other.start_time)
   {
      if (this->channel == other.channel)
         return true;
      if ( this->channel.empty() || other.channel.empty() )
         return false;
      cChannel our_vdrchannel, other_vdrchannel;
      if ( !epgchannel2vdrchannel(this->channel, &our_vdrchannel) ||
           !epgchannel2vdrchannel(other.channel, &other_vdrchannel) )
         return false;
      return (our_vdrchannel.GetChannelID()==other_vdrchannel.GetChannelID());
   } // if (this->start_time == other.start_time)
   else
      return false;
} // operator== (...)


/*! @fn bool blockentry::operator< (const blockentry &other)  const
    @param other  object to compare this one with
*/
bool blockentry::operator< (const blockentry &other)  const
{
   // we compare the times first, as this may be simpler and faster in general

   if (this->start_time < other.start_time)
      return true;
   else if (this->start_time > other.start_time)
      return false;

   // else if (this->start_time == other.start_time):
   if (this->channel == other.channel)
      return false;
      // we define, that an empty channel is always smaller:
   if (this->channel.empty())
      return true;
   if (other.channel.empty())
      return false;


   // return ( *(this->channel) < *(other.channel) ):
   cChannel our_vdrchannel, other_vdrchannel;
   bool our_valid, other_valid;
   our_valid = epgchannel2vdrchannel (this->channel, &our_vdrchannel);
   other_valid = epgchannel2vdrchannel (other.channel, &other_vdrchannel);
      // try to preserve consistency on errors, too:
   if ( !our_valid && !other_valid )  // consider invalid channels being equal
      return false;
   if (!our_valid)  // we define, that an invalid channel is always smaller
      return true;
   if (!other_valid)  // we define, that an invalid channel is always smaller
      return false;

      // we compare by the values contained in a channel's channelID
      // (unfortunately we have to do it the following way):
         // (the order is arbitrary)
   if (our_vdrchannel.Source() < other_vdrchannel.Source())
      return true;
   else if (our_vdrchannel.Source() > other_vdrchannel.Source())
      return false;
   if (our_vdrchannel.Source() < other_vdrchannel.Source())
      return true;
   else if (our_vdrchannel.Source() > other_vdrchannel.Source())
      return false;
   if (our_vdrchannel.Nid() < other_vdrchannel.Nid())
      return true;
   else if (our_vdrchannel.Nid() > other_vdrchannel.Nid())
      return false;
   if (our_vdrchannel.Tid() < other_vdrchannel.Tid())
      return true;
   else if (our_vdrchannel.Tid() > other_vdrchannel.Tid())
      return false;
   if (our_vdrchannel.Sid() < other_vdrchannel.Sid())
      return true;
   else if (our_vdrchannel.Sid() > other_vdrchannel.Sid())
      return false;
   if (our_vdrchannel.Rid() < other_vdrchannel.Rid())
      return true;
   else if (our_vdrchannel.Rid() > other_vdrchannel.Rid())
      return false;
   // so they're equal !
   return false;
} // operator< (...)


/*! @fn const cChannel* blockentry::get_channel (void)  const
    @return the channel, this EPG block belongs to
    @warning NULL might be returned
*/
//const cChannel* blockentry::get_channel (void)  const;


/*! @fn time_t blockentry::get_start_time (void)  const
    @return the start time of this EPG block
*/
//time_t blockentry::get_start_time (void)  const;
