import vlc

all_events = (
    vlc.EventType.MediaDiscovererEnded,
    vlc.EventType.MediaDiscovererStarted,
    vlc.EventType.MediaDurationChanged,
    vlc.EventType.MediaFreed,
    vlc.EventType.MediaListEndReached,
    vlc.EventType.MediaListItemAdded,
    vlc.EventType.MediaListItemDeleted,
    vlc.EventType.MediaListPlayerNextItemSet,
    vlc.EventType.MediaListPlayerPlayed,
    vlc.EventType.MediaListPlayerStopped,
    vlc.EventType.MediaListViewItemAdded,
    vlc.EventType.MediaListViewItemDeleted,
    vlc.EventType.MediaListViewWillAddItem,
    vlc.EventType.MediaListViewWillDeleteItem,
    vlc.EventType.MediaListWillAddItem,
    vlc.EventType.MediaListWillDeleteItem,
    vlc.EventType.MediaMetaChanged,
    vlc.EventType.MediaParsedChanged,
    vlc.EventType.MediaPlayerAudioDevice,
    vlc.EventType.MediaPlayerAudioVolume,
    vlc.EventType.MediaPlayerBackward,
    vlc.EventType.MediaPlayerBuffering,
    vlc.EventType.MediaPlayerChapterChanged,
    vlc.EventType.MediaPlayerCorked,
    vlc.EventType.MediaPlayerESAdded,
    vlc.EventType.MediaPlayerESDeleted,
    vlc.EventType.MediaPlayerESSelected,
    vlc.EventType.MediaPlayerEncounteredError,
    vlc.EventType.MediaPlayerEndReached,
    vlc.EventType.MediaPlayerForward,
    #vlc.EventType.MediaPlayerLengthChanged,
    vlc.EventType.MediaPlayerMediaChanged,
    vlc.EventType.MediaPlayerMuted,
    vlc.EventType.MediaPlayerNothingSpecial,
    vlc.EventType.MediaPlayerOpening,
    vlc.EventType.MediaPlayerPausableChanged,
    vlc.EventType.MediaPlayerPaused,
    vlc.EventType.MediaPlayerPlaying,
    #vlc.EventType.MediaPlayerPositionChanged,
    vlc.EventType.MediaPlayerScrambledChanged,
    vlc.EventType.MediaPlayerSeekableChanged,
    vlc.EventType.MediaPlayerSnapshotTaken,
    vlc.EventType.MediaPlayerStopped,
    #vlc.EventType.MediaPlayerTimeChanged,
    vlc.EventType.MediaPlayerTitleChanged,
    vlc.EventType.MediaPlayerUncorked,
    vlc.EventType.MediaPlayerUnmuted,
    vlc.EventType.MediaPlayerVout,
    vlc.EventType.MediaStateChanged,
    vlc.EventType.MediaSubItemAdded,
    vlc.EventType.MediaSubItemTreeAdded,
    vlc.EventType.RendererDiscovererItemAdded,
    vlc.EventType.RendererDiscovererItemDeleted,
    vlc.EventType.VlmMediaAdded,
    vlc.EventType.VlmMediaChanged,
    vlc.EventType.VlmMediaInstanceStarted,
    vlc.EventType.VlmMediaInstanceStatusEnd,
    vlc.EventType.VlmMediaInstanceStatusError,
    vlc.EventType.VlmMediaInstanceStatusInit,
    vlc.EventType.VlmMediaInstanceStatusOpening,
    vlc.EventType.VlmMediaInstanceStatusPause,
    vlc.EventType.VlmMediaInstanceStatusPlaying,
    vlc.EventType.VlmMediaInstanceStopped,
    vlc.EventType.VlmMediaRemoved,
)


class AudioPlayer:
    def __init__(self):
        self.instance = vlc.Instance()
        self.media_list_player = self.instance.media_list_player_new()
        self.media_player = self.media_list_player.get_media_player()

        evm = self.media_player.event_manager()
        evm.event_attach(vlc.EventType.MediaPlayerStopped, self._callback)
        evm.event_attach(vlc.EventType.MediaListEndReached, self._callback)

        evm2 = self.media_list_player.event_manager()
        evm2.event_attach(vlc.EventType.MediaListEndReached, self._callback)

        self.on_playlist_end_callback = None

    def create_playlist(self, files):
        result = vlc.MediaList()
        for e in files:
            result.add_media(self.instance.media_new(e))
        return result

    def set_playlist(self, media_list):
        self.media_list_player.set_media_list(media_list)

    def next(self):
        self.media_list_player.next()

    def previous(self):
        self.media_list_player.previous()

    def play(self):
        self.media_list_player.play()

    def play_from_start(self):
        self.media_list_player.play_item_at_index(0)

    def is_playing(self):
        return self.media_list_player.is_playing()

    def pause(self):
        self.media_list_player.pause()

    def _callback(self, event, *args, **kwargs):

        if True:  # event.type == vlc.EventType.MediaPlayerEndReached:
            #print("End reached")
            if self.on_playlist_end_callback:
                self.on_playlist_end_callback()
        #print("Callback from VLC", event, args, kwargs)
        #print(event.meta_type, event.obj, event.type)

    def change_volume(self, amount=1):
        vol = self.media_player.audio_get_volume() + amount
        print("volume", vol)
        if vol > 100:
            vol = 100
        if vol < 0:
            vol = 0
        self.media_player.audio_set_volume(vol)
