local compost = CompostLib:GetInstance('compost-1')
local dewdrop = DewdropLib:GetInstance('1.0')
local tablet = TabletLib:GetInstance('1.0')

BagFu = FuBarPlugin:GetInstance("1.2"):new({
	name          = BagFuLocals.NAME,
	description   = BagFuLocals.DESCRIPTION,
	version       = "1.1." .. string.sub("$Revision: 1551 $", 12, -3),
	releaseDate   = string.sub("$Date: 2006-05-06 23:08:11 -0500 (Sat, 06 May 2006) $", 8, 17),
	aceCompatible = 103,
	author        = "ckknight",
	email         = "ckknight@gmail.com",
	website       = "http://ckknight.wowinterface.com/",
	category      = "inventory",
	db            = AceDatabase:new("BagFuDB"),
	defaults      = {
		showDepletion = false,
		includeProfession = true,
		includeAmmo = false,
	},
	cmd           = AceChatCmd:new(BagFuLocals.COMMANDS, BagFuLocals.CMD_OPTIONS),
	loc           = BagFuLocals,
	hasIcon       = true,
	cannotDetachTooltip = TRUE,
})
	-- Methods
function BagFu:IsShowingDepletion()
	return self.data.showDepletion
end

function BagFu:ToggleShowingDepletion(loud)
	self.data.showDepletion = not self.data.showDepletion
	if loud then
		self.cmd:status(self.loc.ARGUMENT_DEPLETION, self.data.showDepletion and 1 or 0,
FuBarLocals.MAP_ONOFF)
	end
	self:Update()
	return self.data.showDepletion
end

function BagFu:IsIncludingAmmo()
	return self.data.includeAmmo
end

function BagFu:ToggleIncludingAmmo(loud)
	self.data.includeAmmo = not self.data.includeAmmo
	if loud then
		self.cmd:status(self.loc.ARGUMENT_AMMO, self.data.includeAmmo and 1 or 0, FuBarLocals.MAP_ONOFF)
	end
	self:Update()
	return self.data.includeAmmo
end

function BagFu:IsIncludingProfession()
	return self.data.includeProfession
end

function BagFu:ToggleIncludingProfession(loud)
	self.data.includeProfession = not self.data.includeProfession
	if loud then
		self.cmd:status(self.loc.ARGUMENT_PROFESSION, self.data.includeProfession and 1 or 0, FuBarLocals.MAP_ONOFF)
	end
	self:Update()
	return self.data.includeProfession
end

function BagFu:Enable()
	self:RegisterEvent("UNIT_MODEL_CHANGED", "Update")
	self:RegisterEvent("PLAYER_ENTERING_WORLD", "OnPlayerEnteringWorld")
	self:RegisterEvent("PLAYER_LEAVING_WORLD", "OnPlayerLeavingWorld")
	self:RegisterEvent("BAG_UPDATE", "Update")
	self:RegisterEvent("ITEM_LOCK_CHANGED", "Update")
end

function BagFu:Report()
	local report = compost:Acquire(
		compost:AcquireHash(
			'text', self.loc.ARGUMENT_DEPLETION,
			'val', self:IsShowingDepletion() and 1 or 0,
			'map', FuBarLocals.MAP_ONOFF
		),
		compost:AcquireHash(
			'text', self.loc.ARGUMENT_PROFESSION,
			'val', self:IsIncludingProfession() and 1 or 0,
			'map', FuBarLocals.MAP_ONOFF
		),
		compost:AcquireHash(
			'text', self.loc.ARGUMENT_AMMO,
			'val', self:IsIncludingAmmo() and 1 or 0,
			'map', FuBarLocals.MAP_ONOFF
		)
	)
	self.cmd:report(report)
	compost:Reclaim(report, 1)
end

function BagFu:MenuSettings(level, value)
	if level == 1 then
		dewdrop:AddLine(
			'text', self.loc.MENU_INCLUDE_AMMO_BAGS,
			'arg1', self,
			'func', "ToggleIncludingAmmo",
			'checked', self:IsIncludingAmmo()
		)
		
		dewdrop:AddLine(
			'text', self.loc.MENU_INCLUDE_PROFESSION_BAGS,
			'arg1', self,
			'func', "ToggleIncludingProfession",
			'checked', self:IsIncludingProfession()
		)
		
		dewdrop:AddLine(
			'text', self.loc.MENU_SHOW_DEPLETION_OF_BAGS,
			'arg1', self,
			'func', "ToggleShowingDepletion",
			'checked', self:IsShowingDepletion()
		)
	end
end

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

function BagFu:OnPlayerLeavingWorld()
	self:UnregisterEvent("BAG_UPDATE")
	self:UnregisterEvent("ITEM_LOCK_CHANGED")
end

function BagFu:UpdateText()
	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:IsIncludingAmmo() and (subtype == self.loc.TEXT_SOUL_BAG or subtype == self.loc.TEXT_AMMO_POUCH or subtype == self.loc.TEXT_QUIVER) then
					usable = FALSE
				elseif not self:IsIncludingProfession() and (subtype == self.loc.TEXT_ENCHANTING_BAG or subtype == self.loc.TEXT_HERB_BAG or subtype == self.loc.TEXT_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 = FuBarUtils.GetThresholdHexColor((totalSlots - takenSlots) / totalSlots)
	
	if self:IsShowingDepletion() then
		takenSlots = totalSlots - takenSlots
	end
	
	self:SetText(format("|cff%s%d/%d|r", color, takenSlots, totalSlots))
end

function BagFu:UpdateTooltip()
	tablet:SetHint(self.loc.TEXT_HINT)
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:IsIncludingAmmo() and (subtype == self.loc.TEXT_SOUL_BAG or subtype == self.loc.TEXT_AMMO_POUCH or subtype == self.loc.TEXT_QUIVER) then
						usable = FALSE
					elseif not self:IsIncludingProfession() and (subtype == self.loc.TEXT_ENCHANTING_BAG or subtype == self.loc.TEXT_HERB_BAG or subtype == self.loc.TEXT_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

BagFu:RegisterForLoad()