local _,ns = ...
local L = ns.L


local myCurrentInventory = {}


function ns.resetMyCurrentInventory()
    wipe(myCurrentInventory)
end

function ns.setMyCurrentInventory()
    wipe(myCurrentInventory)
    for b = 0, NUM_TOTAL_BAG_FRAMES do
        for i = 1, C_Container.GetContainerNumSlots(b) do
            local bag,slot = b,i
            local info = C_Container.GetContainerItemInfo(b,i)
            local itemID = info and info["itemID"]
            local itemCount = info and info["stackCount"]
            if itemID and itemCount then
                if myCurrentInventory[itemID] then
                    myCurrentInventory[itemID] = myCurrentInventory[itemID] + itemCount
                else
                    myCurrentInventory[itemID] = itemCount
                end
            end
        end
    end
end

-- local function printMyCurrentInventory()
--     for itemID, count in pairs(myCurrentInventory) do
--         print("ItemID:", itemID, "Cantidad:", count)
--     end
-- end
-- _G.printInv = printMyCurrentInventory

function ns.farmedItemID(itemID, totalItemID)
    local owned = myCurrentInventory[itemID] or 0
    local remaining = totalItemID - owned
    if remaining < 0 then remaining = 0 end
    return remaining
end

-- _G.testAS = ns.resetMyCurrentInventory

local totalPrice = 0
function ns.initCalcFarmPrice ()
    totalPrice = 0
    -- print("-init")
end

function ns.addCalcFarmPrice (itemID, cantidad)
    -- print(" -->>", itemID, ":", ns.getItemCachedPrice(itemID))
    local precioCalculado = ns.getItemCachedPrice(itemID) or 0
    totalPrice = totalPrice + (precioCalculado * cantidad)
    -- print("- itemID, cantidad, total", itemID, cantidad, (precioCalculado * cantidad))
end

function ns.getTotalFarmPrice()
    local returnTotalPrice = math.floor(totalPrice / 10000)
    return returnTotalPrice
end
