#!/usr/bin/python3

import cgi
import json
import sqlite3 as lite

class GetPlaylistSongsForPopup():
	def __init__(self):
		self.pll_db_loc = "/usr/share/ampyche/db/ampychePlaylist.db"
		
	def _make_a_list(self, genlist):
		agen = []
		for gen in genlist:
			gen = ' '.join(gen)
			agen.append(gen)
		agen.sort()
		return agen
		
	def _format_input(self, a_list):
		count = 0
		meeco = ""
		for a in a_list:
			count += 1
			strcount = str(count)
			b = "radio" + strcount
			c = "<input type='radio' id='%s'" %b
			d = " name='%s'" %b
			e = " value='off'><label for='%s'" %b  
			f = ">%s</label>" %a 
			g = c + d + e + f
			meeco = meeco + g 
		return meeco

	def _get_songs_for_playlist_popup(self, plname):
		con = lite.connect(self.pll_db_loc)
		cur = con.cursor()
		cur.execute("SELECT song FROM playlistsongs WHERE playlistname=?", [plname])
		plsongs = cur.fetchall()
		con.close()
		plpsongs = self._make_a_list(plsongs)
		return plpsongs

pl = cgi.FieldStorage()
pllname = pl.getvalue("playlistname")
taz = GetPlaylistSongsForPopup()
pluto = taz._get_songs_for_playlist_popup(pllname)
print("Content-Type: application/json\n\n")
print(json.dumps(pluto, sort_keys=True, indent=4))