-------------------------------------------------------------------------------
-- LAddMin v0.1
-------------------------------------------------------------------------------
--
-- Copyright (c) 2014 Ales Machat (Garkin)
--
-- Permission is hereby granted, free of charge, to any person
-- obtaining a copy of this software and associated documentation
-- files (the "Software"), to deal in the Software without
-- restriction, including without limitation the rights to use,
-- copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following
-- conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-- OTHER DEALINGS IN THE SOFTWARE.
--
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- Libraries ------------------------------------------------------------------
-------------------------------------------------------------------------------
local LAM1 = LibStub("LibAddonMenu-1.0")
local LAM2 = LibStub("LibAddonMenu-2.0")

-------------------------------------------------------------------------------
-- Library replacement --------------------------------------------------------
-------------------------------------------------------------------------------
local FAKE_LAM1 = setmetatable ({}, { __index = LAM1 })
LibStub.libs["LibAddonMenu-1.0"] = FAKE_LAM1
LibStub.minors["LibAddonMenu-1.0"] = 999

local settingsTable = {}
local panelIDs = {}

local knownAddons = LAddMinCompatability

LAddMinUpconvertSettings = ZO_SavedVars:NewAccountWide('LAddMin_ConversionList', 2, "list", { cfg = {}})

-------------------------------------------------------------------------------
-- FAKE_LAM1 functions --------------------------------------------------------
-------------------------------------------------------------------------------
-- LAM1:CreateControlPanel(controlPanelID, controlPanelName)
function FAKE_LAM1:CreateControlPanel(controlPanelID, controlPanelName)
   local name = controlPanelName:gsub("|[cC]%w%w%w%w%w%w",""):gsub("|[rR]","")

   --Not in settings -- new addon
   if LAddMinUpconvertSettings.cfg[name] == nil then
       --Either known bad or unknown. Set to not convert
       if not knownAddons[name] then
         LAddMinUpconvertSettings.cfg[name] = false
         return LAM1:CreateControlPanel(controlPanelID, name)
       end
       --Known good. Default to convert
       LAddMinUpconvertSettings.cfg[name] = true
   else
       --Use saved value
       if not LAddMinUpconvertSettings.cfg[name] then
         return LAM1:CreateControlPanel(controlPanelID, name)
       end
   end

   if panelIDs[name] ~= nil then
      return panelIDs[name]
   end

   local panelID = #settingsTable + 1001
   panelIDs[name] = panelID

   local panelTable = {
      name = name,
      displayName = controlPanelName,
      controls = {},
   }

   settingsTable[panelID - 1000] = panelTable

   LAM2:RegisterAddonPanel(controlPanelID, panelTable)
   LAM2:RegisterOptionControls(controlPanelID, panelTable.controls)

   return panelID
end
-- LAM1:AddHeader(panelID, controlName, text)
function FAKE_LAM1:AddHeader(panelID, controlName, text)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddHeader(panelID, controlName, text)
   end

   local controlTable = {
      type = "header",
      name = text,
      reference = controlName,
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab]
end
-- LAM1:AddSlider(panelID, controlName, text, tooltip, minValue, maxValue, step, getFunc, setFunc, warning, warningText)
function FAKE_LAM1:AddSlider(panelID, controlName, text, tooltip, minValue, maxValue, step, getFunc, setFunc, warning, warningText)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddSlider(panelID, controlName, text, tooltip, minValue, maxValue, step, getFunc, setFunc, warning, warningText)
   end

   local controlTable = {
      type = "slider",
      name = text,
      tooltip = tooltip,
      min = minValue,
      max = maxValue,
      step = step,
      getFunc = getFunc,
      setFunc = setFunc,
      warning = warningText,
      reference = controlName,
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab]
end
-- LAM1:AddDropdown(panelID, controlName, text, tooltip, validChoices, getFunc, setFunc, warning, warningText)
function FAKE_LAM1:AddDropdown(panelID, controlName, text, tooltip, validChoices, getFunc, setFunc, warning, warningText)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddDropdown(panelID, controlName, text, tooltip, validChoices, getFunc, setFunc, warning, warningText)
   end

   local controlTable = {
      type = "dropdown",
      name = text,
      tooltip = tooltip,
      choices = validChoices,
      getFunc = getFunc,
      setFunc = setFunc,
      warning = warningText,
      reference = controlName,
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab]
end
-- LAM1:AddCheckbox(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
function FAKE_LAM1:AddCheckbox(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddCheckbox(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
   end

   local controlTable = {
      type = "checkbox",
      name = text,
      tooltip = tooltip,
      getFunc = getFunc,
      setFunc = setFunc,
      warning = warningText,
      reference = controlName,
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab]
end
-- LAM1:AddColorPicker(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
function FAKE_LAM1:AddColorPicker(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddColorPicker(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
   end

   local controlTable = {
      type = "colorpicker",
      name = text,
      tooltip = tooltip,
      getFunc = getFunc,
      setFunc = setFunc,
      warning = warningText,
      reference = controlName,
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab]
end
-- LAM1:AddEditBox(panelID, controlName, text, tooltip, isMultiLine, getFunc, setFunc, warning, warningText)
function FAKE_LAM1:AddEditBox(panelID, controlName, text, tooltip, isMultiLine, getFunc, setFunc, warning, warningText)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddEditBox(panelID, controlName, text, tooltip, isMultiLine, getFunc, setFunc, warning, warningText)
   end

   local controlTable = {
      type = "editbox",
      name = text,
      tooltip = tooltip,
      getFunc = getFunc,
      setFunc = setFunc,
      isMultiline = isMultiLine,
      warning = warningText,
      reference = controlName,
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab]
end
-- LAM1:AddButton(panelID, controlName, text, tooltip, onClick, warning, warningText)
function FAKE_LAM1:AddButton(panelID, controlName, text, tooltip, onClick, warning, warningText)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddButton(panelID, controlName, text, tooltip, onClick, warning, warningText)
   end

   local controlTable = {
      type = "button",
      name = text,
      tooltip = tooltip,
      func = onClick,
      warning = warningText,
      reference = controlName,
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab]
end
-- LAM1:AddDescription(panelID, controlName, text, titleText)
function FAKE_LAM1:AddDescription(panelID, controlName, text, titleText)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddDescription(panelID, controlName, text, titleText)
   end

   local controlTable = {
      type = "description",
      title = titleText,
      text = text,
      reference = controlName,
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab]
end
-- LAM1:AddSubMenu(panelID, controlName, text, tooltip)
function FAKE_LAM1:AddSubMenu(panelID, controlName, text, tooltip)
   if type(panelID) == "userdata" or (type(panelID) == "number" and panelID < 1000) then
      return LAM1:AddSubMenu(panelID, controlName, text, tooltip)
   end

   local controlTable = {
      type = "submenu",
      name = text,
      tooltip = tooltip,
      reference = controlName,
      controls = {},
   }

   local tab = type(panelID) == "table" and panelID or settingsTable[panelID - 1000].controls
   table.insert(tab, controlTable)

   return tab[#tab].controls
end