import es
import playerlib
import votelib

#plugin information
info = es.AddonInfo()
info.name = "Vote EventScripts python library"
info.version = "oy4"
info.author = "Hunter"
info.url = "http://www.eventscripts.com/pages/Vote/"
info.description = "Provides vote handling for Source games"
info.basename = "vote"


###########################################
#EventScripts events and blocks start here#
###########################################

def load():
    if not es.exists("command", "vote"):
        es.regcmd("vote", "vote/consolecmd", "Vote console command")

#vote create <votename> <callback-blockname> [vote-callback-blockname]
#vote delete <votename>
#vote exists <var> <votename>
#vote isrunning <var> <votename>
#vote setquestion <votename> "<question>"
#vote setanswer <votename> <option#> "<answer>"
#vote addanswer <votename> "<answer>"
#vote remanswer <votename> <option#>
#vote start <votename> [time]
#vote stop <votename> [cancel]
#vote send <votename> <userid> [force]
#vote submit <votename> <userid> <option#> [force]
#vote getvotes <var> <votename> [answer]
#vote gettimeleft <var> <votename>
#vote showmenu <votename> <0/1>
#vote endtime <votename> <0/1>
#vote enduser <votename> <0/1>
#vote setvar <votename> <variable> <value>
#vote getvar <votename> <var> <variable>
#vote list

def consolecmd():
    #Command from server console or non-python script
    subcmd = es.getargv(1).lower()
    vname = es.getargv(2)
    argc = es.getargc()
    if vname in votelib.gVotes:
        v = votelib.gVotes[vname]
    else:
        v = None
    if subcmd == "create":
        if vname:
            endblock = es.getargv(3)
            subblock = es.getargv(4)
            if endblock == "0": endblock = ""
            if subblock == "0": subblock = ""
            votelib.create(vname,endblock,subblock)
        else:
            es.dbgmsg(0,"Syntax: vote create <votename> <end-callback> [submit-callback]")
    elif subcmd == "delete":
        if vname:
            if v:
                votelib.delete(vname)
            else:
                es.dbgmsg(0,"Popup delete: No such popup: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote delete <votename>")
    elif subcmd == "exists":
        if argc == 4:
            retvar = es.getargv(2)
            vname = es.getargv(3)
            es.set(retvar,int(votelib.exists(vname)))
        else:
            es.dbgmsg(0,"Syntax: vote exists <var> <votename>")
    elif subcmd == "isrunning":
        if argc == 4:
            retvar = es.getargv(2)
            vname = es.getargv(3)
            es.set(retvar,int(votelib.isrunning(vname)))
        else:
            es.dbgmsg(0,"Syntax: vote isrunning <var> <votename>")
    elif subcmd == "getvotes":
        if argc == 4:
            retvar = es.getargv(2)
            vname = es.getargv(3)
            if argc == 5:
                option = int(es.getargv(4))
            else:
                option = None
            es.set(retvar,int(votelib.getvotes(vname,option)))
        else:
            es.dbgmsg(0,"Syntax: vote getvotes <var> <votename> [option#]")
    elif subcmd == "gettimeleft":
        if argc == 4:
            retvar = es.getargv(2)
            vname = es.getargv(3)
            es.set(retvar,int(votelib.gettimeleft(vname)))
        else:
            es.dbgmsg(0,"Syntax: vote gettimeleft <var> <votename>")
    elif subcmd == "setquestion":
        if argc == 4:
            if v:
                question = str(es.getargv(3))
                v.setquestion(question)
            else:
                es.dbgmsg(0,"Vote setquestion: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote setoption <votename> <option#> \"<answer>\"")
    elif (subcmd == "setoption") or (subcmd == "setanswer"):
        if argc == 5:
            if v:
                option = int(es.getargv(3))
                text = str(es.getargv(4))
                v.setoption(option,text)
            else:
                es.dbgmsg(0,"Vote setoption: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote setoption <votename> <option#> \"<answer>\"")
    elif (subcmd == "addoption") or (subcmd == "addanswer"):
        if argc == 4:
            if v:
                text = str(es.getargv(3))
                v.addoption(text)
            else:
                es.dbgmsg(0,"Vote addoption: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote addoption <votename> \"<answer>\"")
    elif (subcmd == "deloption") or (subcmd == "remanswer"):
        if argc == 4:
            if v:
                option = int(es.getargv(3))
                v.deloption(option)
            else:
                es.dbgmsg(0,"Vote deloption: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote deloption <votename> <option#>")
    elif subcmd == "start":
        if argc >= 3:
            if v:
                if argc == 4:
                    time = int(es.getargv(3))
                else:
                    time = 0
                v.start(time)
            else:
                es.dbgmsg(0,"Vote start: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote start <votename> [time]")
    elif subcmd == "stop":
        if argc == 5:
            try:
                cancel = bool(int(es.getargv(4)))
            except ValueError:
                cancel = False
        else:
            cancel = False
        if argc >= 4:
            if v:
                v.start(cancel)
            else:
                es.dbgmsg(0,"Vote stop: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote stop <votename> [cancel]")
    elif subcmd == "send":
        if argc == 5:
            try:
                force = bool(int(es.getargv(4)))
            except ValueError:
                force = False
        else:
            force = False
        if argc >= 4:
            if v:
                users = es.getargv(3)
                v.send(playerlib.getUseridList(users),force,False)
            else:
                es.dbgmsg(0,"Vote send: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote send <votename> <users> [force]")
    elif subcmd == "submit":
        if argc == 6:
            try:
                force = bool(int(es.getargv(5)))
            except ValueError:
                force = False
        else:
            force = False
        if argc >= 5:
            if v:
                users = es.getargv(3)
                option = int(es.getargv(4))
                v.submit(playerlib.getUseridList(users),option,force)
            else:
                es.dbgmsg(0,"Vote submit: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote submit <votename> <users> <option#> [force]")
    elif subcmd == "showmenu":
        if argc == 4:
            if v:
                try:
                    v.showmenu = bool(int(es.getargv(3)))
                except ValueError:
                    v.showmenu = False
            else:
                es.dbgmsg(0,"Vote showmenu: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote showmenu <votename> <0/1>")
    elif subcmd == "endtime":
        if argc == 4:
            if v:
                try:
                    v.endtime = bool(int(es.getargv(3)))
                except ValueError:
                    v.endtime = False
            else:
                es.dbgmsg(0,"Vote endtime: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote endtime <votename> <0/1>")
    elif subcmd == "enduser":
        if argc == 4:
            if v:
                try:
                    v.enduser = bool(int(es.getargv(3)))
                except ValueError:
                    v.enduser = False
            else:
                es.dbgmsg(0,"Vote enduser: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote enduser <votename> <0/1>")
    elif subcmd == "setvar":
        if argc == 5:
            if v:
                varp = es.getargv(3)
                varv = es.getargv(4)
                if varp.isalnum():
                    exec "v.%s='%s'" % (varp, varv)
                else:
                    es.dbgmsg(0,"Invalid vote variable name")
            else:
                es.dbgmsg(0,"Vote setvar: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote setvar <votename> <variable> <value>")
    elif subcmd == "getvar":
        if argc == 5:
            if v:
                varp = es.getargv(3)
                varv = es.getargv(4)
                if varv.isalnum():
                    es.set(varp, eval("v."+varv))
                else:
                    es.dbgmsg(0,"Invalid vote variable name")
            else:
                es.dbgmsg(0,"Vote getvar: No such vote: "+vname)
        else:
            es.dbgmsg(0,"Syntax: vote getvar <votename> <var> <variable>")
    elif subcmd == "list":
        es.dbgmsg(0,"---------- List of votes:")
        if argc == 2:
            listlevel = 0
        else:
            listlevel = int(vname)
        for vname in votelib.gVotes:
            v = votelib.gVotes[vname]
            v.information(listlevel)
        if argc == 2:
            es.dbgmsg(0, " ")
            es.dbgmsg(0, "For more details, supply list level (0-2):")
            es.dbgmsg(0, "Syntax: vote list [level]")
        es.dbgmsg(0,"----------")
    elif subcmd == "info":
        if argc >= 3:
            if argc == 4:
                listlevel = int(es.getargv(3))
            else:
                listlevel = 2
            if v:
                v.information(listlevel)
        else:
            es.dbgmsg(0, "Syntax: vote info <votename> [level]")
    else:
        es.dbgmsg(0,"Invalid vote subcommand, see http://www.eventscripts.com/pages/Vote/ for help")
