# The VLC player
"""
Created on Fri Feb 19 10:40:49 2016

@author: alagu
"""

import vlc
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui

if sys.version_info[0] < 3:
    import Tkinter as Tk
    import ttk
    import FileDialog
    #from filedialog import askopenfilename
else:
    import tkinter as Tk
    import ttk
    import FileDialog
    #from filedialog import askopenfilename

# import standard libraries
import os

from threading import Thread, Event
import time
import platform
import PIL.Image
import PIL.ImageTk


class Player(Tk.Frame):
    """The main window has to deal with events.
    """
    def __init__(self, parent, fname, title=None):
        Tk.Frame.__init__(self, parent)
        self.fname=fname
        self.parent = parent

        if title == None:
            title = "tk_vlc"
        self.parent.title(title)

        # Menu Bar
        #   File Menu
        
	size=256,256
	img = PIL.Image.open("pic.jpg")
	img = img.resize(size)
        background_image=PIL.ImageTk.PhotoImage(img)
        background_label = Tk.Label(self, image=background_image)
        background_label.place(x=0, y=0, relwidth=1, relheight=1)
        background_label.image = background_image
        background_label.pack()
	self.pack()
	menubar = Tk.Menu(self.parent)
        self.parent.config(menu=menubar)
        fileMenu = Tk.Menu(menubar)
        fileMenu.add_command(label="Open", underline=0, command=self.OnOpen)
        fileMenu.add_command(label="Exit", underline=1, command=_quit)
        menubar.add_cascade(label="Player Controls", menu=fileMenu)
	
        self.pack()

        # The second panel holds controls
        self.player = None
        self.videopanel = ttk.Frame(self.parent, width=256, height=256)
        self.canvas = Tk.Canvas(self.videopanel).pack()
        self.videopanel.pack()
#fill=Tk.BOTH,expand=1

        ctrlpanel = ttk.Frame(self.parent, width=256, height=256)
        pause  = ttk.Button(ctrlpanel, text="Pause", command=self.OnPause)
        play   = ttk.Button(ctrlpanel, text="Play", command=self.OnPlay)
        stop   = ttk.Button(ctrlpanel, text="Stop", command=self.OnStop)
        #volume = ttk.Button(ctrlpanel, text="Volume", command=self.OnSetVolume)
	pause.place(x=50, y=200)
        pause.pack(side=Tk.LEFT)
	play.place(x=100, y=200)
        play.pack(side=Tk.LEFT)
	stop.place(x=150, y=200)
        stop.pack(side=Tk.LEFT)
        #volume.pack(side=Tk.LEFT)
        #self.volume_var = Tk.IntVar()
        #self.volslider = Tk.Scale(ctrlpanel, variable=self.volume_var, command=self.volume_sel,from_=0, to=100, orient=Tk.HORIZONTAL, length=100)
        #self.volslider.pack(side=Tk.LEFT)
        ctrlpanel.pack(side=Tk.TOP)

        #ctrlpanel2 = ttk.Frame(self.parent)
        #self.scale_var = Tk.DoubleVar()
        #self.timeslider_last_val = ""
        #self.timeslider = Tk.Scale(ctrlpanel2, variable=self.scale_var, command=self.scale_sel, from_=0, to=1000, orient=Tk.HORIZONTAL, length=500)
        #self.timeslider.pack(side=Tk.BOTTOM, fill=Tk.X,expand=1)
        #self.timeslider_last_update = time.time()
        #ctrlpanel2.pack(side=Tk.BOTTOM,fill=Tk.X)


        # VLC player controls
        self.Instance = vlc.Instance()
        #pdb.set_trace()
        self.player = self.Instance.media_player_new()

        # below is a test, now use the File->Open file menu
        #media = self.Instance.media_new('output.mp4')
        #self.player.set_media(media)
        #self.player.play() # hit the player button
        #self.player.video_set_deinterlace(str_to_bytes('yadif'))

        #self.timer = ttkTimer(self.OnTimer, 1.0)
        #self.timer.start()
        self.parent.update()

        #self.player.set_hwnd(self.GetHandle()) # for windows, OnOpen does does this


    def OnExit(self, evt):
        """Closes the window.
        """
        self.Close()

    def OnOpen(self):
        """Pop up a new dialow window to choose a file, then play the selected file.
        """
        # if a file is already running, then stop it.
        self.OnStop()
        print 'hi'
        # Create a file dialog opened in the current home directory, where
        # you can display all kind of files, having as title "Choose a file".
        self.Media=self.Instance.media_new(self.fname)
        self.player.set_media(self.Media)
        if sys.platform == "darwin":
            vlcApp =QtGui.QApplication(sys.argv)
            vlcWidget = QtGui.QFrame()
            vlcWidget.resize(700,700)
            vlcWidget.show()
            #player = Player(root, sys.argv[1], title="tkinter vlc")
            self.player.set_nsobject(vlcWidget.winId())

        print 'hi', self.player
        self.player.play()
        
        #self.OnPlay()

            # set the volume slider to the current volume
            #self.volslider.SetValue(self.player.audio_get_volume() / 2)
        #self.volslider.set(self.player.audio_get_volume())

    def OnPlay(self):
        """Toggle the status to Play/Pause.
        If no file is loaded, open the dialog window.
        """
        # check if there is a file to play, otherwise open a
        # Tk.FileDialog to select a file
        print 'hi'
        if not self.player.get_media():
            self.OnOpen()
            print 'Opened'
        else:
            # Try to launch the media, if this fails display an error message
            print 'hi'
            if self.player.play() == -1:
                self.errorDialog("Unable to play.")

    def GetHandle(self):
        return self.videopanel.winfo_id()

    #def OnPause(self, evt):
    def OnPause(self):
        """Pause the player.
        """
        self.player.pause()

    def OnStop(self):
        """Stop the player.
        """
        self.player.stop()
        # reset the time slider
        #self.timeslider.set(0)

    '''def OnTimer(self):
        """Update the time slider according to the current movie time.
        """
        if self.player == None:
            return
        # since the self.player.get_length can change while playing,
        # re-set the timeslider to the correct range.
        length = self.player.get_length()
        dbl = length * 0.001
        self.timeslider.config(to=dbl)

        # update the time on the slider
        tyme = self.player.get_time()
        if tyme == -1:
            tyme = 0
        dbl = tyme * 0.001
        self.timeslider_last_val = ("%.0f" % dbl) + ".0"
        # don't want to programatically change slider while user is messing with it.
        # wait 2 seconds after user lets go of slider
        if time.time() > (self.timeslider_last_update + 2.0):
            self.timeslider.set(dbl)'''

    '''def scale_sel(self, evt):
        if self.player == None:
            return
        nval = self.scale_var.get()
        sval = str(nval)
        if self.timeslider_last_val != sval:
            # this is a hack. The timer updates the time slider.
            # This change causes this rtn (the 'slider has changed' rtn) to be invoked.
            # I can't tell the difference between when the user has manually moved the slider and when
            # the timer changed the slider. But when the user moves the slider tkinter only notifies
            # this rtn about once per second and when the slider has quit moving.
            # Also, the tkinter notification value has no fractional seconds.
            # The timer update rtn saves off the last update value (rounded to integer seconds) in timeslider_last_val
            # if the notification time (sval) is the same as the last saved time timeslider_last_val then
            # we know that this notification is due to the timer changing the slider.
            # otherwise the notification is due to the user changing the slider.
            # if the user is changing the slider then I have the timer routine wait for at least
            # 2 seconds before it starts updating the slider again (so the timer doesn't start fighting with the
            # user)
            self.timeslider_last_update = time.time()
            mval = "%.0f" % (nval * 1000)
            self.player.set_time(int(mval)) # expects milliseconds


    def volume_sel(self, evt):
        if self.player == None:
            return
        volume = self.volume_var.get()
        if volume > 100:
            volume = 100
        if self.player.audio_set_volume(volume) == -1:
            self.errorDialog("Failed to set volume")



    def OnToggleVolume(self, evt):
        """Mute/Unmute according to the audio button.
        """
        is_mute = self.player.audio_get_mute()

        self.player.audio_set_mute(not is_mute)
        # update the volume slider;
        # since vlc volume range is in [0, 200],
        # and our volume slider has range [0, 100], just divide by 2.
        self.volume_var.set(self.player.audio_get_volume())

    def OnSetVolume(self):
        """Set the volume according to the volume sider.
        """
        volume = self.volume_var.get()
        # vlc.MediaPlayer.audio_set_volume returns 0 if success, -1 otherwise
        if volume > 100:
            volume = 100
        if self.player.audio_set_volume(volume) == -1:
            self.errorDialog("Failed to set volume")'''

    def errorDialog(self, errormessage):
        """Display a simple error dialog.
        """
        Tk.tkMessageBox.showerror(self, 'Error', errormessage)

def Tk_get_root():
    if not hasattr(Tk_get_root, "root"): #(1)
        Tk_get_root.root= Tk.Tk()  #initialization call is inside the function
    return Tk_get_root.root

def _quit():
    print("_quit: bye")
    root = Tk_get_root()
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
    #os.system('./populate_user_ui.sh ')    
            # Fatal Python Error: PyEval_RestoreThread: NULL tstate
    os._exit(1)

if __name__ == "__main__":
    
    # Create a Tk.App(), which handles the windowing system event loop
    root = Tk_get_root()
    root.protocol("WM_DELETE_WINDOW", _quit)
    if len(sys.argv[1:]) == 1:
        # Only 1 file. Simple interface
        print sys.platform
        
        
        player = Player(root, sys.argv[1], title="tkinter vlc")
    # show the player window centred and run the application
    root.mainloop()
