# Bryan Schumaker (11/25/2010)

import ocarina
gtk      = ocarina.gtk
pango    = ocarina.pango
libsaria = ocarina.libsaria

from components import label
from components import button
from components import pbar
from components import image

footer  = gtk.VBox()
bar     = gtk.HBox()
title   = gtk.Label()
tabs    = gtk.Notebook()
act_bar = gtk.HBox()

pages = dict()

def change_title(filepath):
	id = libsaria.path.file_id(filepath)
	(ttl, artst) = libsaria.sources.library.get_attrs(id, "title", "artist")
	title.set_text("%s by %s" % (ttl, artst))

def bar_add(widget, expand = False, fill = False):
	bar.pack_start(widget, expand, fill)

def act_bar_add(widget, expand = False, fill = False):
	act_bar.pack_start(widget, expand, fill)

def add_page(title, widget):
	label = gtk.Label(title)
	pages[title] = widget
	tabs.append_page(widget, label)

def remove_page(title):
	page = pages.get(title, None)
	if page == None:
		return
	n = tabs.page_num(page)
	tabs.remove_page(n)
	del pages[title]

def up_button():
	tabs.show()
	bar.hide()
	libsaria.prefs.set_pref("ocarina.footer.up", True)

def down_button():
	tabs.hide()
	bar.show()
	libsaria.prefs.set_pref("ocarina.footer.up", False)

def make_bar():
	title.set_text(ocarina.__vers__)
	title.set_ellipsize(pango.ELLIPSIZE_END)
	title.show()

	bar_add(image.AlbumArt(24))
	bar_add(title, True, True)
	bar_add(label.TimeLabel())
	bar_add(label.LengthLabel2())
	bar_add(button.RewindButton())
	bar_add(button.ForwardButton())
	bar_add(button.PlayButton())
	bar_add(button.PauseButton())
	bar_add(button.StopButton())
	bar_add(button.NextButton())
	bar_add(button.UpButton(up_button))

	bar.show()
	footer.pack_start(bar)

def make_action_bar():
	act_bar_add(label.TimeLabel())
	act_bar_add(pbar.PBar(), True, True)
	act_bar_add(button.PlayButton())
	act_bar_add(button.PauseButton())
	act_bar_add(button.StopButton())
	act_bar_add(button.NextButton())
	act_bar_add(button.DownButton(down_button))
	act_bar.show()

def make_now_playing_page():
	page = gtk.HBox(False, 5)
	page.pack_start(image.AlbumArt(), False, False)

	tags = gtk.ScrolledWindow()
	tags.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
	view = gtk.Viewport()
	view.set_shadow_type(gtk.SHADOW_NONE)
	tag_box = gtk.VBox(True)
	tag_box.pack_start(label.TitleLabel())
	tag_box.pack_start(label.ArtistLabel())
	tag_box.pack_start(label.AlbumLabel())
	view.add(tag_box)
	tags.add(view)
	page.pack_start(tags)

	attrs = gtk.HBox(False, 5)
	tags2 = gtk.VBox()
	tags2.pack_start(label.YearLabel())
	tags2.pack_start(label.LengthLabel())
	tags2.pack_start(label.CountLabel())
	attrs.pack_start(tags2)

	buttons = gtk.VBox()
	buttons.pack_start(button.LikeButton(), False, False)
	buttons.pack_start(button.DislikeButton(), False, False)
	attrs.pack_start(buttons, False)
	page.pack_start(attrs)

	page.show_all()
	add_page("Now Playing", page)


def make_tabs():
	make_action_bar()
	tabs.set_action_widget(act_bar, gtk.PACK_END)
	make_now_playing_page()
	tabs.show_all()
	footer.pack_start(tabs)

def init():
	sep = gtk.HSeparator()
	footer.pack_start(sep)
	footer.show_all()
	make_bar()
	make_tabs()

	up = libsaria.init_pref("ocarina.footer.up", False)
	if up == True:
		up_button()
	else:
		down_button()
	libsaria.event.invite("POSTLOAD", change_title)
init()
