import gi
gi.require_version("Gtk", "3.0")

from gi.repository import Gtk, Gdk

import id3tag # Our ID3 tag writer
import config

# Gtk.Box pack_start() / pack_end() docs:
# pack_start (child, expand, fill, padding)

class CalendarWindow(Gtk.Window):
    def __init__(self):
        super().__init__(title="Date")
        self.set_position(Gtk.WindowPosition.CENTER)

        self.calendar = Gtk.Calendar()
        # Show the calendar
        self.add(self.calendar)
        
        # Hook a double click event
        self.calendar.connect("day-selected-double-click", self.daySelected)

    def daySelected(self, widget):
        print(widget.get_date())

class ID3Window(Gtk.Window):
    def __init__(self, filename):
        # Call the super class's init method, set the window title
        super().__init__(title="Edit ID3 tag")
        # Set the window's position to the center of the screen
        self.set_position(Gtk.WindowPosition.CENTER)
        self.filename = filename

        # Our top level vertical box, which will contain
        # multiple horizontal boxes,
        # which will themselves then contain the text labels and editable entries.
        top_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(top_vbox)
        
        # First, just a label of the filename
        hbox_filename = Gtk.Box(spacing=6)
        filenameLabel = Gtk.Label(label="File: {}".format(filename))
        hbox_filename.pack_start(filenameLabel, True, True, 2)
        top_vbox.pack_start(hbox_filename, True, True, 2)
        # Now the horizontal boxes that will contain the actual elements the user interacts with
        # Speaker
        hbox_speaker = Gtk.Box(spacing=6)
        speakerLabel = Gtk.Label(label="Speaker:")
        # self.speakerEntry because we'll need to access this object later when its callback is executed
        self.speakerEntry = Gtk.Entry()
        hbox_speaker.pack_start(speakerLabel, True, False, 10)
        hbox_speaker.pack_start(self.speakerEntry, True, False, 10)
        
        # Title
        hbox_title = Gtk.Box(spacing=6)
        titleLabel = Gtk.Label(label="Title:")
        self.titleEntry = Gtk.Entry()
        # 10 pixel padding
        hbox_title.pack_start(titleLabel, True, True, 10)
        hbox_title.pack_start(self.titleEntry, True, True, 10)
        
        # Pack all the horizontal boxes into the parent vertical box
        top_vbox.pack_start(hbox_speaker, True, True, 10)
        top_vbox.pack_start(hbox_title, True, True, 10)

        # Calendar
        top_vbox.pack_start(Gtk.Label(label="Date:"), True, True, 10)
        calendar = Gtk.Calendar()
        top_vbox.pack_start(calendar, False, False, 10)
        self.date = calendar.get_date() # Defaults to today
        
        # Calendar signals (double click)
        calendar.connect("day-selected-double-click", self.calendarDaySelected)

        # Submit button
        submitButton = Gtk.Button(label="Submit")
        top_vbox.pack_start(submitButton, False, False, 10)
        # Event
        submitButton.connect("clicked", self.submitFile)

    # SIGNAL CALLBACKS
    def calendarDaySelected(self, widget):
        """ 
        Called when the user selects a date on the calendar widget
        with a double click.
        """
        # Set the date
        self.date = widget.get_date()

    def submitFile(self, widget):
        """ Submit button clicked """
        title = self.titleEntry.get_text()
        speaker = self.speakerEntry.get_text()
        print(self.date)
        print(speaker)
        print(title)
        success = id3tag.writeTag(self.filename, speaker, title, self.date)
        if success:
            # TODO: Message dialog to inform the user of success
            print("Updated ID3 tag.")
            self.destroy() # Close the window
        else:
            print("ID3 tag update failed.")

def mainloop(filename=""):
    # Show the ID3 tag editing window for <filename>
    win = ID3Window(filename)
    win.connect("destroy", Gtk.main_quit)
    win.show_all()
    Gtk.main()

# For testing purposes
if __name__ == "__main__":
    mainloop()

import gi
gi.require_version("Gtk", "3.0")

from gi.repository import Gtk, Gdk

import id3tag # Our ID3 tag writer
import config

# Gtk.Box pack_start() / pack_end() docs:
# pack_start (child, expand, fill, padding)

class CalendarWindow(Gtk.Window):
    def __init__(self):
        super().__init__(title="Date")
        self.set_position(Gtk.WindowPosition.CENTER)

        self.calendar = Gtk.Calendar()
        # Show the calendar
        self.add(self.calendar)
        
        # Hook a double click event
        self.calendar.connect("day-selected-double-click", self.daySelected)

    def daySelected(self, widget):
        print(widget.get_date())

class ID3Window(Gtk.Window):
    def __init__(self, filename):
        # Call the super class's init method, set the window title
        super().__init__(title="Edit ID3 tag")
        # Set the window's position to the center of the screen
        self.set_position(Gtk.WindowPosition.CENTER)
        self.filename = filename

        # Our top level vertical box, which will contain
        # multiple horizontal boxes,
        # which will themselves then contain the text labels and editable entries.
        top_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(top_vbox)
        
        # First, just a label of the filename
        hbox_filename = Gtk.Box(spacing=6)
        filenameLabel = Gtk.Label(label="File: {}".format(filename))
        hbox_filename.pack_start(filenameLabel, True, True, 2)
        top_vbox.pack_start(hbox_filename, True, True, 2)
        # Now the horizontal boxes that will contain the actual elements the user interacts with
        # Speaker
        hbox_speaker = Gtk.Box(spacing=6)
        speakerLabel = Gtk.Label(label="Speaker:")
        # self.speakerEntry because we'll need to access this object later when its callback is executed
        self.speakerEntry = Gtk.Entry()
        hbox_speaker.pack_start(speakerLabel, True, False, 10)
        hbox_speaker.pack_start(self.speakerEntry, True, False, 10)
        
        # Title
        hbox_title = Gtk.Box(spacing=6)
        titleLabel = Gtk.Label(label="Title:")
        self.titleEntry = Gtk.Entry()
        # 10 pixel padding
        hbox_title.pack_start(titleLabel, True, True, 10)
        hbox_title.pack_start(self.titleEntry, True, True, 10)
        
        # Pack all the horizontal boxes into the parent vertical box
        top_vbox.pack_start(hbox_speaker, True, True, 10)
        top_vbox.pack_start(hbox_title, True, True, 10)

        # Calendar
        top_vbox.pack_start(Gtk.Label(label="Date:"), True, True, 10)
        calendar = Gtk.Calendar()
        top_vbox.pack_start(calendar, False, False, 10)
        self.date = calendar.get_date() # Defaults to today
        
        # Calendar signals (double click)
        calendar.connect("day-selected-double-click", self.calendarDaySelected)

        # Submit button
        submitButton = Gtk.Button(label="Submit")
        top_vbox.pack_start(submitButton, False, False, 10)
        # Event
        submitButton.connect("clicked", self.submitFile)

    # SIGNAL CALLBACKS
    def calendarDaySelected(self, widget):
        """ 
        Called when the user selects a date on the calendar widget
        with a double click.
        """
        # Set the date
        self.date = widget.get_date()

    def submitFile(self, widget):
        """ Submit button clicked """
        title = self.titleEntry.get_text()
        speaker = self.speakerEntry.get_text()
        print(self.date)
        print(speaker)
        print(title)
        success = id3tag.writeTag(self.filename, speaker, title, self.date)
        if success:
            # TODO: Message dialog to inform the user of success
            print("Updated ID3 tag.")
            self.destroy() # Close the window
        else:
            print("ID3 tag update failed.")

def mainloop(filename=""):
    # Show the ID3 tag editing window for <filename>
    win = ID3Window(filename)
    win.connect("destroy", Gtk.main_quit)
    win.show_all()
    Gtk.main()

# For testing purposes
if __name__ == "__main__":
    mainloop()

# Our ID3 tag writer # Gtk.Box pack_start() / pack_end() docs: # pack_start (child, expand, fill, padding) # Show the calendar # Hook a double click event # Call the super class's init method, set the window title # Set the window's position to the center of the screen # Our top level vertical box, which will contain # multiple horizontal boxes, # which will themselves then contain the text labels and editable entries. # First, just a label of the filename # Now the horizontal boxes that will contain the actual elements the user interacts with # Speaker # self.speakerEntry because we'll need to access this object later when its callback is executed # Title # 10 pixel padding # Pack all the horizontal boxes into the parent vertical box # Calendar # Defaults to today # Calendar signals (double click) # Submit button # Event # SIGNAL CALLBACKS Called when the user selects a date on the calendar widget         with a double click. # Set the date Submit button clicked # TODO: Message dialog to inform the user of success # Close the window # Show the ID3 tag editing window for <filename> # For testing purposes