from browser import document, window, ajax, timer, alert
from browser.html import P, A, SPAN, INPUT, BR, IMG

def returnpostlist(req):
    document["commentlist2"].innerHTML = ""
    if req.status==200 or req.status==0:
        if req.text.strip() == "": return
        postliststring, nameliststring, timeliststring = req.text.strip().split(chr(30))
        postlist = postliststring.strip().split(chr(31))
        namelist = nameliststring.strip().split(chr(31))
        timelist = timeliststring.strip().split(chr(31))
        commentstring = ""
        count = 0
        for (post, name, posttime) in zip(postlist, namelist, timelist):
            count += 1
            commentstring += '<div class="comment"><h4>'+name+' on '+posttime+'</h4>\n'
            commentstring += '<p>'+post+'</p></div>\n'
        document["commentlist2"].innerHTML = commentstring

def on_timeout():
    pass
    #document["commentlist"].innerHTML = ""

def savepost(event):
    filename = "Any"
    username = document["username"].value
    postText = document["postText"].value
    commentstring = '<div class="comment"><h4>'+username+' a few seconds ago</h4>\\n<p>'+postText+'</p></div>\\n'
    commentstring += document["commentlist2"].innerHTML
    document["commentlist2"].innerHTML = commentstring

    req = ajax.ajax()
    req.bind('complete',returnpostlist)
    req.set_timeout(10,on_timeout)
    req.open('POST',"savepost.cgi",True)
    req.set_header('content-type','application/x-www-form-urlencoded')
    req.send({'filename':filename, 'username':username, 'postText':postText, "showall":str(showall)})

def getpostlist():
    filename = "Any"

    req = ajax.ajax()
    req.bind('complete',returnpostlist)
    req.set_timeout(10,on_timeout)
    req.open('POST',"getpostlist.cgi",True)
    req.set_header('content-type','application/x-www-form-urlencoded')
    req.send({'filename':filename, "showall":str(showall)})

def showallposts(event):
    global showall
    if showall:
        showall = False
        document["showallposts"].value = "Show older comments"
    else:
        showall = True
        document["showallposts"].value = "Hide older comments"
    getpostlist()

def leavefullscreen(event):
    print("Leaving full screen")
    if document.fullscreenElement: document.exitFullscreen()

def showoverlay(event):
    video = document["videoplayer"]
    if video.currentTime > video.duration - 10:
        document["overlay"].style.visibility = "visible"
    else:
        document["overlay"].style.visibility = "hidden"

if window.innerWidth < window.innerHeight:
    document["videoplayer"].style.width = "90%"
else:
    h = window.innerHeight-document['header'].offsetHeight-30
    document["videoplayer"].style.height = f"{h}px"

document["overlay"].left = int(document["videoplayer"].offsetLeft) + 20
document["overlay"].top = int(document["videoplayer"].offsetTop) + 20

showall = False
getpostlist()
timer.set_interval(getpostlist, 60000)

document["sendpost"].bind("click", savepost)
document["showallposts"].bind("click", showallposts)
#document["videoplayer"].bind("ended", leavefullscreen)
#document["videoplayer"].bind("timeupdate", showoverlay)
