/*
* Copyright (c) 2011 Nokia Corporation.
*/

#ifndef MIRRORITEM_H
#define MIRRORITEM_H

#include <QDeclarativeItem>
#include <QPainter>
#include <QTimer>

// QtMultimedia (from Qt) classes
// use with QtMobility 1.1.2
//#include <QtMultimedia/QAbstractVideoSurface>
//#include <QtMultimedia/QVideoSurfaceFormat>

// remove with with QtMobility 1.1.2
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>


// Unlike the other APIs in QtMobility,
// the QTMobility Multimedia API is not in the QtMobility namespace
#include <QCamera>
#include <QCameraImageCapture>
#include <QCameraViewfinder>
#include <QVideoRendererControl>


#include "cmirrorhouse.h"

/* ***************************************************************************
* MyVideoSurface
*/
class CameraUpdateIF
{
public:
    virtual void updateVideo() = 0;
};
class MyVideoSurface: public QAbstractVideoSurface
{
Q_OBJECT

public:
    MyVideoSurface(QDeclarativeItem* widget, CameraUpdateIF* target, QObject * parent = 0);
    ~MyVideoSurface();

    void enableEffect(int id, double strength,double count);

    bool start(const QVideoSurfaceFormat &format);

    bool present(const QVideoFrame &frame);

    QList<QVideoFrame::PixelFormat> supportedPixelFormats(
                QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const;

    bool isFormatSupported(const QVideoSurfaceFormat &format, QVideoSurfaceFormat *similar) const;

    void paint(QPainter*);

    bool framesExists();

    QSize targetImageSize();
    QImage::Format targetImageFormat();


public:
    QImage                  m_targetImage;

private:
    QDeclarativeItem*       m_targetWidget;
    CameraUpdateIF*         m_target;
    QVideoFrame             m_frame;
    QImage::Format          m_imageFormat;
    QVideoSurfaceFormat     m_videoFormat;
    CMirrorHouse*           m_mirrorHouseEffect;
    QImage                  m_sourceImage;

    double                  m_strength;
    double                  m_count;
    int                     m_effectId;
    bool                    m_framesExists;
};


/* ***************************************************************************
* MirrorItem
*/
class MirrorItem : public QDeclarativeItem, public CameraUpdateIF
{
    Q_OBJECT
public:
    explicit MirrorItem(QDeclarativeItem *parent = 0);
    virtual ~MirrorItem();
    void updateVideo();
    void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);

public:
    Q_INVOKABLE void enableCamera(QVariant enable);
    Q_INVOKABLE void enableCameraAt(QVariant enable,QVariant cameraIndex);
    Q_INVOKABLE void enableEffect(QVariant id, QVariant strength,QVariant count);
    Q_INVOKABLE void pauseCamera();
    Q_INVOKABLE QVariant isTouch(); // TODO: remove this?
    Q_INVOKABLE void saveToFile();

    bool sceneEvent(QEvent *event);


private slots:
    void startCamera();
    void stopCamera();
    void error(QCamera::Error);

signals:
    void minimizeMirror();
    void maximizeMirror();

private:
    void keepPaintingStoredPicture();
    void doSave(QImage image);


private:
    QCamera*                m_camera;
    MyVideoSurface*         m_myVideoSurface;
    bool                    m_showViewFinder;

    bool                    m_keepPaintingStoredPicture;
    QImage                  m_lastPicture;

    QList<QByteArray>       m_devices;
    int                     m_deviceId;

    double                  m_strength;
    double                  m_count;
    int                     m_effectId;
    int                     m_pinchCounter;
    bool                    m_touchEnabled;
    bool                    m_signalSended;

};

QML_DECLARE_TYPE(MirrorItem)

#endif // MIRRORITEM_H
