/*
 * This file is part of libtqtvd_astronav
 *
 * Copyright (C) 2008-2017 TQTVD Software
 * Copyright (C) 2017 Mirakulo Software (mirakulo@mirakulo.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */


#ifndef AstroNavPlayer_h
#define AstroNavPlayer_h

#include "IAstroNavPlayer.h"
#include "BIListener.h"
#include <pthread.h>

#include "IGraphicManagerAdapter.h"

#include "TQTVD_common.h"

namespace AstroNav {

DECLARE_SHARE_PTR(AstroNavPlayer)
    class AstroNavPlayer : public virtual IAstroNavPlayer, public virtual BAL::BIListener,
    DECLARE_ENABLE_SHARED_FROM_THIS(IAstroNavPlayer)
    {
        public:

            AstroNavPlayer ( IGraphicManagerAdapter        *gm
                           , IAstroNavPlayerUpdateListenerWPtr listener
                           , IAstroNavPlayerFileServerWPtr     fileServer
                           , IAstroNavPlayerCoreServiceProviderWPtr coreServices );

            virtual ~AstroNavPlayer ();

        public:
		virtual void play(IGraphicManagerAdapterSurface* dest, std::string url,
                          int x0, int y0, int w, int h, std::string css = "");
		virtual void stop (void);
		virtual void unload(void);
		virtual void update(void* sourceSurface, WebCore::IntRect srcRect, WebCore::IntRect targetRect);
		virtual void* getWindow(void) { return m_window; }
		void setFocus(void);
		bool hasFocus(void);
		void setBackgroundColor(RGBA32 color);
		RGBA32 getBackgroundColor(void);

		virtual void rcEventReceived (int aKeyCode);
		virtual void kbEventReceived ( int aKeyCode,  bool bShiftKey, bool bCtrlKey, bool bAltKey, bool bMetaKey);
		virtual void setRect(int x, int y, int w, int h);	
		virtual void mouseEventReceived(MouseButton mouseButton, MouseEventType type, int x, int y);

		virtual void back();
		virtual void forward();
		virtual void setURL(std::string url, std::string css);
		virtual void reload() ;
		virtual void cancel();

        virtual wait_func_t getWaitFunc();

		AstroNavPlayerFileServerWrapper *fileServerWrapper() const { return m_fileServerWrapper; }
		IAstroNavPlayerCoreServiceProviderWPtr coreServices() { return m_coreServices; }

        private:
			virtual void play_(IAstroNavPlayerArgs *args);
			virtual void stop_(void);

            IGraphicManagerAdapterSurface* createSurface( int width, int height );
            void releaseSurface( IGraphicManagerAdapterSurface* surface );

            Status            m_status;
	        std::string m_url;

            IGraphicManagerAdapter*        m_gm;
            void*             m_window;
            IAstroNavPlayerUpdateListenerWPtr m_listener;

            IGraphicManagerAdapterSurface* m_internalSurface;
            IGraphicManagerAdapterSurface* m_targetSurface;
            int               m_width;
            int               m_height;

            RGBA32            m_backgroundColor;

			AstroNavPlayerFileServerWrapper * m_fileServerWrapper;
			IAstroNavPlayerCoreServiceProviderWPtr m_coreServices;

            friend void* handleEvent(void *pthis);


    };

}

#endif

