/***************************************************************************
 *   Copyright (C) 2004-2005 by Axel Katzur                                *
 *   software@katzur.de                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <string>
#include <vector>
#include <time.h>

std::string Number2String(unsigned long nNumber);
std::string Number2String(unsigned int nNumber);
std::string Number2String(int nNumber);
std::string Number2String(time_t nNumber);


class  CDescriptorShortEvent
{
public:
	void	DvbDescriptorShortEvent(unsigned char *pchBuffer);

	// get functions, inline
	std::string		GetLanguage()		{return m_strLanguage;};
	std::string		GetEventName()		{return m_strEventName;};
	std::string		GetText()			{return m_strText;};

private:
	std::string				m_strLanguage;
	std::string				m_strEventName;
	std::string				m_strText;
};


class	CDescriptorExtendedEvent
{
public:
	void	DvbDescriptorExtendedEvent(unsigned char *pchBuffer);

	// get functions, inline
	unsigned int	GetDescriptorNumber()				{return m_nDescriptorNumber;};
	unsigned int   GetLastDescriptorNumber()			{return m_nLastDescriptorNumber;};
	std::string		GetLanguage()						{return m_strLanguage;};
	int				GetMaxItemNumber()					{return m_vecstrItem.size();};
	std::string		GetItemDescriptors(int nIndex)	    {if (nIndex < m_vecstrItemDescriptors.size()) 
															return m_vecstrItemDescriptors[nIndex];
                                                        return NULL ;}; // default return on error
	std::string		GetItem(int nIndex)				{if (nIndex < m_vecstrItem.size()) 
														    return m_vecstrItem[nIndex];
                                                        return NULL ;}; // default return on error
	std::string		GetText()							{return m_strText;};

    private:
	unsigned int      			m_nDescriptorNumber;
	unsigned int      			m_nLastDescriptorNumber;
	std::string					m_strLanguage;

	std::vector<std::string>	m_vecstrItemDescriptors;
	std::vector<std::string>	m_vecstrItem;
	std::string					m_strText;
};

class CDescriptorContent
{
public:
	void    DvbDescriptorContent(unsigned char *pchBuffer);
	char*   DvbStringContentNibbleType(unsigned int nIndex);

	// get functions, inline
	int				GetMaxNibble()									{return m_vecnContentNibbleLevel1.size();};
	unsigned int	GetContentNibbleLevel1(unsigned int nIndex)   {if (nIndex < m_vecnContentNibbleLevel1.size())
															 		    return m_vecnContentNibbleLevel1[nIndex];
                                                                    return 0;};
	unsigned int	GetContentNibbleLevel2(unsigned int nIndex)   {if (nIndex < m_vecnContentNibbleLevel2.size())
															 		    return m_vecnContentNibbleLevel2[nIndex];
                                                                    return 0;};
    unsigned int	GetUserNibble1(unsigned int nIndex)           {if (nIndex < m_vecnUserNibble1.size())
															 		    return m_vecnUserNibble1[nIndex];
                                                                    return 0;};
	unsigned int	GetUserNibble2(unsigned int nIndex)		    {if (nIndex < m_vecnUserNibble2.size())
															 		    return m_vecnUserNibble2[nIndex];
                                                                    return 0;};

private:
	std::vector<unsigned int>		m_vecnContentNibbleLevel1;
	std::vector<unsigned int>		m_vecnContentNibbleLevel2;
	std::vector<unsigned int>		m_vecnUserNibble1;
	std::vector<unsigned int>		m_vecnUserNibble2;
};

class CDescriptorContentTransmition
{
public:
    void DvbDescriptorContentTransmisition(unsigned char *pchBuffer);

	// get functions,
    time_t GetTime(int nIndex);

	// get functions, inline
	unsigned int    GetTransportStreamId()                          {return m_nTransportStreamId;};
	unsigned int    GetOrginalNetworkId()                           {return m_nOrginalNetworkId;};
	unsigned int    GetServiceId()                                  {return m_nServiceId;};
	int				GetMaxTimes()									  {return m_vecnStartDateMJD.size();};
	unsigned int	GetStartDateMJD(unsigned int nIndex)            {if (nIndex < m_vecnStartDateMJD.size())
															 		    return m_vecnStartDateMJD[nIndex];
                                                                      return 0;};
	unsigned int	GetStartTime(unsigned int nIndex)               {if (nIndex < m_vecnStartTime.size())
															 		     return m_vecnStartTime[nIndex];
                                                                      return 0;};

private:
    unsigned int                   m_nTransportStreamId;
    unsigned int                   m_nOrginalNetworkId;
    unsigned int                   m_nServiceId;

    std::vector<unsigned long>    m_vecnStartDateMJD;
    std::vector<unsigned long>    m_vecnStartTime;
};


class CPremiereCit
{
public:
    // Constructor
    CPremiereCit(unsigned char *pchBuffer, long nLength);
    
    // Descriptor function
    unsigned int CitDescriptor(unsigned char* pchBuffer);

    // get functions
    std::vector<std::string>    GetEpgForVdr();
    std::string                 GetExtendDescriptorText();
    // get functions, inline
    unsigned long GetContentId()                                                                      {return m_nContentId;};
    unsigned long	GetEventDuration();

    // max for descriptors
    unsigned int	GetMaxDescriptorShortEvent()														{return m_vecDescriptorShortEvent.size();};
	unsigned int	GetMaxDescriptorExtendedEvent()														{return m_vecDescriptorExtendedEvent.size();};
	unsigned int	GetMaxDescriptorContent()															{return m_vecDescriptorContent.size();};
	unsigned int	GetMaxDescriptorContentTransmition()												{return m_vecDescriptorContentTransmition.size();};

	// to high index give an empty class back
	CDescriptorShortEvent					GetDescriptorShortEvent(int nIndex)						{if (nIndex < m_vecDescriptorShortEvent.size())
                                                                                                            return m_vecDescriptorShortEvent[nIndex];
                                                                                                        CDescriptorShortEvent DescriptorShortEvent;
                                                                                                        return DescriptorShortEvent;};
	CDescriptorExtendedEvent				GetDescriptorExtendedEvent(int nIndex)					    {if (nIndex < m_vecDescriptorExtendedEvent.size())
																										    return m_vecDescriptorExtendedEvent[nIndex];
                                                                                                        CDescriptorExtendedEvent DescriptorExtendedEvent;
																										return DescriptorExtendedEvent;};
	CDescriptorContent						GetDescriptorContent(int nIndex)							{if (nIndex < m_vecDescriptorContent.size())
                                                                                                            return m_vecDescriptorContent[nIndex];
                                                                                                        CDescriptorContent DescriptorContent;
																										return DescriptorContent;};
	CDescriptorContentTransmition			GetDescriptorContentTransmition(int nIndex)				{if (nIndex < m_vecDescriptorContentTransmition.size())
																										    return m_vecDescriptorContentTransmition[nIndex];
                                                                                                        CDescriptorContentTransmition DescriptorContentTransmition;
																										return DescriptorContentTransmition;};

private:
    // CIT Data
 	unsigned int m_nSectionSyntaxIndicator;
 	unsigned int m_nPrivateIndicator;
 	unsigned int m_nReserved;
	unsigned int m_nPrivateSectionLength;
 	unsigned long m_nTableIdExtension;
 	unsigned int m_nReserved2;
 	unsigned int m_nVersionNumber;
	unsigned int m_nCurrentNextIndicator;  // 0x00 = "valid next", 0x01 = "valid now"
    unsigned int m_nSectionNumber;
	unsigned int m_nLastSectionNumber;

    // private data
    unsigned long m_nContentId;
    unsigned long m_nDuration;
    unsigned int m_nReserved3;

    // descriptors, possible entries:
	std::vector <CDescriptorShortEvent>					m_vecDescriptorShortEvent;
	// extended_event_descriptor 0x4E
	std::vector <CDescriptorExtendedEvent>				m_vecDescriptorExtendedEvent;
	// content_descriptor 0x54
	std::vector <CDescriptorContent>					m_vecDescriptorContent;
	// 0xF2
	std::vector <CDescriptorContentTransmition>			m_vecDescriptorContentTransmition;
};
