"""
VN Scene Script Ext - vnsound10
Provide functions to call VNSound function to control sound

f_sndstate,
f_sndctrl,

v1.0: 
- add f_sndstate, f_sndctrl function


Function: f_sndstate - import status for sound track
Syntax:
    :a:n:f_sndstate:id::status

Function: f_sndctrl - control sound track play
Syntax:
    :a:n:f_sndctrl:id:cmd:value

"""

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

    from vnsound import get_vnsound_manager
    if act["action"] == "f_sndstate":
        try:
            mgr = get_vnsound_manager(game)
            sid = act["actionparam"]
            state = eval(act["actionparam3"])
            if sid in mgr.soundDict:
                mgr.soundDict[sid].ImportStatus(state)
            else:
                mgr.ImportStatus(state)
            return True
        except Exception, e:
            print "vntext error, can't parse command '%s':"%act["origintext"], e
            return True

    if act["action"] == "f_sndctrl":
        def callCmd(soundsource, cmd, val):
            if cmd == "play":
                soundsource.Play()
            elif cmd == "stop":
                soundsource.Stop()
            elif cmd == "pause":
                soundsource.Pause()
            elif cmd == "seek":
                print "seek", act
                soundsource.Seek(val)
            elif cmd == "volume":
                soundsource.SetVolume(val/100)
            else:
                raise Exception("Unknown sound source control command: %s"%cmd)
        try:
            mgr = get_vnsound_manager(game)
            sid = act["actionparam"]
            cmd = act["actionparam2"].lower()
            if act.has_key("actionparam3") and len(act["actionparam3"]) > 0:
                val = float(act["actionparam3"])
            else:
                val = None
            if len(sid) == 0:
                for ss in mgr.soundDict.values():
                    callCmd(ss, cmd, val)
            elif sid in mgr.soundDict:
                callCmd(mgr.soundDict[sid], cmd, val)
            else:
                raise Exception("Unknown sound id: %s"%sid)
            return True
        except Exception, e:
            print "vntext error, can't parse command '%s':"%act["origintext"], 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]