#include "config.h"
#include "VPlayer.h"
#include "exception.h"
#include <windows.h>
#include <unistd.h>

#if _THIS_BUILD==_BUILD_WINDOWS
#include "libvlc_w32.h"
#endif // _THIS_BUILD

VPlayer::VPlayer(const int argc, const char **argv, const std::string &root_path) {

	libvlc_connect_library();

	if ( (m_instance = libvlc_new(argc, argv))==NULL )
		throw PammmException("Cannot create libvlc instance.");
	if ( (m_player = libvlc_media_player_new(m_instance))==NULL )
		throw PammmException("Cannot create player instance.");

	for ( int ii=0 ; ii<MAX_LEVELS ; ii++ ){
		Level *lp;
		try {
			lp = new Level(m_instance, root_path, '1'+ii);
			}
		catch ( PammmException *x ){
			x->Show();
			}
		if ( lp->GetCount()>0 )
			m_common_levels.push_back(lp);
		else delete lp;
	}
	m_fast_level = new Level(m_instance, root_path, 'c');
	m_slow_level = new Level(m_instance, root_path, 'x');

	/* Create window and assign to player */

	RECT screensize;
	HWND desktop = ::GetDesktopWindow();
    GetWindowRect(desktop, &screensize);
	m_window_handle = ::CreateWindow(TEXT("static"),
		   TEXT(" "),
		   WS_POPUP | WS_VISIBLE,

		   40,40, 640, 480,
	//	   screensize.left,									//@@@@ TODO: restore full-screen window
	//	   screensize.top,
	//	   screensize.right - screensize.left,
	//	   screensize.bottom - screensize.top,
		   desktop, NULL, GetModuleHandle(NULL), 0);

	m_current_level_it = m_common_levels.begin();
	m_current_level = *m_current_level_it;

#if _THIS_BUILD==_BUILD_WINDOWS
	libvlc_media_player_set_hwnd(m_player, m_window_handle);
#endif // _THIS_BUILD
}

VPlayer::~VPlayer() {

	libvlc_media_player_release(m_player);
	//??? TODO: Do we need to release media, etc. first?
	libvlc_release(m_instance);

	::DestroyWindow(m_window_handle);
}

void VPlayer::Start(){
	libvlc_media_t *media = m_current_level->GetThis();
	assign_media(media);
}

void VPlayer::GoNext() {
	libvlc_media_t *media = m_current_level->StepNext();
	assign_media(media);
}

void VPlayer::GoPrev(){
	libvlc_media_t *media = m_current_level->StepPrev();
	assign_media(media);
}

void VPlayer::GoUp(){
	if ( m_current_level!=m_fast_level && m_current_level!=m_slow_level &&
			m_current_level!=*(m_common_levels.end()-1) ){
		m_current_level_it++;
		m_current_level = *m_current_level_it;
		libvlc_media_t *media = m_current_level->GetThis();
		assign_media(media);
		}
}

void VPlayer::GoDown(){
	if ( m_current_level!=m_fast_level && m_current_level!=m_slow_level &&
			m_current_level!=*(m_common_levels.begin()) ){
		m_current_level_it--;
		m_current_level = *m_current_level_it;
		libvlc_media_t *media = m_current_level->GetThis();
		assign_media(media);
		}
}

void VPlayer::GoFast(){
	Level *level = m_fast_level ? m_fast_level : *((m_common_levels.end())-1);
	assign_media(level->GetThis());
}

void VPlayer::GoSlow(){
	Level *level = m_slow_level ? m_slow_level : *m_common_levels.begin();
	assign_media(level->GetThis());
}

//@@@ TODO: Restart() goes here.

void VPlayer::assign_media(libvlc_media_t *media){
	int is_playing = libvlc_media_player_is_playing(m_player);
	if ( media==NULL ){
		if ( is_playing )
			libvlc_media_player_stop(m_player);
		}
	else {
		libvlc_media_player_set_media(m_player, media);
		if ( !is_playing )
			libvlc_media_player_play(m_player);

		}
}
