-- 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


local qh2_button = CreateFrame("Button", "QuestHelper2_Button", WorldMapButton, "UIPanelButtonTemplate2")
qh2_button:SetFrameLevel(qh2_button:GetParent():GetFrameLevel()+4)
shared.onSetting("locale", function() qh2_button:SetText(L.PANEL_QUESTHELPER2) end)
shared.onSetting("menu", function (enabled) qh2_button[enabled and "Show" or "Hide"](qh2_button) end)

local function fadeIn(self, delta)
  local a = self:GetAlpha() + delta * 3
  if a > 1 then
    self:SetAlpha(1)
    self:SetScript("OnUpdate", nil)
  else
    self:SetAlpha(a)
  end
end

local function fadeOut(self, delta)
  local a = self:GetAlpha() - delta * 0.2
  if a < .3 then
    self:SetAlpha(.3)
    self:SetScript("OnUpdate", nil)
  else
    self:SetAlpha(a)
  end
end

local function startFadeOut()
  qh2_button:SetScript("OnUpdate", fadeOut)
end

qh2_button:SetScript("OnEnter", function ()
  -- Fade in, cancel timed fadeout.
  shared.cancelDelayed(startFadeOut)
  qh2_button:SetScript("OnUpdate", fadeIn)
  QuestHelper2_MapOverlayTooltip:SetOwner(qh2_button, "ANCHOR_BOTTOM")
  QuestHelper2_MapOverlayTooltip:AddLine(L.PANEL_QUESTHELPER2)
  QuestHelper2_MapOverlayTooltip:AddLine(L.SETTING_ENABLED_TOOLTIP, 1, 1, 1, true)
  QuestHelper2_MapOverlayTooltip:Show()
end)

qh2_button:SetScript("OnLeave", function ()
  -- Start fading out 2 seconds after the mouse leaves the button.
  shared.onDelay(2, startFadeOut)
  QuestHelper2_MapOverlayTooltip:Hide()
end)

qh2_button:SetScript("OnShow", function ()
  -- When first shown, fade in so the player knows the button is there,
  -- then fade out after 5 seconds so that we're not in the way.
  qh2_button:SetScript("OnUpdate", fadeIn)
  shared.onDelay(5, startFadeOut)
end)

qh2_button:SetAlpha(.25)

local function toggleSetting(frame)
  shared.settings[frame.value] = not shared.settings[frame.value]
end

local toggle_settings = {
  "enabled",
  "overlay_shown",
  "arrow_shown",
  "tracker_shown",
  "compass_shown",
  "digsite_create_objectives",
}

local panel = nil
local function openConfig()
  shared.openConfig(panel)
end

local function initializeMenu(frame, level)
  local info = UIDropDownMenu_CreateInfo()
  
  info.text  = L.OPEN_CONFIGURATION
  info.notCheckable = true
  info.func = openConfig
  
  UIDropDownMenu_AddButton(info, level)
  
  if type(_G.UIDROPDOWNMENU_MENU_VALUE) == "function" then
    _G.UIDROPDOWNMENU_MENU_VALUE()
    return
  end
  
  for i, setting in ipairs(type(_G.UIDROPDOWNMENU_MENU_VALUE) == "table" and _G.UIDROPDOWNMENU_MENU_VALUE or toggle_settings) do
    info = UIDropDownMenu_CreateInfo()
    
    info.text = L["SETTING_"..setting:upper()]
    info.value = setting
    info.isNotRadio = true
    info.checked = shared.settings[setting]
    info.func = toggleSetting
    info.disabled = shared.settings_override[setting] ~= nil
    
    UIDropDownMenu_AddButton(info, level)
  end
end

local qh2_menu = CreateFrame("Frame", "QuestHelper2_MainMenu", nil, "UIDropDownMenuTemplate")

function shared.showMenu(initFunc, value, frame, x, y)
  _G.UIDROPDOWNMENU_MENU_VALUE = nil
  UIDropDownMenu_Initialize(qh2_menu, initFunc, "MENU")
  ToggleDropDownMenu(1, value, qh2_menu, frame, x, y)
end

function shared.showMainMenu(frame, x, y, extra, panel_name)
  panel = panel_name
  shared.showMenu(initializeMenu, extra, frame, x, y)
end

qh2_button:SetHeight(22)
qh2_button:SetWidth(110)
qh2_button:SetPoint("TOPRIGHT", -5, -5)
qh2_button:RegisterForClicks("LeftButtonUp", "RightButtonUp")

qh2_button:SetScript("OnClick", function (self, button)
  if button == "RightButton" then
    shared.showMainMenu(self, -4, -6, nil, "overlay")
  else
    shared.settings.enabled = not shared.settings.enabled
  end
end)

local enabled = true

local function redrawButtonPressed()
  local texture_path = enabled and "Interface\\Buttons\\UI-Panel-Button-Down" or "Interface\\Buttons\\UI-Panel-Button-Disabled-Down"
  
  local name = qh2_button:GetName()
  _G[name.."Left"]:SetTexture(texture_path)
  _G[name.."Middle"]:SetTexture(texture_path)
  _G[name.."Right"]:SetTexture(texture_path)
end

local function redrawButtonNormal()
  local texture_path = enabled and "Interface\\Buttons\\UI-Panel-Button-Up" or "Interface\\Buttons\\UI-Panel-Button-Disabled"
  
  local name = qh2_button:GetName()
  _G[name.."Left"]:SetTexture(texture_path)
  _G[name.."Middle"]:SetTexture(texture_path)
  _G[name.."Right"]:SetTexture(texture_path)
end

qh2_button:SetScript("OnMouseDown", redrawButtonPressed)
qh2_button:SetScript("OnMouseUp", redrawButtonNormal)
shared.onSetting("enabled", function (_enabled)
  enabled = _enabled
  
  if qh2_button:GetButtonState() == "NORMAL" then
    redrawButtonNormal()
  else
    redrawButtonPressed()
  end
end)
