import es
import playerlib
import keymenulib

#plugin information
info = es.AddonInfo()
info.name = "Keymenu EventScripts python library"
info.version = "oy5"
info.author = "Hunter"
info.url = "http://www.eventscripts.com/pages/Keymenu/"
info.description = "Provides menu and pop-up creation from keygroups"
info.basename = "keymenu"


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

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

#keymenu create <menu name> <return var> <script/block> <keygroup> <menu display> <menu return> Menu Title Text
#keymenu delete <menu name>
#keymenu send <menu name> <userid> [page]
#keymenu unsend <menu name> <users> [page]
#keymenu getpages <var> <menu name>
#keymenu exists <var> <menu name>
#keymenu changeblock <menu name> <script/block>
#keymenu setvar <menu name> <menu var> <menu value>
#keymenu getvar <menu name> <var> <menu var>
#keymenu backmenu <menu name> <keymenu/popup>
#keymenu getmenuname <var> <popup-name>
#keymenu update <menu name> [keygroup]

def consolecmd():
    #Command from server console or non-python script
    es.dbgmsg(1, "Keymenu.py: consolecmd: "+str(es.getargs()))
    subcmd = es.getargv(1).lower()
    kname = es.getargv(2)
    argc = es.getargc()
    if kname in keymenulib.gKeymenus:
        k = keymenulib.gKeymenus[kname]
    else:
        k = None
    if subcmd == "create":
        if kname and argc > 7:
            returnvar = es.getargv(3)
            block = es.getargv(4)
            keygroup = es.getargv(5)
            menudisplay = es.getargv(6)
            if menudisplay == "#key":
                tmenudisplay = menudisplay
                menureturn = es.getargv(7)
                if menureturn == "#key":
                    tmenureturn = menureturn
                elif menureturn == "#keyvalue":
                    tmenureturn = menureturn + " " + es.getargv(8)
            elif menudisplay == "#keyvalue":
                tmenudisplay = menudisplay + " " + es.getargv(7)
                menureturn = es.getargv(8)
                if menureturn == "#key":
                    tmenureturn = menureturn
                elif menureturn == "#keyvalue":
                    tmenureturn = menureturn + " " + es.getargv(9)
            tempstring = subcmd + " " + kname + " " + returnvar + " " + block + " " + keygroup + " " + tmenudisplay + " " + tmenureturn + " "
            fullstring = es.getargs()
            titletext = fullstring[len(tempstring):]
            es.dbgmsg(1, "Tempstring: "+tempstring)
            es.dbgmsg(1, "Fullstring: "+fullstring)
            es.dbgmsg(1, "Title: "+titletext)
            k = keymenulib.create(kname, returnvar, block, keygroup, tmenudisplay, tmenureturn, titletext)
        else:
            es.dbgmsg(0,"Syntax: keymenu create <menu name> <return var> <script/block> <keygroup> <menu display> <menu return> Menu Title Text")
    elif subcmd == "delete":
        if kname and argc > 2:
            if k:
                keymenulib.delete(kname)
            else:
                es.dbgmsg(0,"No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu delete <menu name>")
    elif subcmd == "send":
        if kname and argc > 3:
            if k:
                users = es.getargv(3)
                page = es.getargv(4)
                if page:
                    k.send(playerlib.getUseridList(users), int(page))
                else:
                    k.send(playerlib.getUseridList(users))
            else:
                es.dbgmsg(0,"No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu send <menu name> <users> [page]")
    elif subcmd == "unsend":
        if kname and argc > 3:
            if k:
                users = es.getargv(3)
                page = es.getargv(4)
                if page:
                    k.unsend(playerlib.getUseridList(users), int(page))
                else:
                    k.unsend(playerlib.getUseridList(users))
            else:
                es.dbgmsg(0,"No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu unsend <menu name> <users> [page]")
    elif subcmd == "update":
        if kname and argc > 2:
            if k:
                keygroup = es.getargv(3)
                if keygroup:
                    k.update(keygroup)
                else:
                    k.update()
            else:
                es.dbgmsg(0,"No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu update <menu name> [keygroup]")
    elif subcmd == "exists":
        if argc > 3:
            kname = es.getargv(3)
            retvar = es.getargv(2)
            es.set(retvar,int(keymenulib.exists(kname)))
        else:
            es.dbgmsg(0,"Syntax: keymenu exists <var> <menu name>")
    elif subcmd == "getpages":
        if argc > 3:
            kname = es.getargv(3)
            if kname in keymenulib.gKeymenus:
                k = keymenulib.gKeymenus[kname]
                if k:
                    retvar = es.getargv(2)
                    es.set(retvar,k.pagecount)
                else:
                    es.dbgmsg(0,"No such keymenu: "+kname)
            else:
                es.dbgmsg(0,"No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu getpages <var> <menu name>")
    elif subcmd == "getmenuname":
        if argc > 3:
            pname = es.getargv(3)
            retvar = es.getargv(2)
            es.set(retvar,getmenuname(pname))
        else:
            es.dbgmsg(0,"Syntax: keymenu getmenuname <var> <popup-name>")
    elif subcmd == "changeblock":
        if argc > 3:
            if k:
                k.changeblock(es.getargv(3))
            else:
                es.dbgmsg(0,"No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu changeblock <menu name> <script/block>")
    elif subcmd == "backmenu":
        if argc > 3:
            if k:
                k.backmenu(es.getargv(3))
            else:
                es.dbgmsg(0,"No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu backmenu <menu name> <keymenu/popup>")
    elif subcmd == "setvar":
        if argc == 5:
            if k:
                varp = es.getargv(3)
                varv = es.getargv(4)
                if varp.isalnum():
                    exec "k.%s='%s'" % (varp, varv)
                else:
                    es.dbgmsg(0,"Invalid keymenu variable name")
            else:
                es.dbgmsg(0,"Keymenu setvar: No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu setvar <menu name> <variable> <value>")
    elif subcmd == "getvar":
        if argc == 5:
            if k:
                varp = es.getargv(3)
                varv = es.getargv(4)
                if varv.isalnum():
                    es.set(varp, eval("k."+varv))
                else:
                    es.dbgmsg(0,"Invalid keymenu variable name")
            else:
                es.dbgmsg(0,"Keymenu getvar: No such keymenu: "+kname)
        else:
            es.dbgmsg(0,"Syntax: keymenu getvar <menu name> <var> <variable>")
    elif subcmd == "list":
        if argc == 2:
            listlevel = 0
        else:
            listlevel = int(kname)
        for kname in keymenulib.gKeymenus:
            k = keymenulib.gKeymenus[kname]
            k.information(listlevel)
        if argc == 2:
            es.dbgmsg(0, " ")
            es.dbgmsg(0, "For more details, supply list level (0-1):")
            es.dbgmsg(0, "Syntax: keymenu list [level]")
    elif subcmd == "info":
        k.information(listlevel, 1)
        if argc == 3:
            es.dbgmsg(0, " ")
            es.dbgmsg(0, "Syntax: keymenu info <menu name>")
    else:
        es.dbgmsg(0,"Invalid keymenu subcommand, see http://www.eventscripts.com/pages/Keymenu/ for list")
