--***********************************************************
--**                    THE INDIE STONE                    **
--***********************************************************

require "ISUI/ISPanelJoypad"

ISRadialMenu = ISPanelJoypad:derive("ISRadialMenu")

function ISRadialMenu:instantiate()
	self.javaObject = RadialMenu.new(self.x, self.y, self.innerRadius, self.outerRadius)
	self.javaObject:setTable(self)
	self.javaObject:setAnchorLeft(self.anchorLeft)
	self.javaObject:setAnchorRight(self.anchorRight)
	self.javaObject:setAnchorTop(self.anchorTop)
	self.javaObject:setAnchorBottom(self.anchorBottom)

	for k,v in ipairs(self.slices) do
		self.javaObject:addSlice(v.text, v.texture)
	end
end

function ISRadialMenu:onMouseDown(x, y)
	self:undisplay()
	local sliceIndex = self.javaObject:getSliceIndexFromMouse(x, y)
	local command = self:getSliceCommand(sliceIndex + 1)
	if command and command[1] then
		command[1](command[2], command[3], command[4], command[5], command[6], command[7])
	end
end

function ISRadialMenu:onMouseDownOutside(x, y)
	self:undisplay()
end

function ISRadialMenu:clear()
	self.slices = {}
	if self.javaObject then
		self.javaObject:clear()
	end
end

function ISRadialMenu:addSlice(text, texture, command, arg1, arg2, arg3, arg4, arg5, arg6)
	local slice = {}
	slice.text = text
	slice.texture = texture
	slice.command = { command, arg1, arg2, arg3, arg4, arg5, arg6 }
	table.insert(self.slices, slice)
	if self.javaObject then
		self.javaObject:addSlice(text, texture)
	end
end

function ISRadialMenu:setSliceText(sliceIndex, text)
	if sliceIndex < 1 or sliceIndex > #self.slices then return end
	self.slices[sliceIndex].text = text
	if self.javaObject then
		self.javaObject:setSliceText(sliceIndex - 1, text)
	end
end

function ISRadialMenu:setSliceTexture(sliceIndex, texture)
	if sliceIndex < 1 or sliceIndex > #self.slices then return end
	self.slices[sliceIndex].texture = texture
	if self.javaObject then
		self.javaObject:setSliceText(sliceIndex - 1, texture)
	end
end

function ISRadialMenu:getSliceCommand(sliceIndex)
	if sliceIndex < 1 or sliceIndex > #self.slices then
		return nil
	end
	return self.slices[sliceIndex].command
end

function ISRadialMenu:onGainJoypadFocus(joypadData)
	ISPanelJoypad.onGainJoypadFocus(self, joypadData)
	self.javaObject:setJoypad(joypadData.id)
end

function ISRadialMenu:onJoypadDown(button, joypadData)
	if button == Joypad.BButton or button == Joypad.XButton then
		self:undisplay()
	end
end

function ISRadialMenu:onBackButtonReleased()
	self:undisplay()
	local sliceIndex = self.javaObject:getSliceIndexFromJoypad(self.joyfocus.id)
	local command = self:getSliceCommand(sliceIndex + 1)
	if command and command[1] then
		command[1](command[2], command[3], command[4], command[5], command[6], command[7])
	end
end

function ISRadialMenu:undisplay()
	self:removeFromUIManager()
	if self.joyfocus then
		self.joyfocus.focus = nil
	end
end

function ISRadialMenu:new(x, y, innerRadius, outerRadius, playerNum)
	local o = {}
	o = ISPanelJoypad:new(x, y, outerRadius * 2, outerRadius * 2)
	setmetatable(o, self)
	self.__index = self
	self.innerRadius = innerRadius
	self.outerRadius = outerRadius
	self.playerNum = playerNum
	self.slices = {}
	return o
end

