/***************************************************************************
 *   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/status.h>
#include <vdr/remote.h>

#include "def.h"
#include "browserStatus.h"
#include "playlist.h"
#include "userIfBase.h"


//----------cFileBrowserStatus--------------------
cFileBrowserStatus::cFileBrowserStatus()
: active_(false), running_(false), restarting_(false)/*, playerStarted_(false)*/, playListActive_(false), 
  xineStarted_(false), normalExit_(false), enterPlayList_(false), closePlayList_(false), mode_(expert), caller_(normal), xineCtrl_ (xine_browse), exitDir_(BASE_DIR), startDir_(BASE_DIR), startFile_(""), playList_(cPlayList::GetDefaultName())
{
}

void cFileBrowserStatus::Replaying(const cControl *Control, const char *Name, const char *FileName, bool On)
{
    /*if(!On && active_ && !running_)
    {
        RestartFileBrowser();
    }*/
}

void cFileBrowserStatus::SetStartDir(std::string dir, std::string file)
{
    startDir_ = dir;
    startFile_ = file;
}

void cFileBrowserStatus::SetCaller(browserCaller caller)
{
    caller_ = caller;
}

void cFileBrowserStatus::XineControl(xineControl cmd)
{
    xineCtrl_ = cmd; 
}

void cFileBrowserStatus::SetMode(browseMode mode)
{
    mode_ = mode;
}

void cFileBrowserStatus::Shutdown(std::string dir, std::string file, std::string playlist, std::string playListEntry)
{
    //printf("----Shutdown: caller = %d, playListActive_= %d,  xineStarted_ = %d\n", caller_, playListActive_, playerStarted_ , xineStarted_);
    if(restarting_) //new startdir has already been set-> don't reset
    {
        goto STANDARDSHUTDOWN;
    }
    else if(xineStarted_)
    {
        //remember dir and mode when replaying
        SetStartDir(dir, file);
        goto ALWAYSRESET;
    }
    else if((mode_ == restricted || mode_ == browseplaylist) && normalExit_)
    {
        cRemote::CallPlugin("mediaplayer");  //go back to mediaplayer 
    }
    SetStartDir(BASE_DIR, "");

STANDARDSHUTDOWN:
    active_ = false;
    playListActive_ = false;
    cUserIfBase::EraseIfTypes();

ALWAYSRESET:
    if(caller_ != svdrp)
    {
        caller_ = normal;
    }
    running_ = false;
    restarting_ = false;
    normalExit_ = false;
    playList_ = playlist;
    playListEntry_ = playListEntry;
    enterPlayList_ = false;
    closePlayList_ = false;
}

void cFileBrowserStatus::Startup()
{
    printf("\n\n\n----Startup: caller = %d, playListActive_= %d, xineStarted_ = %d\n\n\n", caller_, playListActive_, xineStarted_);
    if(caller_ == svdrp)
    {
        SetMode(autostart);
        SetExitDir(BASE_DIR);
        cUserIfBase::EraseIfTypes();
    }
    else if(caller_ == mediaplayer)
    {
        SetMode(restricted);
        SetExitDir(startDir_);
        cUserIfBase::EraseIfTypes();
    }
    else if(caller_ == normal)
    {
        SetMode(expert);
        SetExitDir(BASE_DIR);
        cUserIfBase::EraseIfTypes();
    }
    else if(caller_ == xine && xineStarted_)
    {
	if(xineCtrl_ == xine_browse && playListActive_) 
	{
	     //goto browse playlist mode
             printf("--------xineCtrl_ = xine_browse\n");
             SetMode(browseplaylist);
	}
	else if(xineCtrl_ == xine_edit && playListActive_)
	{ 
             //goto playlist
             printf("--------xineCtrl_ = xine_edit\n");
             enterPlayList_ = true;
	}
	else if(xineCtrl_ == xine_exit)
	{
	     //goto restricted/standard 
             printf("--------xineCtrl_ = xine_exit, GetPreviousIf() = %d\n", cUserIfBase::GetPreviousIf());
             if(cUserIfBase::GetPreviousIf() == if_playlistmenu && playListActive_)
             {
                 enterPlayList_ = true;
             }
	     else if(cUserIfBase::GetPreviousIf() == if_restricted)
             {
                 printf("--------xineCtrl_ = xine_exit:if_restricted\n");
                 if(playListActive_)
                 {
                     closePlayList_ = true;
                 }
                 SetMode(restricted);
             }
             else if(cUserIfBase::GetPreviousIf() == if_standard )
             {
                 printf("--------xineCtrl_ = xine_exit:if_standard\n");
                 if(playListActive_)
                 {
                     closePlayList_ = true;
                 }
                 SetMode(expert);
             }
	}
        else
	{
            SetMode(restricted);
            SetExitDir(startDir_);
	}
    }
    SetFilter();
}

void cFileBrowserStatus::SetFilter()
{
    filter_ = cl_unknown;
    if(mode_ == restricted || mode_ == browseplaylist)
    {
        if(startDir_ == PICTURES_DIR)
        {
            filter_ = cl_picture|cl_playlist;
        }
        else if(startDir_ == MUSIC_DIR)
        {
            filter_ = cl_audio|cl_playlist;
        }
        else if(startDir_ == VIDEO_DIR)
        {
            filter_ = cl_video|cl_playlist;
        }
    }
}

void cFileBrowserStatus::Started()
{
    active_ = true;
    running_ = true;
    xineStarted_ = false; 
    playListActive_ = false;
}

