"""
VN Scene Script Ext - vnphy10
Provide functions to call VNPhysics function

ver     function            params                  desp
v1.0:   f_runaction         id[::inputParam]        run the action by id, with optional inputParam
        f_firetrigger       id[:force:inputParam]   fire trigger by id, force cooling down or not, with optional inputParam
        f_enablecollider    id:enable               enable/disable collider by id
        f_enabletrigger     id:enable               enable/disable trigger by id
        f_enableaction      id:enable               enable/disable action by id
        f_resetrigidbody    [id]                    reset rigidbody status by id or all of them without set id


"""

def custom_action(game,act):
    """:type game: vngameengine.VNNeoController"""

    from vnphysics import initialize
    vnphy = initialize(game)

    if act["action"] == "f_runaction":
        try:
            aid = act["actionparam"]
            if act.has_key("actionparam3") and len(act["actionparam3"]) > 0:
                inputParam = eval(act["actionparam3"])
                if not isinstance(inputParam, dict):
                    raise Exception("The third param in f_runaction must be a dict")
            else:
                inputParam = {}
            if aid in vnphy.actionIdDict:
                ci = {"caller": "vnss", "sceneIndex": 0}
                vnphy.runAction(aid, ci, inputParam)
            else:
                raise Exception("Action id '%s' not found"%aid)
            return True
        except Exception as e:
            print("vntext error, can't parse command '%s': %s"%(act["origintext"], str(e)))
            return True

    if act["action"] == "f_firetrigger":
        try:
            tid = act["actionparam"]
            if act.has_key("actionparam2") and len(act["actionparam2"]) > 0:
                force = bool(eval(act["actionparam2"]))
            else:
                force = False
            if act.has_key("actionparam3") and len(act["actionparam3"]) > 0:
                inputParam = eval(act["actionparam3"])
                if not isinstance(inputParam, dict):
                    raise Exception("The third param in f_runaction must be a dict")
            else:
                inputParam = {}
            if tid in vnphy.triggerIdDict:
                ci = {"caller": "vnss", "sceneIndex": 0}
                vnphy.fireTrigger(tid, ci, inputParam, force)
            else:
                raise Exception("Trigger id '%s' not found"%tid)
            return True
        except Exception as e:
            print("vntext error, can't parse command '%s': %s"%(act["origintext"], str(e)))
            return True

    if act["action"] == "f_enablecollider":
        try:
            sid = act["actionparam"]
            enable = bool(eval(act["actionparam2"]))
            if sid in vnphy.colliderIdDict:
                vnphy.colliderIdDict[sid].enable = enable
            else:
                raise Exception("Collider id '%s' not found"%sid)
            return True
        except Exception as e:
            print("vntext error, can't parse command '%s': %s"%(act["origintext"], str(e)))
            return True

    if act["action"] == "f_enabletrigger":
        try:
            sid = act["actionparam"]
            enable = bool(eval(act["actionparam2"]))
            if sid in vnphy.triggerIdDict:
                vnphy.triggerIdDict[sid].enable = enable
            else:
                raise Exception("Trigger id '%s' not found"%sid)
            return True
        except Exception as e:
            print("vntext error, can't parse command '%s': %s"%(act["origintext"], str(e)))
            return True

    if act["action"] == "f_enableaction":
        try:
            sid = act["actionparam"]
            enable = bool(eval(act["actionparam2"]))
            if sid in vnphy.actionIdDict:
                vnphy.actionIdDict[sid].enable = enable
            else:
                raise Exception("VNAction id '%s' not found"%sid)
            return True
        except Exception as e:
            print("vntext error, can't parse command '%s': %s"%(act["origintext"], str(e)))
            return True

    if act["action"] == "f_resetrigidbody":
        try:
            if act.has_key("actionparam") and len(act["actionparam"]) > 0:
                sid = act["actionparam"]
                if sid not in vnphy.rigidBodyIdDict:
                    raise Exception("RigidBody id '%s' not found"%sid)
            else:
                sid = None
            for rgb in vnphy.rigidBodys:
                if sid == None or rgb.id == sid:
                    rgb.resetRigidBody()
            return True
        except Exception as e:
            print("vntext error, can't parse command '%s': %s"%(act["origintext"], str(e)))
            return True

    return False

def debug_buttons(game,state):
    return [] # if we want no additional buttons

    # return in format of set_buttons_alt
    #return ["Demo btn (keitaro10)", demo1]