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

shared.onAchievementComplete(function (id)
  --[===[@debug@
  print("Complete achievement: "..tostring(select(2, GetAchievementInfo(id))))
  --@end-debug@]===]
  shared.onLocation(function (area, level, x, y, ex, ey)
    shared.getAchievementData(id, function (data)
      data:addFinishLocation(area, level, x, y, ex, ey)
      --[===[@debug@
      print("Saved data for "..tostring(select(2, GetAchievementInfo(id))))
      --@end-debug@]===]
    end)
  end)
end)

-- These are achievement criteria types that we won't bother collecting locations for.
local ignored_criteria_types = {
 [7] = true, -- Weapon skill
 [8] = true, -- Another achievement
 [9] = true, -- Completing any quest.
 [10] = true, -- Completing daily quests.
 [11] = true, -- Area specific quests.
 [14] = true, -- Daily quests.
 [27] = true, -- A specific quest.
 [31] = true, -- PvP kills.
 [34] = true, -- Obtain a pet.
 [35] = true, -- PvP kills while under the influence of something.
 [37] = true, -- Winning arenas
 [41] = true, -- Eat or drink something.
 [45] = true, -- Bankslots.
 [46] = true, -- Reputation to exalted.
 [47] = true, -- 5 repultations to exalted.
 [49] = true, -- Equip an item.
 [52] = true, -- Kill a player of some class.
 [53] = true, -- Kill a player of some race.
 [54] = true, -- Emote something.
 [62] = true, -- Gold from quests.
 [67] = true, -- Gold from loot.
 [75] = true, -- Obtain mounts.
 [112] = true, -- Learn recipies.
 [113] = true -- Honorable kills.
}

shared.onAchievementProgress(function (id, criteria, _, _, criteria_type)
  if not ignored_criteria_types[criteria_type] then
    --[===[@debug@
    print("Progress toward "..tostring(select(2, GetAchievementInfo(id))))
    --@end-debug@]===]
    shared.onLocation(function (area, level, x, y, ex, ey)
      shared.getAchievementData(id, function (data)
        data:addCriteriaLocation(criteria, area, level, x, y, ex, ey)
        --[===[@debug@
        print("Saved data for "..tostring(select(2, GetAchievementInfo(id))))
        --@end-debug@]===]
      end)
    end)
  else
    --[===[@debug@
    print("Ignored toward "..tostring(select(2, GetAchievementInfo(id))).."; criteria_type = "..criteria_type)
    --@end-debug@]===]
  end
end)

shared.onAchievementAsset(function (id, criteria, asset, asset_type)
  --[===[@debug@
  print("Assigning asset #"..asset.." to "..tostring(select(2, GetAchievementInfo(id))))
  --@end-debug@]===]
  shared.getAchievementData(id, function (data)
    data:addCriteriaAsset(criteria, asset, asset_type)
  end)
end)

shared.onQuestAdd(function (id, reason, ...)
  if reason == "npc" then
    local npc_id = ...
    shared.onLocation(function (area, level, x, y, ex, ey)
      shared.getQuestData(id, function (data)
        data:addStartNPC(npc_id)
        data:addAcquireLocation(area, level, x, y, ex, ey)
        data:addRace(select(2, UnitRace("player")))
        data:addClass(select(2, UnitClass("player")))
        data:addLevel(UnitLevel("player"))
      end)
    end)
  elseif reason == "object" then
    local object_id = ...
    shared.onLocation(function (area, level, x, y, ex, ey)
      shared.getQuestData(id, function (data)
        data:addStartObject(object_id)
        data:addAcquireLocation(area, level, x, y, ex, ey)
        data:addRace(select(2, UnitRace("player")))
        data:addClass(select(2, UnitClass("player")))
        data:addLevel(UnitLevel("player"))
      end)
    end)
  elseif reason == "autoquest" then
    shared.getQuestData(id, function (data)
      data:addAutoQuestAcquire()
      data:addRace(select(2, UnitRace("player")))
      data:addClass(select(2, UnitClass("player")))
      data:addLevel(UnitLevel("player"))
    end)
  elseif reason == "item" then
    local item_id = ...
    shared.getQuestData(id, function (data)
      data:addStartItem(item_id)
      data:addRace(select(2, UnitRace("player")))
      data:addClass(select(2, UnitClass("player")))
      data:addLevel(UnitLevel("player"))
    end)
  else
    assert(reason == "existing" or reason == "player")
  end
end)

shared.onQuestRemove(function (id, reason, ...)
  if reason ~= "abandon" and not shared.quest_order_set[id] then
    -- Add this quest to the quest order list for the player,
    -- this list will eventually be used to infer the dependencies
    -- between quests.
    shared.quest_order_set[id] = true
    table.insert(shared.quest_order, id)
  end
  
  if reason == "npc" then
    local npc_id = ...
    shared.onLocation(function (area, level, x, y, ex, ey)
      shared.getQuestData(id, function (data)
        data:addEndNPC(npc_id)
        data:addTurninLocation(area, level, x, y, ex, ey)
      end)
    end)
  elseif reason == "object" then
    local object_id = ...
    shared.onLocation(function (area, level, x, y, ex, ey)
      shared.getQuestData(id, function (data)
        data:addEndObject(object_id)
        data:addTurninLocation(area, level, x, y, ex, ey)
      end)
    end)
  elseif reason == "autoquest" then
    shared.getQuestData(id, function (data)
      data:addAutoQuestTurnin()
    end)
  else
    assert(reason == "abandon")
  end
end)

shared.onQuestComplete(function (id)
  shared.onLocation(function (area, level, x, y, ex, ey)
    shared.getQuestData(id, function (data)
      data:addCompletionLocation(area, level, x, y, ex, ey)
    end)
  end)
end)

shared.onQuestProgress(function (id, objective)
  shared.onLocation(function (area, level, x, y, ex, ey)
    shared.getQuestData(id, function (data)
      data:addObjectiveLocation(objective, area, level, x, y, ex, ey)
    end)
  end)
end)

-- Also, Classes/building.lua collects information on buildings.
