/*
 * 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: itemmenupresenter.cc 3407 2006-04-26 18:08:29Z svntobi $
 *
 */

#include <vdr/player.h>
#include "itemmenupresenter.h"
#include "servicelocator.h"
#include "downloadpool.h"
#include "downloadcache.h"
#include <sstream>
#include "itembuttonsupdater.h"

using namespace std;

ItemMenuPresenter::ItemMenuPresenter(IServiceLocator& serviceLocator, Feed feed)
:serviceLocator(serviceLocator), feed(feed), itemButtonsUpdater(serviceLocator)
{
}

void ItemMenuPresenter::Initialize(IListMenu* listMenu)
{
    menu = listMenu;
    menu->SetTitle(feed.GetTitle());
    podcastsOnly = false;
    UpdateMenu();
}

eOSState ItemMenuPresenter::ProcessKey(int selectedItem, eKeys key,
  eOSState state)
{
    if (state == osUnknown)
    {
        if (key == kYellow)
        {
            TogglePodcastFilter();
            return osContinue;
        }

        if (IsValidItemSelection(selectedItem))
        {
            if (ProcessItemAction(key, displayedItems[selectedItem]))
            {
                return osContinue;
            }
        }
    }

    if (menu->IsActiveMenu())
    {
        if ((key == kDown) || (key == kUp) || (key == kRight) ||
          (key == kLeft) || (key == kNone))
        {
            UpdateDownloadAndPlayButton(selectedItem);
        }
    }

    return state;
}

void ItemMenuPresenter::TogglePodcastFilter()
{
    menu->Clear();
    podcastsOnly = !podcastsOnly;
    UpdateMenu();
}

bool ItemMenuPresenter::IsValidItemSelection(int selectedItem)
{
    int maxItems = displayedItems.size();
    return ((selectedItem < maxItems) && (selectedItem >= 0));
}

bool ItemMenuPresenter::ProcessItemAction(eKeys key, Item item)
{
    switch (key)
    {
        case kOk:
        {
            ShowItemView(item);
            break;
        }

        case kRed:
        {
            DownloadItem(item);
            break;
        }

        case kGreen:
        {
            PlayItem(item);
            break;
        }

        default:
        {
            return false;
        }
    }

    return true;
}

void ItemMenuPresenter::ShowItemView(Item item)
{
    cOsdMenu* itemView = serviceLocator.CreateItemView(feed, item);
    menu->ShowSubMenu(itemView);
}

void ItemMenuPresenter::DownloadItem(Item item)
{
    ref_ptr<Download> download(new Download(item.GetMp3Url()));
    serviceLocator.GetDownloadPool()->AddDownload(download);
}

void ItemMenuPresenter::PlayItem(Item item)
{
    if (item.IsPodcast())
    {
        if (serviceLocator.GetDownloadCache()->IsCached(item.GetMp3Url()))
        {
            cControl::Launch(serviceLocator.CreatePlayerControl(item));
        }
    }
}

void ItemMenuPresenter::UpdateMenu()
{
    UpdateItemList();
    UpdateFilterButton();
    UpdateDownloadAndPlayButton(0);

    menu->Refresh();
}

void ItemMenuPresenter::UpdateItemList()
{
    ItemList items = feed.GetItems();

    displayedItems.clear();

    for (ItemList::iterator i = items.begin(); i != items.end(); i++)
    {
        if (!podcastsOnly || (*i).IsPodcast())
        {
            menu->AddItem((*i).GetDate().ToShortString() + "\t" + (*i).GetTitle());
            displayedItems.push_back(*i);
        }
    }
}

void ItemMenuPresenter::UpdateFilterButton()
{
    if (podcastsOnly)
    {
        menu->SetYellowHelp(tr("all"));
    }
    else
    {
        menu->SetYellowHelp(tr("MP3 only"));
    }
}

void ItemMenuPresenter::UpdateDownloadAndPlayButton(int selectedItem)
{
    if (IsValidItemSelection(selectedItem))
    {
        Item item = displayedItems[selectedItem];
        itemButtonsUpdater.UpdateButtonHelp(menu, item);
    }
}
