#include "config.h"

#if _THIS_BUILD==_BUILD_WINDOWS

#include "libvlc_w32.h"
#include <windows.h>

HMODULE module;

typedef libvlc_instance_t *(WINAPI *t_libvlc_new)(int argc, const char *const *argv);
t_libvlc_new p_libvlc_new;

typedef libvlc_media_t *(WINAPI *t_libvlc_media_new_path)(libvlc_instance_t *p_instance, const char *path);
t_libvlc_media_new_path p_libvlc_media_new_path;

typedef libvlc_media_player_t *(WINAPI *t_libvlc_media_player_new_from_media)(libvlc_media_t *m);
t_libvlc_media_player_new_from_media p_libvlc_media_player_new_from_media;

typedef void (WINAPI *t_libvlc_media_release)(libvlc_media_t *m);
t_libvlc_media_release p_libvlc_media_release;

typedef void (WINAPI *t_libvlc_media_player_release)(libvlc_media_player_t *mp);
t_libvlc_media_player_release p_libvlc_media_player_release;

typedef int  (WINAPI *t_libvlc_media_player_play)(libvlc_media_player_t *p_mi);
t_libvlc_media_player_play p_libvlc_media_player_play;

typedef void  (WINAPI *t_libvlc_media_player_stop)(libvlc_media_player_t *p_mi);
t_libvlc_media_player_stop p_libvlc_media_player_stop;

typedef void (WINAPI *t_libvlc_release)(libvlc_instance_t *p_instance);
t_libvlc_release p_libvlc_release;

typedef int   (WINAPI *t_libvlc_media_player_is_playing)(libvlc_media_player_t *p_mi);
t_libvlc_media_player_is_playing p_libvlc_media_player_is_playing;

typedef void  (WINAPI *t_libvlc_media_player_set_media)(libvlc_media_player_t *p_mi, libvlc_media_t *p_md);
t_libvlc_media_player_set_media p_libvlc_media_player_set_media;

typedef libvlc_media_player_t *(WINAPI *t_libvlc_media_player_new)(libvlc_instance_t *p_libvlc_instance);
t_libvlc_media_player_new p_libvlc_media_player_new;

typedef libvlc_media_t *(WINAPI *t_libvlc_media_duplicate)(libvlc_media_t *p_media);
t_libvlc_media_duplicate p_libvlc_media_duplicate;

typedef void (WINAPI *t_libvlc_media_player_set_hwnd)(libvlc_media_player_t *p_mi, void *drawable);
t_libvlc_media_player_set_hwnd p_libvlc_media_player_set_hwnd;

int libvlc_connect_library(){
	module = LoadLibrary("libvlc.dll");
	if ( !module ){
		int err = GetLastError();
	}
	if ( module ){
		p_libvlc_new = (t_libvlc_new)GetProcAddress(module, "libvlc_new");
		p_libvlc_media_new_path = (t_libvlc_media_new_path)GetProcAddress(module, "libvlc_media_new_path");
		p_libvlc_media_player_new_from_media = (t_libvlc_media_player_new_from_media)GetProcAddress(module, "libvlc_media_player_new_from_media");
		p_libvlc_media_release = (t_libvlc_media_release)GetProcAddress(module, "libvlc_media_release");
		p_libvlc_media_player_release = (t_libvlc_media_player_release)GetProcAddress(module, "libvlc_media_player_release");
		p_libvlc_media_player_play = (t_libvlc_media_player_play)GetProcAddress(module, "libvlc_media_player_play");
		p_libvlc_media_player_stop = (t_libvlc_media_player_stop)GetProcAddress(module, "libvlc_media_player_stop");
		p_libvlc_release = (t_libvlc_release)GetProcAddress(module, "libvlc_release");
		p_libvlc_media_player_is_playing = (t_libvlc_media_player_is_playing)GetProcAddress(module, "libvlc_media_player_is_playing");
		p_libvlc_media_player_set_media = (t_libvlc_media_player_set_media)GetProcAddress(module, "libvlc_media_player_set_media");
		p_libvlc_media_player_new = (t_libvlc_media_player_new)GetProcAddress(module, "libvlc_media_player_new");
		p_libvlc_media_duplicate = (t_libvlc_media_duplicate)GetProcAddress(module, "libvlc_media_duplicate");
		p_libvlc_media_player_set_hwnd = (t_libvlc_media_player_set_hwnd)GetProcAddress(module, "libvlc_media_player_set_hwnd");
		return 1;
	}

	return module!=0;
}

int libvlc_disconnect_library(){
	FreeLibrary(module);
	return 1;
}

libvlc_instance_t *libvlc_new(int argc, const char *const *argv){
	return (*p_libvlc_new)(argc, argv);
}

libvlc_media_t *libvlc_media_new_path(libvlc_instance_t *p_instance, const char *path){
	return (*p_libvlc_media_new_path)(p_instance, path);
}

libvlc_media_player_t *libvlc_media_player_new_from_media(libvlc_media_t *m){
	return (*p_libvlc_media_player_new_from_media)(m);
}

void libvlc_media_release(libvlc_media_t *m){
	return (*p_libvlc_media_release)(m);
}

void libvlc_media_player_release(libvlc_media_player_t *mp){
	return (*p_libvlc_media_player_release)(mp);
}

int libvlc_media_player_play(libvlc_media_player_t *mp){
	return (*p_libvlc_media_player_play)(mp);
}

void libvlc_media_player_stop(libvlc_media_player_t *mp){
	(*p_libvlc_media_player_stop)(mp);
}

void libvlc_release(libvlc_instance_t *p_instance){
	(*p_libvlc_release)(p_instance);
}

int libvlc_media_player_is_playing(libvlc_media_player_t *mp){
	return (*p_libvlc_media_player_is_playing)(mp);
}

void libvlc_media_player_set_media(libvlc_media_player_t *p_mi, libvlc_media_t *p_md){
	(*p_libvlc_media_player_set_media)(p_mi, p_md);
}

libvlc_media_player_t *libvlc_media_player_new(libvlc_instance_t *p_libvlc_instance){
	 return (*p_libvlc_media_player_new)(p_libvlc_instance);
}

libvlc_media_t *libvlc_media_duplicate(libvlc_media_t *p_libvlc_media){
	 return (*p_libvlc_media_duplicate)(p_libvlc_media);
}

void libvlc_media_player_set_hwnd(libvlc_media_player_t *p_mi, void *drawable){
	(*p_libvlc_media_player_set_hwnd)(p_mi, drawable);
}

#endif // _THIS_BUILD
