local addonName = ...
local PetSummoner = CreateFrame("Frame")

-- Events for auto-summon
PetSummoner:RegisterEvent("PLAYER_ENTERING_WORLD")
PetSummoner:RegisterEvent("ZONE_CHANGED_NEW_AREA")
PetSummoner:RegisterEvent("PLAYER_STOPPED_MOVING")

-- Auto-summon modes (default to favorite)
PetSummoner.autoFavorite = true
PetSummoner.autoAny      = false
PetSummoner.enabled      = true

-- LibDataBroker / LibDBIcon
local LDB    = LibStub("LibDataBroker-1.1")
local DBIcon = LibStub("LibDBIcon-1.0")

-- Create the popup
local popup = CreateFrame("Frame", "RPSPopup", UIParent, "BackdropTemplate")
popup:SetSize(240, 140)
popup:SetPoint("CENTER")
popup:SetBackdrop({
    bgFile   = "Interface\\DialogFrame\\UI-DialogBox-Background",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    tile     = true, tileSize = 16,
    edgeSize = 16,
    insets   = { left = 4, right = 4, top = 4, bottom = 4 },
})
popup:EnableMouse(true)
popup:SetMovable(true)
popup:RegisterForDrag("LeftButton")
popup:SetScript("OnDragStart", function(self) self:StartMoving() end)
popup:SetScript("OnDragStop",  function(self) self:StopMovingOrSizing() end)
popup:Hide()

-- Close (X) button
local closeBtn = CreateFrame("Button", nil, popup, "UIPanelCloseButton")
closeBtn:SetPoint("TOPRIGHT", popup, "TOPRIGHT", -4, -4)
closeBtn:SetScript("OnClick", function() popup:Hide() end)

-- Styling helper
local function styleButton(btn, enabled)
    local fs = btn:GetFontString()
    fs:SetTextColor(enabled and 0 or 1, enabled and 1 or 0, 0)  -- green if on, red if off
end

-- Create the three buttons
local favButton = CreateFrame("Button", "RPSFavBtn", popup, "UIPanelButtonTemplate")
favButton:SetSize(200, 24)
favButton:SetPoint("TOP", popup, "TOP", 0, -30)
favButton:SetText("Auto Favorite Pet")

local anyButton = CreateFrame("Button", "RPSRandBtn", popup, "UIPanelButtonTemplate")
anyButton:SetSize(200, 24)
anyButton:SetPoint("TOP", popup, "TOP", 0, -60)
anyButton:SetText("Auto Any Pet")

local offButton = CreateFrame("Button", "RPSOffBtn", popup, "UIPanelButtonTemplate")
offButton:SetSize(200, 24)
offButton:SetPoint("TOP", popup, "TOP", 0, -90)
offButton:SetText("Off")

-- Initial styling
styleButton(favButton, PetSummoner.autoFavorite)
styleButton(anyButton, PetSummoner.autoAny)
styleButton(offButton,     not (PetSummoner.autoFavorite or PetSummoner.autoAny))

-- OnClick scripts (mutually exclusive)
favButton:SetScript("OnClick", function()
    PetSummoner.autoFavorite = true
    PetSummoner.autoAny      = false
    PetSummoner.enabled      = true
    styleButton(favButton, true)
    styleButton(anyButton, false)
    styleButton(offButton,     false)
    print("Auto Favorite Pet |cff00ff00ON|r")
end)

anyButton:SetScript("OnClick", function()
    PetSummoner.autoFavorite = false
    PetSummoner.autoAny      = true
    PetSummoner.enabled      = true
    styleButton(favButton, false)
    styleButton(anyButton, true)
    styleButton(offButton,    false)
    print("Auto Any Pet |cff00ff00ON|r")
end)

offButton:SetScript("OnClick", function()
    PetSummoner.autoFavorite = false
    PetSummoner.autoAny      = false
    PetSummoner.enabled      = false
    styleButton(favButton, false)
    styleButton(anyButton, false)
    styleButton(offButton,    true)
    print("Auto-summon |cffff0000OFF|r")
end)

-- LDB launcher icon
local dataObj = LDB:NewDataObject("RandomPetSummoner", {
    type         = "launcher",
    text         = "RandomPetSummoner",
    icon         = "Interface\\AddOns\\RandomPetSummoner\\minimap_icon",
    OnClick      = function(self, button)
        if button == "LeftButton" then
            popup:SetShown(not popup:IsShown())
        end
    end,
    OnTooltipShow = function(tooltip)
        tooltip:AddLine("Random Pet Summoner")
        tooltip:AddLine("|cFFFFFF00Left-click:|r Toggle settings", 1, 1, 1)
    end,
})

-- Saved vars + icon registration
PetSummonerDB = PetSummonerDB or {}
PetSummonerDB.profile = PetSummonerDB.profile or {}
PetSummonerDB.profile.minimap = PetSummonerDB.profile.minimap or { hide = false }

-- first-run default angle (pick any 0–360)
if not PetSummonerDB.profile.minimap.minimapPos then
    PetSummonerDB.profile.minimap.minimapPos = 200
end

DBIcon:Register("RandomPetSummoner", dataObj, PetSummonerDB.profile.minimap)
DBIcon:Show("RandomPetSummoner")

-- Pet-summoning helper
function PetSummoner:HasActivePet()
    return C_PetJournal.GetSummonedPetGUID() ~= nil
end

-- Auto-summon logic
PetSummoner:SetScript("OnEvent", function(self, event)
    if not self.enabled then return end
    if     self.autoFavorite and not self:HasActivePet() then
        C_PetJournal.SummonRandomPet(true)
    elseif self.autoAny      and not self:HasActivePet() then
        C_PetJournal.SummonRandomPet(false)
    end
end)

-- Slash to toggle all
SLASH_RPS1 = "/rps"
SlashCmdList["RPS"] = function(msg)
    if msg:lower() == "toggle" then
        PetSummoner.enabled = not PetSummoner.enabled
        print("Auto-summon " .. (PetSummoner.enabled and "|cff00ff00ENABLED|r" or "|cffff0000DISABLED|r"))
    else
        print("Usage: /rps toggle — Enable/disable auto-summoning")
    end
end
