require 'scripts/core/core.lua'

--------------------------------------------------------------------------------
-- Controller Object script
--------------------------------------------------------------------------------

Controller = class()

-- Callback when object is added to the world
function Controller:start()
	print('Controller:start()')
	self.n64Node = WorldNode_getChildByName(self.worldNode, 'n64')
	if self.n64Node then
		self.buttonsNode = WorldNode_getChildByName(self.n64Node, 'buttons')
		self.spriteNode = WorldNode_getChildByName(self.n64Node, 'controller')
		self.circleNode = WorldNode_getChildByName(self.n64Node, 'circle')
		self.circleSprite = WorldNode_getComponentByTypeName(self.circleNode, 'sheet_sprite')
		self.tipNode = WorldNode_getChildByName(self.circleNode, 'tip')
		self.vcmenuNode = WorldNode_getChildByName(self.worldNode, 'vc_menu')
		WorldNode_setEnabled(self.tipNode, false)
		self.buttons = {}
		local i = 1
		while true do
			local button = WorldNode_getChildByName(self.buttonsNode, tostring(i))
			if not button then button = WorldNode_getChildByName(self.vcmenuNode, tostring(i)) end
			if not button then break end
			self.buttons[i] = button
			WorldNode_setEnabled(self.buttons[i], false)
			i = i + 1
		end
	else	
		self.buttonsNode = WorldNode_getChildByName(self.worldNode, 'buttons')
		self.spriteNode = WorldNode_getChildByName(self.worldNode, 'controller')
		self.circleNode = WorldNode_getChildByName(self.worldNode, 'circle')
		self.circleSprite = WorldNode_getComponentByTypeName(self.circleNode, 'sheet_sprite')
		self.tipNode = WorldNode_getChildByName(self.circleNode, 'tip')
		self.buttons = {}
		local i = 1
		while true do
			local button = WorldNode_getChildByName(self.buttonsNode, tostring(i))
			if not button then break end
			self.buttons[i] = button
			WorldNode_setEnabled(self.buttons[i], false)
			i = i + 1
		end
	end
	self.buttonSprite = nil
	self.current = 1
	self.target = 1
	self.tipPositions = {{0, -80}, {-122, 0}}
	self.timer = 0
	self.timer2 = 0
	self.circlesDisabled = false
	self.idleTime = 2.0
end

-- Callback when object is removed from the world
function Controller:stop()
end

-- Callback called every frames
function Controller:update(dt)

	if not self.circlesDisabled then
		self.timer2 = self.timer2 + dt
		if self.editingDisabled then
			self.timer = 0
			WorldNode_setEnabled(self.circleNode, false)
			WorldNode_setEnabled(self.tipNode, false)
			for i = 1, #self.buttons do 
				WorldNode_setEnabled(self.buttons[i], i == self.target)
			end
			if self.buttonSprite ~= WorldNode_getComponentByTypeName(self.buttons[self.target], 'sheet_sprite') then
				self.buttonSprite = WorldNode_getComponentByTypeName(self.buttons[self.target], 'sheet_sprite')
				VisualComponent_setAlpha(self.buttonSprite, 0)
				self.timer2 = 0
			else
				VisualComponent_setAlpha(self.buttonSprite, clamp(self.timer2*5, 0, 1))
			end

		else
			self.timer = self.timer + dt
			WorldNode_setEnabled(self.circleNode, true)
			for i = 1, #self.buttons do 
				WorldNode_setEnabled(self.buttons[i], false)
			end

			if self.current ~= self.target then
				VisualComponent_setAlpha(self.circleSprite, 0)
				self.timer2 = 0
			else
				VisualComponent_setAlpha(self.circleSprite, clamp(self.timer2*5, 0, 1) )
			end
			
			local tarX, tarY = WorldNode_getWorldPosition(self.buttons[self.target])
			if self.changed then
				self.timer = 0
				self.changed = false
			end
			
			WorldNode_setWorldPosition(self.circleNode, tarX, tarY)

			if self.n64Node and (self.target == 13 or self.target == 11 ) then
				WorldNode_setLocalPosition(self.tipNode, self.tipPositions[2][1], self.tipPositions[2][2])
			else
				WorldNode_setLocalPosition(self.tipNode, self.tipPositions[1][1], self.tipPositions[1][2])
			end
			WorldNode_setEnabled(self.tipNode, self.timer > self.idleTime)
			self.current = self.target
		end
	else 
		self.timer = 0
		for i = 1, #self.buttons do 
			WorldNode_setEnabled(self.buttons[i], false)
		end
		self.buttonSprite = nil
		WorldNode_setEnabled(self.circleNode, false)
	end
end

-- Callback when object starts colliding with another
function Controller:beginCollision(componentA, componentB)
end

-- Callback when object finishes colliding with another
function Controller:endCollision(componentA, componentB)
end

-- Callback when object is a button and is pressed
function Controller:onPressed()
end

-- Callback when object is a button and is released
function Controller:onReleased()
end
