local addonName, privateDB = ...

-- ============================================================================
-- HDG_VAMOOSE MODULE
-- Lumber farming quotes with Talking Head display
-- Features two speakers: Vamoose (shaman) and Gazlowe (goblin)
-- Quotes are stored in HDG_CraftQuotes for unified quote management
-- ============================================================================

local HDG_Vamoose = {}
privateDB.HDG_Vamoose = HDG_Vamoose

-- Reference to the centralized quote database
local HDG_CraftQuotes = privateDB.HDG_CraftQuotes

-- ============================================================================
-- TALKING HEAD FRAME
-- Custom talking head display for lumber farming quotes
-- ============================================================================

-- Create the main frame
local VamooseHead = CreateFrame("Frame", "HDG_VamooseTalkingHead", UIParent)
VamooseHead:SetSize(570, 155)
VamooseHead:SetPoint("TOP", UIParent, "TOP", 0, -200)
VamooseHead:Hide()
VamooseHead:SetFrameStrata("DIALOG")

-- Background texture
local vamooseBg = VamooseHead:CreateTexture(nil, "BACKGROUND")
vamooseBg:SetPoint("TOPLEFT", 0, 0)
vamooseBg:SetPoint("BOTTOMRIGHT", 0, 0)

-- Portrait frame overlay
local vamoosePortraitFrame = VamooseHead:CreateTexture(nil, "OVERLAY")
vamoosePortraitFrame:SetSize(143, 143)
vamoosePortraitFrame:SetPoint("TOPLEFT", 5, -6)

-- 3D Model frame
local vamooseModel = CreateFrame("PlayerModel", nil, VamooseHead)
vamooseModel:SetSize(115, 115)
vamooseModel:SetPoint("TOPLEFT", 19, -20)
vamooseModel:SetFrameStrata("DIALOG")
vamooseModel:SetFrameLevel(VamooseHead:GetFrameLevel() + 1)

-- Name text
local vamooseNameText = VamooseHead:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
vamooseNameText:SetPoint("TOPLEFT", 160, -28)
vamooseNameText:SetTextColor(1, 0.82, 0) -- Gold

-- Body text
local vamooseBodyText = VamooseHead:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge")
vamooseBodyText:SetPoint("TOPLEFT", 160, -55)
vamooseBodyText:SetWidth(380)
vamooseBodyText:SetJustifyH("LEFT")
vamooseBodyText:SetWordWrap(true)

-- Play a quote with the talking head
-- speaker: "SHAMAN" or "GOBLIN" (defaults to SHAMAN)
-- displayID: optional override for the model display ID
local function PlayTalkingHead(quote, speaker, displayID)
    local speakerData = HDG_CraftQuotes.GetSpeaker(speaker) or HDG_CraftQuotes.GetSpeaker("SHAMAN")
    displayID = displayID or speakerData.displayID

    -- Set backdrop art (GarrFollower style - golden/bronze)
    vamooseBg:SetAtlas("TalkingHeads-GarrFollower-TextBackground")
    vamoosePortraitFrame:SetAtlas("TalkingHeads-GarrFollower-PortraitFrame")

    -- Setup 3D model
    vamooseModel:ClearModel()
    vamooseModel:SetDisplayInfo(displayID)
    vamooseModel:SetPortraitZoom(0.8)
    vamooseModel:SetCamDistanceScale(1.0)
    vamooseModel:SetPosition(0, 0, 0)

    -- Lighting for WoW 11.x
    local lightValues = {
        omnidirectional = false,
        point = CreateVector3D(-1, 0, -1),
        ambientIntensity = 0.7,
        ambientColor = CreateColor(0.7, 0.7, 0.7),
        diffuseIntensity = 1.0,
        diffuseColor = CreateColor(0.8, 0.8, 0.8),
    }
    vamooseModel:SetLight(true, lightValues)
    vamooseModel:SetAnimation(60) -- Talking animation

    -- Set text with speaker name
    vamooseNameText:SetText(speakerData.name)
    vamooseBodyText:SetText(quote)

    -- Show with fade in
    VamooseHead:Show()
    VamooseHead:SetAlpha(0)
    UIFrameFadeIn(VamooseHead, 0.3, 0, 1)

    -- Auto-hide after 5 seconds
    if VamooseHead.timer then VamooseHead.timer:Cancel() end
    if VamooseHead.hideTimer then VamooseHead.hideTimer:Cancel() end
    VamooseHead.timer = C_Timer.NewTimer(5, function()
        UIFrameFadeOut(VamooseHead, 0.5, 1, 0)
        VamooseHead.hideTimer = C_Timer.NewTimer(0.5, function() VamooseHead:Hide() end)
    end)
end

-- Expose for external use
HDG_Vamoose.PlayTalkingHead = PlayTalkingHead

-- ============================================================================
-- QUOTE TRACKING
-- Track last quote time and count per lumber type to avoid spam
-- ============================================================================

local lastQuoteTime = {}
local collectionsSinceQuote = {}
local nextQuoteThreshold = {}

-- Initialize tracking for a lumber type with chattiness setting
-- chattiness: 1-99, determines max random threshold for next quote
local function InitTracking(lumberId, chattiness)
    chattiness = chattiness or 30
    if not lastQuoteTime[lumberId] then
        lastQuoteTime[lumberId] = 0
        collectionsSinceQuote[lumberId] = 0
        -- Random threshold between 1 and chattiness value
        nextQuoteThreshold[lumberId] = math.random(1, math.max(1, chattiness))
    end
end

-- Get a random quote for a lumber type (delegates to HDG_CraftQuotes)
-- Returns: quote, speaker ("SHAMAN" or "GOBLIN")
function HDG_Vamoose.GetRandomQuote(lumberId)
    return HDG_CraftQuotes.GetRandomLumberQuote(lumberId)
end

-- Called when lumber is collected - may trigger a quote
-- chattiness: 1-99, determines frequency (higher = less frequent)
-- Returns: quote, speaker if one should be displayed, nil otherwise
function HDG_Vamoose.OnLumberCollected(lumberId, amount, chattiness)
    if not HDG_CraftQuotes.HasLumberQuotes(lumberId) then
        return nil, nil
    end

    chattiness = chattiness or 30
    InitTracking(lumberId, chattiness)

    -- Increment collection count
    collectionsSinceQuote[lumberId] = collectionsSinceQuote[lumberId] + (amount or 1)

    -- Check if we've hit the threshold
    if collectionsSinceQuote[lumberId] >= nextQuoteThreshold[lumberId] then
        -- Reset for next quote with new random threshold
        collectionsSinceQuote[lumberId] = 0
        nextQuoteThreshold[lumberId] = math.random(1, math.max(1, chattiness))
        lastQuoteTime[lumberId] = GetTime()

        return HDG_Vamoose.GetRandomQuote(lumberId)
    end

    return nil, nil
end

-- Display a quote using the talking head frame
-- If speaker is nil, defaults to SHAMAN
function HDG_Vamoose.DisplayQuote(quote, speaker)
    if not quote then return end
    PlayTalkingHead(quote, speaker or "SHAMAN")
end

-- Reset tracking (e.g., on session end)
function HDG_Vamoose.ResetTracking(lumberId)
    if lumberId then
        lastQuoteTime[lumberId] = nil
        collectionsSinceQuote[lumberId] = nil
        nextQuoteThreshold[lumberId] = nil
    else
        -- Reset all
        lastQuoteTime = {}
        collectionsSinceQuote = {}
        nextQuoteThreshold = {}
    end
end

-- Check if quotes are available for a lumber type (delegates to HDG_CraftQuotes)
function HDG_Vamoose.HasQuotes(lumberId)
    return HDG_CraftQuotes.HasLumberQuotes(lumberId)
end

-- Get quote count for a lumber type (delegates to HDG_CraftQuotes)
function HDG_Vamoose.GetQuoteCount(lumberId)
    return HDG_CraftQuotes.GetLumberQuoteCount(lumberId)
end

-- Get total lumber quote count (delegates to HDG_CraftQuotes)
function HDG_Vamoose.GetTotalQuoteCount()
    return HDG_CraftQuotes.GetTotalLumberQuoteCount()
end
