---@diagnostic disable: undefined-field, undefined-global
---@type string
local AddonName = ...
---@class Private
local Private = select(2, ...)
---@class WTAPI
WTAPI = {}

-- Create the Ace addon
Private.Ace = LibStub("AceAddon-3.0"):NewAddon("WildThings", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0", "AceSerializer-3.0", "AceComm-3.0")
local wt = Private.Ace

-- LDB and Minimap Icon Setup
local LDB = LibStub("LibDataBroker-1.1")
local LDBIcon = LibStub("LibDBIcon-1.0")

local defaults = {
    profile = {
        minimap = {
            hide = false,
        },
        update = {
            auto_open = true,
        },
        assignment = {
            auto_open = true,
            current_encounter_id = 0,
        }
    },
}

local wtLDB = LDB:NewDataObject("WildThings", {
    type = "data source",
    text = "Wild Things",
    icon = "Interface\\Addons\\" .. AddonName .. "\\Media\\Icons\\icon_big.tga",
    OnClick = function(clickedFrame, button)
        if button == "LeftButton" then
            wt:Update()
        elseif button == "RightButton" then
            wt:Assignment()
        end
    end,
    OnTooltipShow = function(tooltip)
        local ICON_MOUSE_LEFT = "|A:newplayertutorial-icon-mouse-leftbutton:0:0|a"
        local ICON_MOUSE_RIGHT = "|A:newplayertutorial-icon-mouse-rightbutton:0:0|a"
        tooltip:AddLine("Wild Things")
        tooltip:AddLine(ICON_MOUSE_LEFT .. " Left-Click: Open WeakAuras Updater")
        tooltip:AddLine(ICON_MOUSE_RIGHT .. " Right-Click: Open your Assignments")
    end,
})

function wt:OnInitialize()
    -- Options DB
    self.db = LibStub("AceDB-3.0"):New("WildThings", defaults, true)

    -- Chat Commands
    self:RegisterChatCommand("wt", "ChatCommand")

    -- Sync Minimap Icon state with saved setting
    LDBIcon:Register("WildThings", wtLDB, self.db.profile.minimap)
    if self.db.profile.minimap.hide then
        LDBIcon:Hide("WildThings")
    else
        LDBIcon:Show("WildThings")
    end
end

function wt:OnEnable()
    Private.playerGUID = UnitGUID("player")
    Private.playerName = UnitName("player")
    Private.playerClassID = UnitClass("player")
    Private.playerSpecID, Private.playerSpecName, _, _, Private.playerRole = GetSpecializationInfo(GetSpecialization())
end

function wt:ChatCommand(cmd)
    if cmd == "update" then
        self:Update()
    elseif cmd == "assignment" then
        self:Assignment()
    elseif cmd == "icon hide" then
        self.db.profile.minimap.hide = true
        LDBIcon:Hide("WildThings")
        print("Wild Things Minimap Icon hidden.")
    elseif cmd == "icon show" then
        self.db.profile.minimap.hide = false
        LDBIcon:Show("WildThings")
        print("Wild Things Minimap Icon shown.")
    elseif cmd == "icon" then
        print("Usage: /wt icon [show|hide]")
    elseif cmd == "help" then
        print("Usage: /wt {command} [options]")
        print("Commands:")
        print("'update' - Shows the WeakAuras update frame.")
        print("'icon [show|hide]' - Use to show/hide the minimap icon.")
    elseif cmd == "roster" then
        self:Roster()
    end
end
