-- 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, ...)

-- Set our global interface; and use QuestHelper2_Collection as fallback for any missing functions.
_G.QuestHelper2 = setmetatable(shared, {__index=_G.QuestHelper2_Collection})

-- Callbacks to invoke when QuestHelper is loaded.
local on_load = {}
local qh2_vars_avail = false
local qc_avail = false

-- Launches the onLoad callbacks if everything we depend on is available.
local function loadTest()
  local list = on_load
  if list and qh2_vars_avail and qc_avail then
    print("QuestHelper2 Loaded!")
    on_load = nil
    for i, cb in ipairs(list) do cb() end
  end
end

-- Use QuestHelper2_Collection's onLoad function to detect when it loads, before we replace it with
-- one specific to QuestHelper2.
shared.onLoad(function ()
  qc_avail = true
  loadTest()
end)

-- Need our own onLoad function, QuestHelper2_Collection's won't cut it.
function shared.onLoad(cb,...)
  if on_load then table.insert(on_load, shared.callback(cb, ...))
  else cb(...) end
end

-- Ask QuestHelper2_Collection to tell us when our saved variables are loaded.
shared.onEvent("ADDON_LOADED", function (addon)
  if addon == "QuestHelper2" then
    qh2_vars_avail = true
    loadTest()
  end
end)
