/***************************************************************************
 *   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.             *
 ***************************************************************************/

// BkgPicPlayer.c

#include "BkgPicPlayer.h"

#include "config.h"
#include "ReelBoxDevice.h"

#include <vector>

namespace Reel
{
    int const BKGND_IMG_INTERVAL = 15;

    BkgPicPlayer::BkgPicPlayer(ReelBoxDevice &device)
    :   device_(device), stop_(false),
        lastPicTime_(0), nextPic_(0), picInterval_(15), running_(false)
    {
    }

    BkgPicPlayer::~BkgPicPlayer()
    {
        Stop();
    }

    void BkgPicPlayer::Action()
    {
        MutexLocker lock(mutex_);
        picCount_ = 0;
        while (!stop_)
        {
            ShowPic();
            Sleep();
        }
    }

    void BkgPicPlayer::ShowPic()
    {
        int now = ::time(0);
        if (now >= lastPicTime_ + picInterval_)
        {
            lastPicTime_ = now;
            ++ nextPic_;
        }
        ShowPic2();
        ++ picCount_;
    }

    void BkgPicPlayer::ShowPic2()
    {
	char bkgndImgFileName[512];
#if defined RBLITE || ! defined(REELVDR)
	::sprintf(bkgndImgFileName, BACKGROUND_IMAGE, nextPic_);
#else
	cPlugin *plugin=cPluginManager::GetPlugin("music");
	if (plugin && plugin->Service("running playback")) {
	 ::sprintf(bkgndImgFileName, BACKGROUND_IMAGE2, nextPic_);
	 } else {
	 ::sprintf(bkgndImgFileName, BACKGROUND_IMAGE, nextPic_);
	}
#endif	

        FILE *imgFile = ::fopen(bkgndImgFileName, "rb");

        if (imgFile)
        {
            struct stat fileStat;
            ::stat(bkgndImgFileName, &fileStat);
            int fileSize = fileStat.st_size;

            if (fileSize > 0)
            {
                std::vector<Byte> buffer(fileSize);
    
                Int r = ::fread(&buffer[0], 1, fileSize, imgFile);
    
                if (r == fileSize)
                {
                    device_.StillPicture(&buffer[0], fileSize);
                }
            }
            ::fclose(imgFile);
        }
        else if (nextPic_ != 1)
        {
            nextPic_ = 1;
            ShowPic2();
        }
    }

    void BkgPicPlayer::Sleep()
    {
        int const minT = 2;
        int t = picCount_ < 15 ? minT : lastPicTime_ + picInterval_ - ::time(0);
        if (t < minT)
        {
            t = minT;
        }
        condWake_.Wait(mutex_, t * 1000000000LL);
    }

    void BkgPicPlayer::Start()
    {
        if (!running_)
        {
            running_ = true;
            MutexLocker lock(mutex_);
            stop_ = false;
            thread_.Start(*this, &BkgPicPlayer::Action);
        }
    }

    void BkgPicPlayer::Stop()
    {
        if (running_)
        {
            {
                MutexLocker lock(mutex_);
                stop_ = true;
                condWake_.Broadcast(mutex_);
            }
            thread_.Join();
            running_ = false;
        }
    }
}
