--Idea:
--Get loot from everyone
--Put loot in table sorted with everyone
--If AH visit get prices of boe items? and DE mats
--In item table add value in vendor, AH and DE
--Add option to export all to excel
--Add timer (toggleable?) with a button and macro
--Add option to mark significant items, idk how maybe use macro maybe some sort of button
--Use only for guildies
--Maybe reminder to turn timer on
--Maybe individual member timer
--Sort table by member > item > iteminfo
--Where member holds time and key is name
--Maybe use time as start and endtime
--Maybe establish comms with other weakauras to make sure times are synced
--Add gold / hour !!
--Maybe add split gold f(totalgold, membertimespent)?
--Google sheets uses tab for column seperation and newline for row seperation !!
--Make into addon not weakaura

--CHAT_MSG_LOOT AUCTION_HOUSE_SHOW AUCTION_ITEM_LIST_UPDATE AH_READY
function(e, msg)
    if e == "CHAT_MSG_LOOT" then
        if msg then
            local who = msg:match("%w+")
            if who == "You" then who = UnitName("player") end
            if who:match("(-)") then who:match("(.+)-%w*") end
            local howmuch = msg:match("(x%d+).$")
            local link
            for itemLink in msg:gmatch("|%x+|Hitem:.-|h.-|h|r") do
                link = itemLink
            end
            local itemName, _, itemQuality, _, _, itemType = GetItemInfo(link)
            local itemLevel = GetDetailedItemLevelInfo(link)
            if itemQuality > 1 then
                if not aura_env.lootTable[who] then aura_env.lootTable[who] = {["items"] = {}} end
                if not aura_env.lootTable[who].items[itemName] then
                    aura_env.lootTable[who].items[itemName] = {
                        ["name"] = itemName,
                        ["vendor"] = select(11, GetItemInfo(link)),
                        ["quantity"] = howmuch or 1,
                    }
                    --Handles DE price prediction
                    --if itemType == "Armor" or itemType == "Weapon" then
                    --    print("itemLevel", itemLevel)
                    --    if itemLevel >= 45 and itemLevel <= 55  then
                    --        local price = 0
                    --        for k,v in pairs(WeakAurasSaved.LootLog.DEStats[1]) do
                    --            price = price + (v * WeakAurasSaved.LootLog.AHPrices[k])
                    --        end
                    --        price = price * (howmuch or 1)
                    --        aura_env.lootTable[who].items[itemName].DE = price
                    --        DevTools_Dump(aura_env.lootTable)
                    --    elseif itemLevel >= 56 then
                    --        local price = 0
                    --        for k,v in pairs(WeakAurasSaved.LootLog.DEStats[2]) do
                    --            price = price + (v * WeakAurasSaved.LootLog.AHPrices[k])
                    --        end
                    --        price = price * (howmuch or 1)
                    --        aura_env.lootTable[who].items[itemName].DE = price
                    --        DevTools_Dump(aura_env.lootTable)
                    --    elseif itemLevel > 58 and itemLevel < 77 and itemQuality == 4 then
                    --        local price = 0
                    --        for k,v in pairs(WeakAurasSaved.LootLog.DEStats[3]) do
                    --            price = price + (v * WeakAurasSaved.LootLog.AHPrices[k])
                    --        end
                    --        price = price * (howmuch or 1)
                    --        aura_env.lootTable[who].items[itemName].DE = price
                    --        DevTools_Dump(aura_env.lootTable)
                    --    end
                    --end
                    if WeakAurasSaved.LootLog.Notables[itemName] then
                        aura_env.lootTable[who].items[itemName].AH = WeakAurasSaved.LootLog.Notables[itemName]
                    end
                else
                    aura_env.lootTable[who].items[itemName].quantity = aura_env.lootTable[who].items[itemName].quantity + 1
                end
            end
            DevTools_Dump(aura_env.lootTable)
            --DevTools_Dump(WeakAurasSaved.LootLog)
        end
    elseif e == "AUCTION_HOUSE_SHOW" then
        DevTools_Dump(WeakAurasSaved.LootLog.AHPrices)
        if aura_env.i == 1 then
            QueryAuctionItems("Illusion Dust")
            print("Querry", "Illusion Dust")
        end
        --Querry update that fires item list update then get item info from like a page or two and get median value if the spread isn't too much
    elseif e == "AUCTION_ITEM_LIST_UPDATE" then
        --Get auction info
        if not aura_env.timer then
            SortAuctionSetSort("list", "buyout")
            SortAuctionApplySort("list")
        end
        if aura_env.timer then aura_env.timer:Cancel(); aura_env.timer = nil end
        if not aura_env.timer then aura_env.timer = C_Timer.NewTimer(1, function() WeakAuras.ScanEvents("AH_READY") end) end
    elseif e == "AH_READY" then
        if aura_env.timer then aura_env.timer = nil end
        local masterName, masterID
        if aura_env.i == 1 then
            masterName = "Illusion Dust"
            masterID = "ID"
        elseif aura_env.i == 2 then
            masterName = "Dream Dust"
            masterID = "DD"
        elseif aura_env.i == 3 then
            masterName = "Small Brillaint Shard"
            masterID = "SBS"
        elseif aura_env.i == 4 then
            masterName = "Large Brilliant Shard"
            masterID = "LBS"
        elseif aura_env.i == 5 then
            masterName = "Lesser Eternal Essence"
            masterID = "LEE"
        elseif aura_env.i == 6 then
            masterName = "Greater Eternal Essence"
            masterID = "GEE"
        end
        for i = 1, GetNumAuctionItems("list") do
            local name, _, count = GetAuctionItemInfo("list", i)
            local buyoutPrice = select(10, GetAuctionItemInfo("list", i))
            if name == masterName then
                local pricePerEach = buyoutPrice / count
                if WeakAurasSaved.LootLog.AHPrices[masterID] == 0 then
                    WeakAurasSaved.LootLog.AHPrices[masterID] = pricePerEach 
                else
                    WeakAurasSaved.LootLog.AHPrices[masterID] = (WeakAurasSaved.LootLog.AHPrices[masterID] + pricePerEach) / 2
                end
            end
        end
        aura_env.i = aura_env.i + 1
        if WeakAurasSaved.LootLog.AHPrices[masterID] then WeakAurasSaved.LootLog.AHPrices[masterID] = math.floor(WeakAurasSaved.LootLog.AHPrices[masterID]) end
        --DevTools_Dump(WeakAurasSaved.LootLog.AHPrices)
        if aura_env.i == 1 then
            masterName = "Illusion Dust"
            masterID = "ID"
        elseif aura_env.i == 2 then
            masterName = "Dream Dust"
            masterID = "DD"
        elseif aura_env.i == 3 then
            masterName = "Small Brillaint Shard"
            masterID = "SBS"
        elseif aura_env.i == 4 then
            masterName = "Large Brilliant Shard"
            masterID = "LBS"
        elseif aura_env.i == 5 then
            masterName = "Lesser Eternal Essence"
            masterID = "LEE"
        elseif aura_env.i == 6 then
            masterName = "Greater Eternal Essence"
            masterID = "GEE"
        elseif aura_env.i > 6 then
            print("AH Scan complete")
            DevTools_Dump(WeakAurasSaved.LootLog.AHPrices)
            masterName = nil
            masterID = nil
            aura_env.i = 1
        end
        if masterName then 
            QueryAuctionItems(masterName) 
            print("Querry", masterName)
        end
    end
end

--INIT
aura_env.i = 1
aura_env.lootTable = {}
if not WeakAurasSaved.LootLog then WeakAurasSaved.LootLog = {} end
if not WeakAurasSaved.LootLog.AHPrices then 
	WeakAurasSaved.LootLog.AHPrices = {
		["ID"] = 0,
		["DD"] = 0,
		["SBS"] = 0,
		["LBS"] = 0,
		["LEE"] = 0,
		["GEE"] = 0,
	} 
end
if not WeakAurasSaved.LootLog.DEStats then 
	WeakAurasSaved.LootLog.DEStats = {
		--<45,55>
		[1] = {
			["DD"] = 0,
			["SBS"] = 0,
			["LBS"] = 0,
			["LEE"] = 0,
			["GEE"] = 0,
			["GME"] = 0,
		},
		--<56,65>
		[2] = {
			["ID"] = 2.8699,
			["LBS"] = 0.018,
			["GEE"] = 0.4,
		},
		--Purple items between 59 and 78
		[3] = {
			["LBS"] = 0,
		},
	} 
end
if not WeakAurasSaved.LootLog.Notables then WeakAurasSaved.LootLog.Notables = {} end