-- 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, ...)
local L = shared.L

-- Who the hell designed WoW's menu system? I mean, WTF.
local function setPriority(frame, objective, priority)
  objective.priority = priority
  
  -- ToggleDropDownMenu(nil, nil, menu)
end

local function openObjective(frame, objective)
  objective()
end

local function disableObjective(frame, objective)
  objective.enabled = false
end

local function initializeMenu(frame, level)
  local value = _G.UIDROPDOWNMENU_MENU_VALUE
  
  if value then
    local type = value.type
    
    if type == "objective_list" then
      for i, objective in ipairs(value.list) do
        local info = UIDropDownMenu_CreateInfo()
        
        info.text  = objective.text
        info.value = { type = "objective", objective = objective }
        info.notCheckable = true
        info.hasArrow = true
        
        UIDropDownMenu_AddButton(info, level)
      end
    elseif type == "objective" then
      local info = UIDropDownMenu_CreateInfo()
      info.text = value.objective.text
      info.notCheckable = true
      
      if value.objective.callback then
        info.colorCode = "|cffffd100"
        info.arg1 = value.objective
        info.func = openObjective
      else
        info.isTitle = true
      end
      
      UIDropDownMenu_AddButton(info, level)
      
      if not value.objective.owned then
        info = UIDropDownMenu_CreateInfo()
        info.text  = L.OBJECTIVE_DISABLE
        info.func = disableObjective
        info.arg1 = value.objective
        info.notCheckable = true
        
        UIDropDownMenu_AddButton(info, level)
      end
      
      info = UIDropDownMenu_CreateInfo()
      info.text  = L.OBJECTIVE_PRIORITY
      info.value = { type = "priority", objective = value.objective }
      info.notCheckable = true
      info.hasArrow = true
      
      UIDropDownMenu_AddButton(info, level)
    elseif type == "priority" then
      local lowest_legal_priority = value.objective.selected and 0 or 5
      
      for dep in value.objective:revDependencies() do
        lowest_legal_priority = math.min(lowest_legal_priority, dep.priority)
      end
      
      for i = 1,lowest_legal_priority do
        local info = UIDropDownMenu_CreateInfo()
        
        info.text  = L["PRIORITY"..i]
        info.arg1 = value.objective
        info.arg2 = i
        info.func  = setPriority
        info.checked = value.objective.priority == i
        
        UIDropDownMenu_AddButton(info, level)
      end
      
      -- Disable all the rest.
      for i = lowest_legal_priority+1,5 do
        local info = UIDropDownMenu_CreateInfo()
        
        info.text  = L["PRIORITY"..i]
        info.value = nil
        info.disabled = true
        
        UIDropDownMenu_AddButton(info, level)
      end
    end
  end
end

function shared.showObjectiveMenu(frame, objective)
  shared.showMenu(initializeMenu, {type="objective", objective=objective}, frame, 0, 0)
end

function shared.showObjectiveListMenu(frame, list)
  if #list == 1 then
    -- Only one objective to display, show it directly.
    shared.showObjectiveMenu(frame, list[1])
    return
  end
  
  shared.showMenu(initializeMenu, {type="objective_list", list=list}, frame, 0, 0)
end
