local addonName,BM=...

BM.queueActive=false

local function ResetActionButtons()
    BM.queueActive=false

    if BM.TakeGoldButton and BM.TakeGoldButton.label then
        BM.TakeGoldButton:Enable()
        BM.TakeGoldButton.label:SetText(BM:L("take_gold"))
    end

    if BM.TakeAllButton and BM.TakeAllButton.label then
        BM.TakeAllButton:Enable()
        BM.TakeAllButton.label:SetText(BM:L("take_all"))
    end

    if BM.TakeSelectedButton and BM.TakeSelectedButton.label then
        BM.TakeSelectedButton:Enable()
        BM.TakeSelectedButton.label:SetText(BM:L("take_selected"))
    end

    if BM.UpdateActionTexts then
        BM:UpdateActionTexts()
    end
end

function BM:TakeSingleMail(index)
    if BM.queueActive or not index then
        return
    end

    local _, _, _, _, money, _, _, hasItem = GetInboxHeaderInfo(index)
    if not ((money and money > 0) or (hasItem and hasItem > 0)) then
        BM:SetStatus(BM:L("selected_mail_empty"), unpack(BM.Theme.mutetext))
        return
    end

    BM.queueActive=true

    if BM.TakeSelectedButton then
        BM.TakeSelectedButton:Disable()
        if BM.TakeSelectedButton.label then
            BM.TakeSelectedButton.label:SetText(BM:L("collecting"))
        end
    end

    if BM.TakeGoldButton then
        BM.TakeGoldButton:Disable()
    end

    if BM.TakeAllButton then
        BM.TakeAllButton:Disable()
    end

    AutoLootMailItem(index)
    TakeInboxMoney(index)

    C_Timer.After(.5,function()
        BM.selectedMailIndex = nil
        ResetActionButtons()
        BM:RefreshInbox()
    end)
end

function BM:CreateTakeAll()
    local actionBar = CreateFrame("Frame", nil, BM.MainFrame)
    actionBar:SetAllPoints(BM.MainFrame)

    local buttonAnchor = CreateFrame("Frame", nil, actionBar)
    buttonAnchor:SetPoint("BOTTOM", BM.MainFrame, "BOTTOM", 126, 58)
    buttonAnchor:SetSize(620, 32)

    local selectedBtn = BM:CreateFlatActionButton(buttonAnchor, 170, 32, "")
    selectedBtn:SetPoint("LEFT", buttonAnchor, "LEFT", 0, 0)
    selectedBtn:SetScript("OnClick", function()
        if BM.selectedMailIndex then
            BM:TakeSingleMail(BM.selectedMailIndex)
        end
    end)

    local goldBtn = BM:CreateFlatActionButton(buttonAnchor, 170, 32, "")
    goldBtn:SetPoint("LEFT", selectedBtn, "RIGHT", 12, 0)
    goldBtn:SetScript("OnClick", function()
        if BM.queueActive then return end
        BM.queueActive=true
        goldBtn:Disable()
        if goldBtn.label then goldBtn.label:SetText(BM:L("collecting")) end
        BM:ProcessGoldOnly()
    end)

    local btn=BM:CreateFlatActionButton(buttonAnchor, 170, 32, "")
    btn:SetPoint("LEFT", goldBtn, "RIGHT", 12, 0)
    btn:SetScript("OnClick",function()
        if BM.queueActive then return end
        BM.queueActive=true
        btn:Disable()
        if btn.label then btn.label:SetText(BM:L("collecting")) end
        BM:ProcessMail()
    end)

    local refreshBtn = BM:CreateFlatActionButton(buttonAnchor, 132, 32, "")
    refreshBtn:SetPoint("LEFT", btn, "RIGHT", 12, 0)
    refreshBtn:SetScript("OnClick", function()
        CheckInbox()
        BM:SetStatus(BM:L("refreshing"), 0.13, 0.72, 1)
        C_Timer.After(.3, function()
            BM:RefreshInbox()
        end)
    end)

    BM.TakeAllButton = btn
    BM.TakeGoldButton = goldBtn
    BM.TakeSelectedButton = selectedBtn
    BM.RefreshButton = refreshBtn
    BM.ActionButtonAnchor = buttonAnchor
    BM.ActionBar = actionBar
    BM:UpdateActionTexts()
end

function BM:UpdateActionTexts()
    if BM.TakeAllButton and BM.TakeAllButton.label and not BM.queueActive then BM.TakeAllButton.label:SetText(BM:L("take_all")) end
    if BM.TakeGoldButton and BM.TakeGoldButton.label and not BM.queueActive then BM.TakeGoldButton.label:SetText(BM:L("take_gold")) end
    if BM.TakeSelectedButton and BM.TakeSelectedButton.label and not BM.queueActive then BM.TakeSelectedButton.label:SetText(BM:L("take_selected")) end
    if BM.RefreshButton and BM.RefreshButton.label then BM.RefreshButton.label:SetText(BM:L("refresh")) end
    if BM.TakeSelectedButton then
        if BM.queueActive then
            BM.TakeSelectedButton:Disable()
        else
            BM.TakeSelectedButton:SetEnabled(BM.selectedMailIndex ~= nil)
        end
    end
end

function BM:ProcessMail()
    local visible, totalItems = GetInboxNumItems()
    local total = math.max(visible or 0, totalItems or 0)

    for i=total,1,-1 do
        AutoLootMailItem(i)
        TakeInboxMoney(i)
    end

    C_Timer.After(.5,function()
        BM.selectedMailIndex = nil
        ResetActionButtons()
        BM:RefreshInbox()
    end)
end

function BM:ProcessGoldOnly()
    local visible, totalItems = GetInboxNumItems()
    local total = math.max(visible or 0, totalItems or 0)

    for i=total,1,-1 do
        TakeInboxMoney(i)
    end

    C_Timer.After(.5,function()
        ResetActionButtons()
        BM:RefreshInbox()
    end)
end
