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

local ASjunkTable = {}

-- Note for me:
-- each time you change a item from list, call ns.updateUIALLBags() to update the list on the inventory bags.


--Note for developers:
-- Maybe in the future i will add an API for you. You can mail/pm me to do it if you are interested.
-- Basically there are 3 list:
--    -whiteList: for the items you will never want to be sold: AutoScrap_CharSettings.noVenderList | made on GUI/AutoScrapNoVender.lua
--    -AutoScrap list: the items that the autoscrap detect as scrap. This start always as {}, is not saved on anyplace, just a cache here on ASjunkTable
--    -blackList: The items that the user has flagged as junk manually to sell them always. AutoScrap_CharSettings.blackList

--- ####### AutoScrap list functions ---

function ns.isJunkAS(itemID)
    if not itemID then
        error("ItemID es nulo",2)
        return
    end
    if ASjunkTable[itemID] then
        return true
    end
    return false --nil or false
end

function ns.toggleJunkAS(itemID, itemLink)
    if not itemID and not itemLink then
        error("ItemID o ItemLink es nulo",2)
        return
    end
    if not ns.isJunkBL(itemID) and not AutoScrap_CharSettings.noVenderList[itemID] then -- cached avoid to being called after it is on junkBL the first time. No need to extra code it from AutoScrap.lua
        if ASjunkTable[itemID] then
            ASjunkTable[itemID] = nil
            print (itemLink, L[" Removed from AutoScrap"])
        else
            ASjunkTable[itemID] = true
            print (itemLink, L[" Added to AutoScrap"])
        end
    end
    --print ("fired")
    ns.updateUIALLBags()
end

function ns.limpiarASjunkTable()
    ASjunkTable = {}
    ns.updateUIALLBags()
end

--- ####### BLACKLIST functions ---

function ns.isJunkBL(itemID)
    if not itemID then
        error("ItemID es nulo",2)
        return
    end
    if AutoScrap_CharSettings.blackList[itemID] then
        return true
    end
    return false --nil or false
end

function ns.toggleJunkBL(itemID,itemLink) --itemlink is required because getting it from itemID can cause itemlink is not cached.
    if not itemID or not itemLink then
        error("ItemID o ItemLink es nulo",2)
        return
    end
    if AutoScrap_CharSettings.noVenderList[itemID] then
        ns.removeItemFromWhiteList(itemID,itemLink)
    elseif ns.isJunkAS(itemID) then
        ns.addItemToWhiteList(itemID,itemLink)
--        ns.toggleJunkAS(itemID,itemLink) lo saca addItemToWhiteList
    else
        if AutoScrap_CharSettings.blackList[itemID] then
            AutoScrap_CharSettings.blackList[itemID] = nil
            print (itemLink, L[" Removed from BlackList"])
        else
            AutoScrap_CharSettings.blackList[itemID] = true
            print (itemLink, L[" Added to BlackList"])
        end
    end
    ns.updateUIALLBags()
    ns.limpiarCacheAS()
    ns.scrapIventory()
end

function ns.limpiarBLjunkTable()
    AutoScrap_CharSettings.blackList = {}
    ns.updateUIALLBags()
    ns.scrapIventory()
end

