---@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")

-- Ensures the necessary addon settings is SavedVariables all exist and have sane values
local function EnsureSettings()
    if not WildThingsSavedVars then WildThingsSavedVars = {} end
    if not WildThingsSavedVars.minimap then WildThingsSavedVars.minimap = {} end
    if not WildThingsSavedVars.update then WildThingsSavedVars.update = {} end
    if not WildThingsSavedVars.assignment then WildThingsSavedVars.assignment = {} end

    if not WildThingsSavedVars.settings then WildThingsSavedVars.settings = {} end
    if not WildThingsSavedVars.settings.frames then WildThingsSavedVars.settings.frames = {} end

    if WildThingsSavedVars.minimap.hide == nil then WildThingsSavedVars.minimap.hide = false end
    if WildThingsSavedVars.update.auto_open == nil then WildThingsSavedVars.update.auto_open = true end
    if WildThingsSavedVars.update.party_mode == nil then WildThingsSavedVars.update.party_mode = false end
    if WildThingsSavedVars.update.memes == nil then WildThingsSavedVars.update.memes = true end
    if WildThingsSavedVars.assignment.auto_open == nil then WildThingsSavedVars.assignment.auto_open = true end
    if WildThingsSavedVars.assignment.current_encounter_id == nil then WildThingsSavedVars.assignment.current_encounter_id = 0 end
end

-- Updates the visibility and look of the minimap icon
-- When the player's addons/auras are up to date, and there are no warnings the icon is either white or hidden
-- If they are not, the icon is red
-- This is called whenever the player's version table updates, and whenever the highest seen versions update
function Private:UpdateMinimapIcon()
    -- Create minimap icon if it doesn't exist yet
    if not Private.LDB then
        Private.LDB = LDB:NewDataObject(
            "Wild Things AddOn",
            {
                type = "data source",
                text = "Wild Things AddOn",
                icon = [[Interface\Addons\WildThings\Media\Icons\icon_big.tga]],
                OnClick = function(clickedFrame, button)
                    if button == "LeftButton" then
                        Private.window:SetShown(not Private.window:IsShown())
                    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 Updater")
                    -- tooltip:AddLine(ICON_MOUSE_RIGHT .. " Right-Click: Open your Assignments")
                end,
            }
        )

        LDBIcon:Register("WildThings", Private.LDB, WildThingsSavedVars.minimap)
    end

    -- Update color
    if Private.upToDate then
        Private.LDB.icon = [[Interface\Addons\WildThings\Media\Icons\icon_big.tga]]
    else
        Private.LDB.icon = [[Interface\Addons\WildThings\Media\Icons\icon_red_big.tga]]
    end

    -- Update visibility
    if Private.upToDate and WildThingsSavedVars.settings.hideMinimapIcon then
        LDBIcon:Hide("WildThings")
    else
        LDBIcon:Show("WildThings")
    end
end

function wt:OnInitialize()
    EnsureSettings()

    Private:InitializeWeakAurasImporter()
    Private:InitializeInterfaceUpdate()
    Private:InitializeTransmission()
    Private:InitializeVersions()

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

    RunNextFrame(function() Private:UpdateMinimapIcon() end)

    RunNextFrame(function()
        if not Private.upToDate and WildThingsSavedVars.update.auto_open and not Private.versionCheckDone then
            if Private:CompareAddOnVersions(Private:GetPlayerVersionsTable().addOn, Private:GetHighestSeenAddOnVersion()) == 1 then
                print("|cff99ff00Wild Things:|r ", "A new AddOn version is available:", "|cffff5555"..Private:GetPlayerVersionsTable().addOn.."|r » |cff55ff55"..Private:GetHighestSeenAddOnVersion().."|r")
            end
            print("|cff99ff00Wild Things:|r ", "A new WeakAura version is available!")
            Private.window:Show()
        end
        Private.versionCheckDone = true
    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" or cmd == "u" then
        Private.window:Show()
    elseif cmd == "assignment" then
        self:Assignment()
    elseif cmd == "icon hide" then
        WildThingsSavedVars.minimap.hide = true
        LDBIcon:Hide("WildThings")
        print("Wild Things Minimap Icon hidden.")
    elseif cmd == "icon show" then
        WildThingsSavedVars.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
