#include <string>
#include "menu_tracks.h"
#include <vdr/menu.h>
#include <vdr/interface.h>
#include "deluxe_config.h"
#include "replay_control.h"
#include "i18n.h"

using std::string;

int cMenuTracks::lastReplayed = -1;
string GetRecordingChannelID(cRecording* recording);

cMenuTrackItem::cMenuTrackItem(cTrack* Track)
{
    track = Track;
    Set();
}

void cMenuTrackItem::Set(void)
{
    char *buffer = NULL;
    asprintf(&buffer, "%s\t%s\t%s\t%04d", (track->status == 0?">":""), track->artist.c_str(), track->title.c_str(), track->year);
    SetText(buffer, false);
}

// --- cMenuTracks ----------------------------------------------------------
cMenuTracks::cMenuTracks(cRecording* Recording)
:cOsdMenu("", 2, 18, 21)
{
    int offset = DeluxeConfig.offset;
    recording = Recording;
    string channelID = GetRecordingChannelID(recording);
    int itemIndex = 0;

    cIndexFile index(recording->FileName(), false);
    for(cTrack* track = Tracks.First(); track; track = Tracks.Next(track))
    {
	if (track->channelID != channelID) continue;
	int indexIStart = index.GetNextIFrame((track->starttime - recording->start + offset) * FRAMESPERSEC, true);
	int indexIStop = index.GetNextIFrame((track->starttime + track->duration - recording->start + offset) * FRAMESPERSEC, false);
	if (indexIStart < 0 || indexIStop < 0)
	    continue;
	Add(new cMenuTrackItem(track), lastReplayed == itemIndex);
	itemIndex++;
    }
    UpdateTitle();
    lastReplayed = -1;
}

void cMenuTracks::UpdateTitle()
{
    char *buffer = NULL;
    asprintf(&buffer, "%d %s", Count(), tr("Songs"));
    SetTitle(buffer);
    Display();
    free(buffer);
    SetHelp(trVDR("Button$On/Off"), trVDR("Button$Play"), NULL, NULL); 
}

eOSState cMenuTracks::Toggle(void)
{
    cMenuTrackItem *ri = (cMenuTrackItem*)Get(Current());
    if (!ri) return osContinue;
    cTrack* track = ri->Track();
    if (!track) return osContinue;

    if (track->status > 0) 
	track->status = 0;
    else
	track->status = 1;
    Tracks.Save();
    RefreshCurrent();
    Display();
    return osContinue;
}

eOSState cMenuTracks::Play(void)
{
    cMenuTrackItem *ri = (cMenuTrackItem*)Get(Current());
    if (!ri) return osContinue;
    cTrack* track = ri->Track();
    if (!track) return osContinue;

    int offset = DeluxeConfig.offset;
    cIndexFile index(recording->FileName(), false);
    int indexIStart = index.GetNextIFrame((track->starttime - recording->start + offset) * FRAMESPERSEC, true);
    if (indexIStart < 0)
	return osContinue;

    cResumeFile ResumeFile(recording->FileName());
    ResumeFile.Save(indexIStart);

    lastReplayed = Current();
    cDeluxeReplayControl::SetRecording(recording->FileName(), recording->Title());
    cControl::Shutdown();
    cControl::Launch(new cDeluxeReplayControl);
    return osEnd;
}

eOSState cMenuTracks::Blacklist(void)
{
    if (Count() == 0) return osContinue;
    if (!Interface->Confirm(tr("Apply blacklist again?"))) return osContinue;

    
    for(int i=0; i<Count(); i++)
    {
	cMenuTrackItem *ri = (cMenuTrackItem*)Get(i);
	cTrack* track = ri->Track();
	if (!track) continue;
	if (track->status == 0)
	{
	    if (Tracks.Blacklisted(*track))
	    {
		track->status = 1;
		ri->Set();
	    }
	}
    }
    Display();
    return osContinue;
}

eOSState cMenuTracks::ProcessKey(eKeys Key)
{
    eOSState state = cOsdMenu::ProcessKey(Key);
    if (state == osUnknown && !HasSubMenu() && Current() >= 0) 
    {
	switch (Key) 
	{
	    case kRed:
		state = Toggle();
		break;
	    case kGreen:
	    case kOk:
		state = Play();	      
		break;
	    case k0:
		state = Blacklist();
		break;
	    default: 
		break;
      }
  }
  return state;
}
