local AddOnName, ns = ...
local ss = ns.SS

local setmetatable = setmetatable
local GameMenuFrame, GameMenuButtonLogout, GameMenuButtonAddons = GameMenuFrame, GameMenuButtonLogout, GameMenuButtonAddons
local CloseWindows = CloseWindows

local mtMenuButton = {
};

local positionGameMenuButton = function()
    GameMenuFrame:SetHeight(GameMenuFrame:GetHeight() + GameMenuButtonLogout:GetHeight() - 4)
    local _, relTo, _, _, offY = GameMenuButtonLogout:GetPoint()

    if relTo ~= GameMenuFrame[AddOnName] then
        GameMenuFrame[AddOnName]:ClearAllPoints()
        GameMenuFrame[AddOnName]:Point("TOPLEFT", relTo, "BOTTOMLEFT", 0, -1)
--        GameMenuButtonLogout:ClearAllPoints()
--        GameMenuButtonLogout:Point("TOPLEFT", GameMenuFrame[AddOnName], "BOTTOMLEFT", 0, offY)
    end
end

local createInterfaceOption = function()
    local interfaceOption = CreateFrame("Frame", AddOnName.."InterfaceOption")
    interfaceOption.name = AddOnName
    InterfaceOptions_AddCategory(interfaceOption)

    local fs = interfaceOption:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    fs:SetPoint("TOPLEFT", 10, -15)
    fs:SetPoint("BOTTOMRIGHT", interfaceOption, "TOPRIGHT", 10, -45)
    fs:SetJustifyH("LEFT")
    fs:SetJustifyV("TOP")
    fs:SetText("Seamore Spheres")

    interfaceOption.configureButton = CreateFrame("Button", "SeamoreSpheresInterfaceOptionConfigure", interfaceOption, "UIPanelButtonTemplate")
    interfaceOption.configureButton:SetText("Configure")
    interfaceOption.configureButton:SetWidth(128)
    interfaceOption.configureButton:SetPoint("TOPLEFT", 10, -48)
    interfaceOption.configureButton:SetScript('OnClick', function()
        while CloseWindows() do end
        ss:ToggleConfig()
    end)
end

local createGameMenuButton = function()
    local GameMenuButton = CreateFrame("Button", "SeamoreSpheresGameMenuButton", GameMenuFrame, "GameMenuButtonTemplate")
    GameMenuButton:SetText("Spheres")
    GameMenuButton:SetScript("OnClick", function()
        ss:ToggleConfig()
        HideUIPanel(GameMenuFrame)
    end)
    GameMenuFrame[AddOnName] = GameMenuButton

    GameMenuButton:Size(GameMenuButtonLogout:GetWidth(), GameMenuButtonLogout:GetHeight())
    GameMenuButton:Point("TOPLEFT", GameMenuButtonAddons, "BOTTOMLEFT", 0, -1)
    hooksecurefunc('GameMenuFrame_UpdateVisibleButtons', positionGameMenuButton)
end

function mtMenuButton:Initialize()
--    createGameMenuButton()
    createInterfaceOption()
end

ss["MenuButton"] = setmetatable({}, { __index = mtMenuButton });
