--defining a function to make coloured text
function treesay(text)
  current_sprite:say("`%" .. text)
end

-- a function to one-shot SFX w/no looping - obsolete and inbuilt
function oneshot(sound)
  return dink.playsound(sound, 0, 0, 0, false)
end

function main()
  treesay("Media test - Lua variant")
end

function hit()
  treesay("Ouch!")
end

function talk()
  player:freeze()
  -- picker
  local menu = dink.create_choice_menu()
  menu.title_color = 3
  menu.title = "Pick something will ya"

  local video = menu:add_choice("Play video")
  local tracker = menu:add_choice("Play tracker module")
  local spc = menu:add_choice("Play SNES SPC")
  local wav = menu:add_choice("Repeating WAV with feature test")
  local ogg = menu:add_choice("Ogg Vorbis SFX")
  local mpthree = menu:add_choice("MP3 SFX")

  local result = menu:show()

  if result == video then
    dink.playavi("anim/fireworks.mp4")
    treesay("A video should have popped up if you have FFMpeg installed")
  end

  if result == tracker then
    dink.playmidi("Farewell.it")
    treesay('Playing "Farewell" by Siren')
    dink.wait(5000)
    treesay("Let's test pattern-setting")
    dink.play_mod_order(2)
  end

  if result == spc then
    dink.playmidi("prime.spc")
    treesay("If your build has MixerX you should be hearing something")
  end

  if result == wav then
    dink.stopmidi()
    local snd = dink.playsound(53, 0, 0, 0, true)
    dink.wait(2000)
    if dink.get_platform() == 0 then
      treesay("Volume-setting doesn't work on Windows, skipping")
    else
      treesay("Attempting to attenuate")
      snd.vol = 15
    end
    dink.wait(2000)
    -- default vol in Lua is 100 now since we don't need backwards compat
    snd:fade_vol(100, 5)
    treesay("Fading back to full volume over 5 seconds")
    dink.wait(5000)
    snd:oscillate_vol(50, 100, 5)
    treesay("Oscillating volume between half and full, cycle of 5 seconds")
    dink.wait(5000)
    snd.pan = -100
    treesay("Panning to the left")
    dink.wait(4000)
    snd:fade_pan(100, 5)
    treesay("Panning right over 5 seconds")
    dink.wait(7500)
    snd:oscillate_pan(-50, 50, 1)
    treesay("Oscillating pan, 1-second cycle")
    dink.wait(5000)
    treesay("Increasing speed over 5 seconds")
    snd:fade_speed(120, 5)
    dink.wait(4000)
    snd.vol = 100
    snd.pan = 0
    player:say("Hurry up this is boring...")
    snd:oscillate_speed(90, 110, 4)
    treesay("Oscillating speed, 4-second cycle")
    dink.wait(5000)
    treesay("Decreasing playback rate to 22khz without altering speed")
    snd.hz = 22050
    dink.wait(6000)
    snd.pause = true
    treesay("Pausing")
    dink.wait(3000)
    treesay("Resuming")
    snd.pause = false
    dink.wait(5000)
    treesay("Setting back to 44khz. Speed normal. " .. snd.pos .. " seconds into clip")
    snd.hz = 44100
    dink.wait(100)
    snd.speed = 100
    dink.wait(4000)
    treesay("Setting loop point. Sound has looped " .. snd.loopcount .. " times")
    snd.looppoint = 11
    dink.wait(4000)
    treesay("Scheduling stop in 20 seconds")
    snd:schedule_stop(20)
  end

  if result == ogg then
    dink.stopmidi()
    local sfx = dink.playsound(55, 0, 0, 0, true)
    sfx.survive = true
    treesay("This should loop indefinitely...")
    dink.wait(3000)
    treesay("And survive screen changes")
  end

  if result == mpthree then
    dink.stopmidi()
    local ewql = oneshot(57)
    treesay("MP3 as SFX, no looping")
  end


  player:unfreeze()
end
