/*
 * zaphistoryosd.c: The actual menu implementations
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id:
 */

#include "zaphistoryosd.h"
#include "config.h"

class cMenuChannelItem : public cOsdItem {
public:
  enum eChannelSortMode { csmNumber, csmName, csmProvider };
private:
  static eChannelSortMode sortMode;
  cChannel *channel;
public:
  cMenuChannelItem(cChannel *Channel);
  static void SetSortMode(eChannelSortMode SortMode) { sortMode = SortMode; }
  static void IncSortMode(void) { sortMode = eChannelSortMode((sortMode == csmProvider) ? csmNumber : sortMode + 1); }
  virtual int Compare(const cListObject &ListObject) const;
  virtual void Set(void);
  cChannel *Channel(void) { return channel; }
  static eChannelSortMode SortMode(void) { return sortMode; }
  };

cMenuChannelItem::eChannelSortMode cMenuChannelItem::sortMode = csmNumber;

cMenuChannelItem::cMenuChannelItem(cChannel *Channel)
{
  channel = Channel;
  if (channel->GroupSep())
     SetSelectable(false);
  Set();
}

int cMenuChannelItem::Compare(const cListObject &ListObject) const
{
  cMenuChannelItem *p = (cMenuChannelItem *)&ListObject;
  int r = -1;
  if (sortMode == csmProvider)
     r = strcoll(channel->Provider(), p->channel->Provider());
  if (sortMode == csmName || r == 0)
     r = strcoll(channel->Name(), p->channel->Name());
  if (sortMode == csmNumber || r == 0)
     r = channel->Number() - p->channel->Number();
  return r;
}

void cMenuChannelItem::Set(void)
{
  char *buffer = NULL;
  asprintf(&buffer, "%d %s", channel->Number(), channel->Name());
  SetText(buffer, false);
}

cMenuZappedChannels::cMenuZappedChannels()
:cOsdMenu(tr("Zapping History"), 20)
{
  	Setup();
}

cMenuZappedChannels::~cMenuZappedChannels()
{

}

void cMenuZappedChannels::Setup(void)
{
//  cChannel *currentChannel = Channels.GetByNumber(zappedChannels[0]);
//  if (!currentChannel)
//     currentChannel = Channels.GetByNumber(zappedChannels[0]);
//  cMenuChannelItem *currentItem = NULL;
  Clear();
  for (int i=0;i<config.maxEntries;i++)
  {
  	    if (zappedChannels[i]!=0)
   	    {
  			cChannel *channel = Channels.GetByNumber(zappedChannels[i]);
        	cMenuChannelItem *item = new cMenuChannelItem(channel);
        	Add(item);
	    }            	
  }
  Display();
}

cChannel *cMenuZappedChannels::GetChannel(int Index)
{
  cMenuChannelItem *p = (cMenuChannelItem *)Get(Index);
  return p ? (cChannel *)p->Channel() : NULL;
}

eOSState cMenuZappedChannels::Switch(void)
{
  cChannel *ch = GetChannel(Current());
  if (ch)
     return cDevice::PrimaryDevice()->SwitchChannel(ch, true) ? osEnd : osContinue;
  return osEnd;
}

eOSState cMenuZappedChannels::ProcessKey(eKeys Key)
{
  eOSState state = cOsdMenu::ProcessKey(Key);

  switch (state) {
    default:
         if (state == osUnknown) {
            switch (Key) {
              case kOk:     return Switch();
              default: break;
              }
            }
    }
  return state;
}


