/***************************************************************************
 *   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.             *
 ***************************************************************************/
 
#ifndef P__CONVERT_H
#define P__CONVERT_H

#include <string>
#include <list>
#include <map>
#include <vector>

#include <vdr/thread.h>

#include "playlist.h"
#include "fileType.h"

class cFileCache;

enum cvStatus { stNone, stRun, stCopying, stFin};

struct InListElement
{
    cPlayListItem item;
    bool live;
    bool operator==(const InListElement &elem)
    {
        return (item == elem.item);
    } 
}; 

struct OutListElement
{
    cPlayListItem item;
    bool valid;
    std::vector<unsigned char> buffer; 
}; 

//-----------cImageConvert-------------

class cImageConvert : public cThread 
{
public:
    cImageConvert(cFileCache *cache);
    ~cImageConvert();
    void InsertInConversionList(const cPlayListItem &item, bool live);
    void InsertInConversionList(const std::map<std::string, cPlayListItem> &newfiles);
    void RemoveFromConversionList(const cPlayListItem &item, bool lock = true);
    bool GetConvertedFile(const cPlayListItem &item, OutListElement& elem);
    void ClearConvertedFiles();
    void StartConversion();
    void WaitUntilCopied();
    bool IsInConversion(const cPlayListItem &item);
    void CancelConversion();

protected:
    
    /*override*/ void Action();    
    virtual int CallConversionScript(std::string img, std::string type, std::string resImg,
            std::string width, std::string height, std::string aspect);
    cFileCache *cache_;
    cCondWait cond;
    cPlayListItem currentFile_;
    std::list<OutListElement> outFiles_;
    std::list<InListElement> inFiles_;
    std::vector<unsigned char> buffer_;
    volatile cvStatus status_;
    bool live_;

    std::string GetFileTypeString(efiletype type);
    void PutInOutputList(bool success);
    bool GetFromInputList();
    void GetScreenParams(std::string &widthStr, std::string &heightStr, std::string &aspectStr);
    int ReadFileInBuffer(std::string img);
    int CopyFile(std::string destFile, std::string srcFile);
};

//-----------cThumbConvert-------------

class cThumbConvert : public cImageConvert 
{
public:
    cThumbConvert(cFileCache *cache);
    ~cThumbConvert(){};

protected:
     /*override*/ void Action();
     /*override*/ int CallConversionScript(std::string img, std::string type, std::string resImg,
           std::string width, std::string height, std::string aspect);
};

//----inline-----

inline cImageConvert::cImageConvert(cFileCache *cache) 
: cache_(cache), status_(stNone), live_(false)
{
}

inline cThumbConvert::cThumbConvert(cFileCache *cache) 
: cImageConvert(cache)
{
}

#endif //P__CONVERT_H



