/***************************************************************************
 *   Copyright (C) 2005 by Reel Multimedia                                 *
 *                                                                         *
 *                                                                         *
 *   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 "vdr/remote.h"

#include "menuBrowser.h"
#include "setup.h"
#include "i18n.h"
#include "def.h"
#include "filecache.h"
#include "convert.h"
#include "filebrowser.h"
#include <cstring>

static const char *VERSION  = "0.30";

cFileBrowserStatus cPluginFileBrowser::status;
cFileCache *cPluginFileBrowser::cache = NULL;
cImageConvert *cPluginFileBrowser::convert = NULL;
cThumbConvert *cPluginFileBrowser::thumbConvert = NULL;

cPluginFileBrowser::cPluginFileBrowser()
{ 
}

cPluginFileBrowser::~cPluginFileBrowser()
{
    delete cache;
    delete convert;
}

const char *cPluginFileBrowser::Version()
{
    return VERSION;
}

const char *cPluginFileBrowser::Description()
{ 
    return DESCRIPTION;
}

bool cPluginFileBrowser::ProcessArgs(int argc, char *argv[])
{
    std::string basedir, startfile;
    if(argc >= 3 && std::string(argv[1]) == "-basedir")
    {
        basedir = argv[2];
    }
    if(argc >= 5 && std::string(argv[3]) == "-startfile")
    {
        startfile = argv[4]; 
    }
    if(basedir != "")
    { 
        status.SetStartDir(basedir, startfile);
    } 
    return true;
}

bool cPluginFileBrowser::Initialize()
{
    RegisterI18n(Phrases);

    cache = new cFileCache;
    if(!(cache->Open(BROWSER_CACHE, MAX_CACHE_SIZE) || cache->Open(TMP_CACHE, TMP_CACHE_SIZE)))
    {
        DELETENULL(cache);
    }
    convert = new cImageConvert(cache);
    thumbConvert = new cThumbConvert(cache);

    return true;
}

cMenuSetupPage *cPluginFileBrowser::SetupMenu()
{
    return new cFileBrowserSetupPage(cache);
}

bool cPluginFileBrowser::SetupParse(const char *Name, const char *Value)
{
    return fileBrowserSetup.SetupParse(Name, Value);
}

cOsdObject *cPluginFileBrowser::MainMenuAction(void)
{
    std::string titlestring = tr(DESCRIPTION) + std::string(": ") + GetLastN(status.GetStartDir(), 20); 
    return new cMenuFileBrowser(titlestring, &status, cache, convert, thumbConvert);
}

bool cPluginFileBrowser::Service(char const *id, void *data)
{
    if (std::strcmp(id, "Filebrowser set startdir") == 0)
    { 
        //printf("------------Filebrowser set startdir---------------\n");
        struct FileBrowserSetStartDir
        {
            char const *dir;
            char const *file;
        };
        status.SetStartDir( static_cast<FileBrowserSetStartDir *>(data)->dir,
                            static_cast<FileBrowserSetStartDir *>(data)->file);
        return true;
    }
    else if (std::strcmp(id, "Filebrowser set playlist") == 0)
    { 
        //printf("------------Filebrowser set playlist---------------\n");
        status.PlayListActive(true);
        struct FileBrowserSetPlayList
        {
            char const *file;
        };
        status.SetPlayList( static_cast<FileBrowserSetPlayList *>(data)->file);
        return true;
    }
    else if (std::strcmp(id, "Filebrowser set caller") == 0)
    {
        //printf("------------Filebrowser set browsemode---------------\n");
        struct FileBrowserSetCaller
        {
            browserCaller caller;
        };
        status.SetCaller(static_cast<FileBrowserSetCaller *>(data)->caller);
        return true;
    }
    /*else if (std::strcmp(id, "Filebrowser control") == 0)
    {
        //printf("------------Filebrowser activate---------------\n");
        struct FileBrowserControl
        {
            browserControl cmd;
        };
        status.Control(static_cast<FileBrowserControl *>(data)->cmd);
        return true;
    }*/
    else if (std::strcmp(id, "Filebrowser xine control") == 0)
    {
        //printf("------------Filebrowser xine control---------------\n");
        struct FileBrowserControl
        {
            xineControl cmd;
        };
        status.XineControl(static_cast<FileBrowserControl *>(data)->cmd); 
        return true;
    }
    else if (std::strcmp(id, "Filebrowser set playlist entry") == 0)
    {
        struct FileBrowserSetPlayListEntry
        {
            char const *entry;
        };
        std::string entry = static_cast<FileBrowserSetPlayListEntry *>(data)->entry;
        status.SetPlayListEntry(entry);
    }
    return false;
}

const char **cPluginFileBrowser::SVDRPHelpPages(void)
{
    //printf("-----------cPluginFileBrowser::SVDRPHelpPages-----------\n");
    static const char *HelpPages[] = 
    {
        "OPEN\n"
        "    Open filebrowser plugin.",
        NULL
    };
    return HelpPages;
}

cString cPluginFileBrowser::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
    if (strcasecmp(Command, "OPEN") == 0) 
    {
        //set new start directory
        std::string option = Option;
        if(option == "")
        {
            option = BASE_DIR;
        }
        status.SetStartDir(option, "");
        status.SetCaller(svdrp);

        cRemote::CallPlugin("filebrowser"); 

        std::string msg = "Opening filebrowser plugin, startdir = " + option;
        return cString(msg.c_str());
    }
    return NULL;
}

VDRPLUGINCREATOR(cPluginFileBrowser); // Don't touch this!

