import display, buttons, sndmixer, orientation, listbox, os, system, time



def drawMessageBox(text):
    oneFourthWidth = display.width()//4
    width = display.getTextWidth(text, "org01_8")
    height = display.getTextHeight(text, "org01_8")
    display.drawRect((oneFourthWidth*2) - ((width-4)//2), (display.height()-height-4)//2, width+2, height+2, True, 0xFFFF00)
    display.drawText((oneFourthWidth*2) - ((width-4)//2), (display.height()-height)//2-2, text, 0x000000, "org01_8")

display.drawFill(0x000000)
drawMessageBox("Starting audio driver...")
display.flush()
sndmixer.begin(1, True)
mp3files = []

def scanDir(directory):
    try:
        display.drawFill(0x000000)
        drawMessageBox("Loading file list...\nScanning "+directory+"...")
        display.flush()
        files = os.listdir(directory)
        for name in files:
            if name.endswith('.mp3'):
                mp3files.append(directory+'/'+name)
            else:
                scanDir(directory+'/'+name)
    except:
        pass

scanDir('/sd')
scanDir('/music')
display.drawFill(0x000000)
drawMessageBox("Welcome!")
display.flush()

fileList = listbox.List(0, 16, display.width(), display.height()-16)
fileList.fgColor = 0xFFFF00
fileList.bgColor = 0x000000
for name in mp3files:
    fileList.add_item(name)

currentFile = 0
playerId = -1
playerFd = None

def onUp(pressed):
    global currentFile, mp3files, fileList
    if pressed:
        currentFile -= 1
        if currentFile < 0:
            currentFile = len(mp3files)-1
        if fileList:
            fileList.moveUp()
        draw()

def onDown(pressed):
    global currentFile, mp3files, fileList
    if pressed:
        currentFile += 1
        if currentFile >= len(mp3files):
            currentFile = 0
        if fileList:
            fileList.moveDown()
        draw()

def onA(pressed):
    global currentFile, mp3files, playerFd, playerId
    if pressed:
        if playerId >= 0:
            sndmixer.stop(playerId)
            playerId = -1
        if playerFd:
            playerFd.close()
            playerFd = None
        playerFd = open(mp3files[currentFile], "rb")
        playerId = sndmixer.mp3_stream(playerFd)
        sndmixer.volume(playerId, 255)
        drawMessageBox("Playing...\n"+mp3files[currentFile])
        display.flush()
        time.sleep(0.5)
        draw()

def onB(pressed):
    global playerFd, playerId
    if pressed:
        if playerId >= 0:
            sndmixer.stop(playerId)
            playerId = -1
        if playerFd:
            playerFd.close()
            playerFd = None
    drawMessageBox("Stopped")
    display.flush()
    time.sleep(0.5)
    draw()

def onHome(pressed):
    if pressed:
        system.launcher()
        
def draw():
    global fileList
    display.drawFill(0x000000)
    display.drawText(0,0, "MP3 player", 0xFFFF00, "org01_8")
    for i in range(8):
        display.drawLine(0,10+i,display.width(),10+i,((i*32)<<16) + ((i*32)<<8))
    fileList.draw()
    display.flush()

buttons.attach(buttons.BTN_UP, onUp)
buttons.attach(buttons.BTN_DOWN, onDown)
buttons.attach(buttons.BTN_A, onA)
buttons.attach(buttons.BTN_B, onB)
try:
    buttons.attach(buttons.BTN_HOME, onHome)
except:
    try:
        buttons.attach(buttons.BTN_START, onHome)
    except:
        buttons.attach(buttons.BTN_B, onHome)

draw()