-- .add <id> <quant> <mod1> <mod2> <mod3> <mod4> <mod5>
-- .add 136716 1 1672 42 41 40 1
-- Add chat command interaction, pass through flags and aura_env
-- CHAT_MSG_LOOT
function(e, ...)
    if not WeakAurasSaved.CustomTrash then WeakAurasSaved.CustomTrash = {} end
    if not WeakAurasSaved.CustomTrash.ItemResearch then WeakAurasSaved.CustomTrash.ItemResearch = {} end
    if not WeakAurasSaved.CustomTrash.ItemResearch.Stage then WeakAurasSaved.CustomTrash.ItemResearch.Stage = 1 end
    if not WeakAurasSaved.CustomTrash.ItemResearch.ilvl then WeakAurasSaved.CustomTrash.ItemResearch.ilvl = 1672 end
    local daiTooltip = function(link)
        if aura_env.tooltip then
            aura_env.tooltip:SetOwner(UIParent, "ANCHOR_NONE")
            aura_env.tooltip:ClearLines();
            aura_env.tooltip:SetHyperlink(link)

            local itemData = {}
            itemData[#itemData + 1] = "New item"
            itemData[#itemData + 1] = WeakAurasSaved.CustomTrash.ItemResearch.Stage - 1 -- Add parameter info
            itemData[#itemData + 1] = "Left text"

            for i = 1, aura_env.tooltip:NumLines() do
                local field = "MyTooltipTextLeft" .. i
                if _G[field]:GetText() then
                    local text = _G[field]:GetText()
                    itemData[#itemData + 1] = text
                end
            end

            itemData[#itemData + 1] = "Right text"
            for i = 1, aura_env.tooltip:NumLines() do
                local field = "MyTooltipTextRight" .. i
                if _G[field]:GetText() then
                    local text = _G[field]:GetText()
                    itemData[#itemData + 1] = text
                end
            end
            itemData[#itemData + 1] = "End of Item"
            itemData[#itemData + 1] = ""
            WeakAurasSaved.CustomTrash.ItemResearch[#WeakAurasSaved.CustomTrash.ItemResearch + 1] = itemData;
            SendChatMessage(".add 152134 -1", "SAY");
        end
    end
    if e == "CHAT_MSG_LOOT" then
        local msg = ...
        local link = msg:match("\124c.+\124r")
        daiTooltip(link)
    end
end

--INIT
if not WeakAurasSaved.CustomTrash then WeakAurasSaved.CustomTrash = {} end
if not WeakAurasSaved.CustomTrash.ItemResearch then WeakAurasSaved.CustomTrash.ItemResearch = {} end
if not WeakAurasSaved.CustomTrash.ItemResearch.Stage then WeakAurasSaved.CustomTrash.ItemResearch.Stage = 1 end
if not WeakAurasSaved.CustomTrash.ItemResearch.ilvl then WeakAurasSaved.CustomTrash.ItemResearch.ilvl = 1672 end

aura_env.tooltip = CreateFrame("GameTooltip", "MyTooltip", UIParent, "GameTooltipTemplate")
local aura_env = aura_env
aura_env.daiItem = function()
    print("On stage " .. WeakAurasSaved.CustomTrash.ItemResearch.Stage)
    local message = ".add 152134 1 " .. WeakAurasSaved.CustomTrash.ItemResearch.ilvl .. " 42 41 40 " .. WeakAurasSaved.CustomTrash.ItemResearch.Stage
    SendChatMessage(message, "PARTY")
    WeakAurasSaved.CustomTrash.ItemResearch.Stage = WeakAurasSaved.CustomTrash.ItemResearch.Stage + 1
end

if WeakAurasSaved.CustomTrash.ItemResearch.Ticker and WeakAurasSaved.CustomTrash.ItemResearch.Ticker[0] then WeakAurasSaved.CustomTrash.ItemResearch.Ticker:Cancel(); WeakAurasSaved.CustomTrash.ItemResearch.Ticker = nil end
if not WeakAurasSaved.CustomTrash.ItemResearch.Ticker or WeakAurasSaved.CustomTrash.ItemResearch.Ticker[0] then WeakAurasSaved.CustomTrash.ItemResearch.Ticker = C_Timer.NewTicker(1, aura_env.daiItem) end