-- Copyright (C) 2010 Tyson Brown
-- 
-- This file is part QuestHelper2, an AddOn for World of Warcraft.
-- 
-- QuestHelper2 is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- 
-- QuestHelper2 is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with QuestHelper2. If not, see <http://www.gnu.org/licenses/>.

if LibDebug then LibDebug() end
local shared = select(2, ...)

-- Plays sound for progress on objectives.

local selected_sound, selected_priority = nil, 0
local last_sound_play = GetTime()

local function playSelectedSound()
  local now = GetTime()
  
  if now > last_sound_play + 5 and shared.settings.sound_enabled then
    last_sound_play = now
    PlaySoundFile(selected_sound)
  end
  
  selected_sound, selected_priority = nil, 0
end

local function queueSound(sound, priority)
  if sound ~= "Silence" and priority > selected_priority then
    selected_sound, selected_priority = sound, priority
    shared.callOnce(playSelectedSound)
  end
end

shared.onObjectiveProgress(function (objective)
  if objective.complete then
    if objective.root then
      queueSound(shared.settings.sound_root_complete, 4)
    else
      local parent_obtainable = false
      for dep in objective:revDependencies() do
        if dep.obtainable then
          parent_obtainable = true
          break
        end
      end
      
      if parent_obtainable then
        queueSound(shared.settings.sound_objective_unblocked, 3)
      else
        queueSound(shared.settings.sound_objective_complete, 2)
      end
    end
  else
    queueSound(shared.settings.sound_objective_progress, 1)
  end
end)
