local addonName, SinsToolkit = ...

local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("PLAYER_LOGIN")

local function HandleDefaultBinding(bindingName, defaultKey)
    local bind1, bind2 = GetBindingKey(bindingName)
    local action = GetBindingAction(defaultKey)
    if bind1 == nil and bind2 == nil and action == "" then
        SetBinding(defaultKey, bindingName)
    end
end

function IsRetail()
    return WOW_PROJECT_ID == WOW_PROJECT_MAINLINE
end

function IsClassic()
    return WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
end

function IsBCC()
    return WOW_PROJECT_ID == WOW_PROJECT_BURNING_CRUSADE_CLASSIC
end

function IsMop()
    return WOW_PROJECT_ID == WOW_PROJECT_MISTS_CLASSIC
end

local function EnsureQuickLinkDB()
    if SinsToolkit.EnsureDB then SinsToolkit.EnsureDB() end
    SinsToolkitWowheadQuickLinkCfg = SinsToolkitDB.wowheadQuickLink
end

frame:SetScript("OnEvent", function(self, event, arg1)
    if event == "ADDON_LOADED" and arg1 == addonName then
        EnsureQuickLinkDB()
    elseif event == "PLAYER_LOGIN" then
        EnsureQuickLinkDB()
        if SinsToolkitWowheadQuickLinkCfg.default_bindings_set == nil then
            HandleDefaultBinding("SINS_TOOLKIT_WOWHEAD_QUICK_LINK_NAME", "CTRL-C")
            HandleDefaultBinding("SINS_TOOLKIT_WOWHEAD_QUICK_LINK_RAIDERIO_NAME", "CTRL-SHIFT-C")
            SaveBindings(GetCurrentBindingSet())
            SinsToolkitWowheadQuickLinkCfg.default_bindings_set = true
        end
        self:UnregisterEvent("PLAYER_LOGIN")
    end
end)

BINDING_HEADER_SINS_TOOLKIT_WOWHEAD_QUICK_LINK_HEADER = "Sin's Toolkit"
BINDING_NAME_SINS_TOOLKIT_WOWHEAD_QUICK_LINK_NAME = "Generate Wowhead link"
BINDING_NAME_SINS_TOOLKIT_WOWHEAD_QUICK_LINK_RAIDERIO_NAME = "Generate Raider.IO link"
