def delthread(n):
    uid = getUID()

    if request.method == "POST":
        tid = int(request.form["tid"])
        thread = c.threads.Collections.find_one({"id":tid})
        
        if thread:
            if admin(uid):
                utils.delthread(tid)
                if "ajax" in request.form:
                    return "/forum-%d?type=1"%(thread['fid'])
                else:
                    return redirect("/forum-%d?type=1"%(thread['fid']))
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.threadDoesNotExist(),uid)
    else:
        r = ""
    
        thread = c.threads.Collections.find_one({"id":int(n)})
        if thread:
            if admin(uid):
                r += html.delthread(thread["id"])
                return page(r,uid)
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.threadDoesNotExist(),uid)
def delpost(n):
    uid = getUID()

    if request.method == "POST":
        pid = int(request.form["pid"])
        post = c.posts.Collections.find_one({"id":pid})
        
        if post:
            if admin(uid) and getThread(post['tid'])['pid'] != post['id']:
                utils.delpost(pid)
                if "ajax" in request.form:
                    return "/thread-%d?type=4"%(post['tid'])
                else:
                    return redirect("/thread-%d?type=51"%(post['tid']))
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.postDoesNotExist(),uid)
    else:
        r = ""
    
        post = c.posts.Collections.find_one({"id":int(n)})
        if post:
            if admin(uid) and getThread(post['tid'])['pid'] != post['id']:
                r += html.delpost(post["id"])
                return page(r,uid)
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.postDoesNotExist(),uid)
def edittitle(n):
    uid = getUID()

    if request.method == "POST":
        tid = int(request.form["tid"])
        thread = c.threads.Collections.find_one({"id":tid})
        
        if thread:
            if admin(uid):
                utils.edittitle(tid, request.form["title"], request.form["desc"])
                return redirect("/thread-%d?type=1"%(tid))
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.threadDoesNotExist(),uid)
    else:
        r = ""
    
        thread = c.threads.Collections.find_one({"id":int(n)})
        if thread:
            if admin(uid):
                r += html.edittitle(thread["id"])
                return page(r,uid)
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.threadDoesNotExist(),uid)
def editpost(n):
    uid = getUID()

    if request.method == "POST":
        pid = int(request.form["pid"])
        post = c.posts.Collections.find_one({"id":pid})
        
        if post:
            if (uid == post["uid"] and not post['hid'] and not getThread(post['tid'])['hid']) or admin(uid):
                utils.editPost(uid, pid, request.form["content"])
                return redirect("/thread-%d?type=52"%(getThread(post["tid"])['id']))
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.postDoesNotExist(),uid)
    else:
        r = ""
    
        post = c.posts.Collections.find_one({"id":int(n)})
        if post:
            if (uid == post["uid"] and not post['hid'] and not getThread(post['tid'])['hid']) or admin(uid):
                r += html.editpost(post["id"])
                return page(r,uid)
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.postDoesNotExist(),uid)
def reply(n):
    uid = getUID()

    if uid == -1:
        return page(html.permissionDenied(),-1)
    if request.method == "POST":
        tid = int(request.form["tid"])
        thread = c.threads.Collections.find_one({"id":tid})
        
        if thread:
            if (not thread['hid'] and not thread['lock']) or admin(uid):
                utils.createPost(uid, tid, request.form["content"])
                return redirect("/thread-%d"%(tid))
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.threadDoesNotExist(),uid)
    else:
        r = ""
    
        thread = c.threads.Collections.find_one({"id":int(n)})
        if thread:
            if (not thread['hid'] and not thread['lock']) or admin(uid):
                r += html.reply(thread["id"])
                return page(r,uid)
            else:
                return page(html.permissionDenied(),uid)
        else:
            return page(html.threadDoesNotExist(),uid)
def unlockthread(n):
    uid = getUID()

    thread = c.threads.Collections.find_one({"id":int(n)})
    if thread:
        if admin(uid):
            utils.unlockthread(thread['id'])
            return redirect("thread-%d?type=5"%(thread['id']))
        else:
            return page(html.permissionDenied(),uid)
    else:
        return page(html.threadDoesNotExist(),uid)
def unhidepost(n):
    uid = getUID()

    post = c.posts.Collections.find_one({"id":int(n)})
    if post:
        if admin(uid):
            utils.unhidepost(post['id'])
            return redirect("thread-%d"%(post['tid']))
        else:
            return page(html.permissionDenied(),uid)
    else:
        return page(html.postDoesNotExist(),uid)
def newthread(n):
    uid = getUID()

    if uid == -1:
        return page(html.permissionDenied(),-1)

    if request.method == "POST":
        fid = int(request.form["fid"])
        forum = c.forums.Collections.find_one({"id":fid})
        
        if forum:
            tid = utils.createThread(uid, fid, request.form["title"], request.form["desc"], request.form["content"])
            return redirect("/thread-%d"%(tid))
        else:
            return page(html.forumDoesNotExist(),uid)
    else:
        r = ""
    
        forum = c.forums.Collections.find_one({"id":int(n)})
        if forum:
            r += html.newthread(forum["id"])
            return page(r,uid)
        else:
            return page(html.forumDoesNotExist(),uid)
def thread(n):
    uid = getUID()


    r = ""

    thread = getThread(n)
    
    if thread:
        # if thread is hidden and cannot view
        if thread['hid'] and not admin(uid):
            return page(html.permissionDenied(),uid)

        posts = c.posts.Collections.find({"tid":thread['id']}).sort("id",1)
        # pages
        pg = 1
        p = request.args.get("page")
        if p:
            pg = int(p)

        pghtml = html.pages(pg,int((posts.count()-1)/config.postsPerPage())+1,"thread-%d?page="%(thread['id']))

        forum = getForum(thread["fid"])
        nav = utils.nav([
                ["/",fn],
                ["/forum-%d"%(forum['id']),forum["name"]],
                [thread["title"]]
                ])


        atools = ""



        # admin tools
        if admin(uid):
            atools += """
<table class="table">
<tr class="warning">
<td style="width:150px;text-align:center;font-weight:bold;">Administrative<br />Tools</td>
<td style="vertical-align:middle;">
<a href="edittitle-%d" class="btn btn-warning"><span class="glyphicon glyphicon-pencil"></span> Edit Thread Title</a>"""%(thread['id'])

            # lock/unlock
            if "lock" in thread.keys() and thread['lock']:
                atools += ' <a href="unlockthread-%d" class="btn btn-success"><span class="glyphicon glyphicon-lock"></span> Unlock Thread</a>'%(thread['id'])
            else:
                atools += ' <a href="lockthread-%d" class="btn btn-danger"><span class="glyphicon glyphicon-lock"></span> Lock Thread</a>'%(thread['id'])

            # hide/unhide
            if "hid" in thread.keys() and thread['hid']:
                atools += ' <a href="unhidethread-%d" class="btn btn-success"><span class="glyphicon glyphicon-eye-open"></span> Unhide Thread</a>'%(thread['id'])
            else:
                atools += ' <a href="hidethread-%d" class="btn btn-danger"><span class="glyphicon glyphicon-eye-close"></span> Hide Thread</a>'%(thread['id'])
            atools += """
<a href="delthread-%d" class="btn btn-danger deleteThread"><span class="glyphicon glyphicon-remove"></span> Delete Thread</a>
</td>
</tr>
</table>"""%(thread['id'])

        options = ""
        if uid != -1 and (not thread['lock'] or admin(uid)):
            options = '<div class="options"><a href="reply-%d" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Reply</a></div>'%(thread['id'])


        r += '<div id="header">%(title)s</div><div class="description">%(desc)s</div>'%(thread)

        r += '<div class="thread_info">'

        if thread['lock']:
            r += '<span class="alert alert-danger"><span class="glyphicon glyphicon-lock"></span> Locked</span>'
        if thread['hid']:
            r += '<span class="alert alert-danger"><span class="glyphicon glyphicon-eye-close"></span> Hidden</span>'
        
        r += '</div>'

        mes = request.args.get("type")
        if mes == '1':
            mes = "Thread title edited"
        elif mes == '2':
            mes = "Thread hidden"
        elif mes == '3':
            mes = "Thread unhidden"
        elif mes == '4':
            mes = "Thread locked"
        elif mes == '5':
            mes = "Thread unlocked"
        elif mes == '51':
            mes = "Post deleted"
        elif mes == '52':
            mes = "Post edited"



        r += nav
        if mes:
            r += '<div class="alert alert-success"><strong>Success:</strong> %s</div>'%(mes)
        r += atools
        r += pghtml
        r += options
        r += '<div id="posts">'


        ppp = config.postsPerPage()
        for x in range((pg-1)*ppp,min(pg*ppp,posts.count())):
            r += html.post(uid,posts[x])

        r += '</div>'
        r += options
        r += pghtml
        r += nav

        return page(r,uid)
    else:
        return page(html.threadDoesNotExist(),uid)