-- Copyright (C) 2011 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

-- This file adds a warning to the tracker if profiling is enabled, as it can really hurt performance.

if GetCVar("scriptProfile") == "1" then
  local frame = CreateFrame("Button", nil, UIParent)
  frame:EnableMouse(true)
  
  local text = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  text:SetPoint("LEFT", 32, 0)
  text:SetJustifyH("LEFT")
  
  local icon = frame:CreateTexture(nil, "ARTWORK")
  icon:SetWidth(32)
  icon:SetHeight(32)
  icon:SetPoint("LEFT")
  icon:SetTexture("Interface\\AddOns\\QuestHelper2\\Icons\\Caution.tga")
  
  local old_SetWidth = frame.SetWidth
  function frame:SetWidth(width)
    old_SetWidth(self, width)
    text:SetWidth(width-32)
    self:SetHeight(math.max(32, text:GetStringHeight()))
  end
  
  local disabled = false
  
  shared.onSetting("locale", function ()
    if not disabled then
      text:SetText(L.PROFILING_WARNING)
      shared.addFrameToTracker(frame, 0, true)
    end
  end)
  
  frame:SetScript("OnClick", function ()
    disabled = true
    shared.removeFrameFromTracker(frame)
    
    StaticPopupDialogs["QH2_DISABLE_PROFILING"] = {
      text = L.PROFILING_DISABLE_DIALOG,
      button1 = ACCEPT,
      button2 = CANCEL,
      OnAccept = function ()
        SetCVar("scriptProfile", "0")
        ReloadUI()
      end,
      timeout = 0,
      whileDead = true,
      hideOnEscape = true,
      showAlert = true
    }
    
    StaticPopup_Show("QH2_DISABLE_PROFILING")
  end)
end
