BagFu = AceLibrary("AceAddon-2.0"):new("FuBarPlugin-2.0", "AceDB-2.0", "AceEvent-2.0", "AceConsole-2.0")
BagFu:RegisterDB("BagFuDB")
BagFu:RegisterDefaults('profile', {
	showDepletion = false,
	includeProfession = true,
	includeAmmo = false,
})

BagFu.hasIcon = true
BagFu.cannotDetachTooltip = true
BagFu.defaultPosition = "RIGHT"

local L = AceLibrary("AceLocale-2.0"):new("FuBar_BagFu")

local Tablet = AceLibrary("Tablet-2.0")
local Crayon = AceLibrary("Crayon-2.0")

local options = {
	type = "group",
	args = {
		ammobags = {
			type = "toggle",
			name = L["Ammo/Soul Bags"],
			desc = L["Include ammo/soul bags"],
			get = function() return BagFu.db.profile.includeAmmo end,
			set = function()
				BagFu.db.profile.includeAmmo = not BagFu.db.profile.includeAmmo
				BagFu:Update()
			end,
		},
		professionbags = {
			type = "toggle",
			name = L["Profession Bags"],
			desc = L["Include profession bags"],
			get = function() return BagFu.db.profile.includeProfession end,
			set = function()
				BagFu.db.profile.includeProfession = not BagFu.db.profile.includeProfession
				BagFu:Update()
			end,
		},
		depletion = {
			type = "toggle",
			name = L["Bag Depletion"],
			desc = L["Show depletion of bags"],
			get = function() return BagFu.db.profile.showDepletion end,
			set = function()
				BagFu.db.profile.showDepletion = not BagFu.db.profile.showDepletion
				BagFu:Update()
			end,
		}
	}
}
BagFu.OnMenuRequest = options
BagFu:RegisterChatCommand({ "/bagfu" }, options)

function BagFu:OnEnable()
	self:RegisterEvent("BAG_UPDATE", "Update")
end

function BagFu:OnTextUpdate()
	local totalSlots = 0
	local takenSlots = 0
	for i = 0, 4 do
		local usable = true
		if i >= 1 then
			local link = GetInventoryItemLink("player", ContainerIDToInventoryID(i))
			if link ~= nil then
				local itemId = tonumber(string.gsub(link, "|cff%x%x%x%x%x%x|Hitem:(%d+):%d+:%d+:%d+|h.*", "%1") or 0)
				local _,_,_,_,_,subtype = GetItemInfo(itemId)
				if not self.db.profile.includeAmmo and (subtype == L["Soul Bag"] or subtype == L["Ammo Pouch"] or subtype == L["Quiver"]) then
					usable = false
				elseif not self.db.profile.includeProfession and (subtype == L["Enchanting Bag"] or subtype == L["Herb Bag"] or subtype == L["Engineering Bag"]) then
					usable = false
				end
			end
		end
		if usable then
			local size = GetContainerNumSlots(i)
			
			if size ~= nil and size > 0 then
				totalSlots = totalSlots + size
				for slot = 1, size do
					if GetContainerItemInfo(i, slot) then
						takenSlots = takenSlots + 1
					end
				end
			end
		end
	end
	
	local color = Crayon:GetThresholdHexColor((totalSlots - takenSlots) / totalSlots)
	
	if self.db.profile.showDepletion then
		takenSlots = totalSlots - takenSlots
	end
	
	self:SetText(format("|cff%s%d/%d|r", color, takenSlots, totalSlots))
end

function BagFu:OnTooltipUpdate()
	Tablet:SetHint(L["Click to open your bags"])
end

function BagFu:OnClick()
	if not ContainerFrame1:IsShown() then
		for i = 1, 4 do
			if getglobal("ContainerFrame" .. (i + 1)):IsShown() then
				getglobal("ContainerFrame" .. (i + 1)):Hide()
			end
		end
		ToggleBackpack()
		if ContainerFrame1:IsShown() then
			for i = 1, 4 do
				local link = GetInventoryItemLink("player", ContainerIDToInventoryID(i))
				if link ~= nil then
					local itemId = tonumber(string.gsub(link, "|cff%x%x%x%x%x%x|Hitem:(%d+):%d+:%d+:%d+|h.*", "%1") or 0)
					local _,_,_,_,_,subtype = GetItemInfo(itemId)
					local usable = true
					if not self.db.profile.includeAmmo and (subtype == L["Soul Bag"] or subtype == L["Ammo Pouch"] or subtype == L["Quiver"]) then
						usable = false
					elseif not self.db.profile.includeProfession and (subtype == L["Enchanting Bag"] or subtype == L["Herb Bag"] or subtype == L["Engineering Bag"]) then
						usable = false
					end
					if usable then
						ToggleBag(i)
					end
				end
			end
		end
	else
		for i = 0, 4 do
			if getglobal("ContainerFrame" .. (i + 1)):IsShown() then
				getglobal("ContainerFrame" .. (i + 1)):Hide()
			end
		end
	end
end
