/*
 * vdr-podcatcher - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2006 Tobias Grimm <vdr@e-tobi.net>
 *
 * 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
 *
 * $Id: playbackcontrol.cc 3342 2006-04-24 21:30:53Z svntobi $
 *
 */

#include "playbackcontrol.h"
#include "thread.h"
#include "playbackcontroller.h"
#include "vdrpesplayer.h"

using namespace std;

PlaybackControl::PlaybackControl(Item item, ref_ptr<VdrPesPlayer> player,
  ref_ptr<PlaybackController> playbackController)
:cControl(player.get()), item(item), player(player),
  playbackController(playbackController), replayDisplay(NULL)
{
}

PlaybackControl::~PlaybackControl()
{
    Hide();
}

void PlaybackControl::Hide()
{
    delete replayDisplay;
    replayDisplay = NULL;
}

eOSState PlaybackControl::ProcessKey(eKeys Key)
{
    if (playbackController->IsPlaybackFinished())
    {
        return osEnd;
    }

    eOSState state = cControl::ProcessKey(Key);

    switch (Key)
    {
        case kOk:
        {
            if (replayDisplay)
            {
                Hide();
            }
            else
            {
                replayDisplay = Skins.Current()->DisplayReplay(false);
                replayDisplay->SetTitle(item.GetTitle().c_str());
            }
            break;
        }

        case kDown:
        {
            playbackController->Pause();
            break;
        }

        default:
        {
        }
    }

    if (replayDisplay)
    {
        char* timeIndexString;

        int hours = player->GetTimeIndex() / (60 * 60);
        int minutes = (player->GetTimeIndex() / 60) % 60;
        int seconds = player->GetTimeIndex() % 60;
        asprintf(&timeIndexString, "%.2d:%.2d:%.2d", hours, minutes, seconds);
        replayDisplay->SetCurrent(timeIndexString);

        hours = player->GetTotalTime() / (60 * 60);
        minutes = (player->GetTotalTime() / 60) % 60;
        seconds = player->GetTotalTime() % 60;
        asprintf(&timeIndexString, "%.2d:%.2d:%.2d", hours, minutes, seconds);
        replayDisplay->SetTotal(timeIndexString);

        replayDisplay->SetProgress(player->GetTimeIndex(), player->GetTotalTime());
        free(timeIndexString);

        replayDisplay->SetMode(playbackController->IsPlaying(), true, -1);
    }

    return state;
}
