GuildBankCycler = ZO_Object:Subclass()

function GuildBankCycler:New()
	return ZO_Object.New(self)
end

GUILDBANKCYCLER = GuildBankCycler:New()

GuildBankCycler.guildCount = 0
GuildBankCycler.dropDownLookup = {}

local currentSelector = nil

function GuildBankCycler:SelectorPushed(...)
	local eventId, a, b = ...
	if not (a == b and a == 4) then return end
	if ZO_SelectGuildBankDialogGuild:IsHidden() then return end

	local dropDown = ZO_SelectGuildBankDialogGuild.m_comboBox
	local entries = dropDown:GetItems()

	self.guildCount = #entries
	for _,v in pairs(entries) do
		self.dropDownLookup[v.guildId] = v
	end

	currentSelector = GetSelectedGuildBankId()
end

function GuildBankCycler:ChangeGuildSelected(control, delta)
	if delta < 0 then
		if currentSelector < self.guildCount then
			currentSelector = currentSelector + 1
		else
			if currentSelector == self.guildCount then
				currentSelector = 1
			end
		end
	else
		if currentSelector > 1 then
			currentSelector = currentSelector - 1
		else
			if currentSelector == 1 then
				currentSelector = self.guildCount
			end
		end
	end
	local entry = self.dropDownLookup[currentSelector]
	entry:callback(entry, entry)
end



local function GuildBankCyclerLoaded(eventCode, addOnName)

	if(addOnName ~= "GuildBankCycler") then
        return
    end
	ZO_PreHookHandler(ZO_SelectGuildBankDialogModalUnderlay, "OnMouseWheel", function(...) GuildBankCycler:ChangeGuildSelected(...) end)
	EVENT_MANAGER:RegisterForEvent("GuildBankCyclerGuildSelector", EVENT_ACTION_LAYER_PUSHED, function(...) GuildBankCycler:SelectorPushed(...) end)
end

EVENT_MANAGER:RegisterForEvent("GuildBankCyclerLoaded", EVENT_ADD_ON_LOADED, GuildBankCyclerLoaded)