require 'scripts/core/core.lua'

LINKLINE_STATE_IDLE = 0
LINKLINE_STATE_START = 1
LINKLINE_STATE_HOR_START = 2
LINKLINE_STATE_VER_MID = 3
LINKLINE_STATE_HOR_END = 4

LinkLine = class()

function LinkLine:start()
	print('LinkLine:start()')
	WorldNode_setEnabled(self.start_point, false)
	WorldNode_setEnabled(self.end_point, false)
	WorldNode_setEnabled(self.hor_start, false)
	WorldNode_setEnabled(self.ver_mid, false)
	WorldNode_setEnabled(self.hor_end, false)
	self.disabled = true
	self.startPosX = 0
	self.startPosY = 0
	self.endPosX = 0
	self.endPosY = 0
	self.horStartTarScale = 0
	self.verMidTarScale = 0
	self.horEndTarScale = 0
	self.pointDuration = 0.4
	self.lineDuration = 0.2
	self.timer = 0
	self.lineWidth = 8
	self.state = LINKLINE_STATE_IDLE
end

function LinkLine:stop()
end

function LinkLine:update(dt)

	if not self.disabled then
		if self.state == LINKLINE_STATE_IDLE then
			self.horStartTarScale = (self.startPosX - self.endPosX)/2 + self.lineWidth/2
			self.verMidTarScale = math.abs(self.startPosY - self.endPosY) + self.lineWidth
			self.horEndTarScale = (self.startPosX - self.endPosX)/2 + self.lineWidth/2
			
			
			
			WorldNode_setWorldPosition(self.start_point, self.startPosX, self.startPosY)
			WorldNode_setWorldPosition(self.hor_start, self.startPosX, self.startPosY)
			if self.startPosY - self.endPosY > 0 then
				WorldNode_setLocalRotation(self.ver_mid, math.rad(90))
				WorldNode_setWorldPosition(self.ver_mid, self.startPosX - (self.startPosX - self.endPosX)/2, self.startPosY + self.lineWidth/2)
			else 
				WorldNode_setLocalRotation(self.ver_mid, math.rad(-90))
				WorldNode_setWorldPosition(self.ver_mid, self.startPosX - (self.startPosX - self.endPosX)/2, self.startPosY - self.lineWidth/2)
			end
			WorldNode_setWorldPosition(self.hor_end, self.startPosX - (self.startPosX - self.endPosX)/2 - self.lineWidth/2, self.endPosY)
			WorldNode_setWorldPosition(self.end_point, self.endPosX, self.endPosY)
			WorldNode_setEnabled(self.start_point, true)
			WorldNode_setEnabled(self.hor_start, true)			
			self.drawSpeed = (self.startPosX - self.endPosX + math.abs(self.endPosY - self.startPosY))/self.lineDuration
			self.state = LINKLINE_STATE_START
		end
		
		if self.state == LINKLINE_STATE_START then
			local currentScaleX, _ = WorldNode_getLocalScale(self.hor_start)
			if currentScaleX < self.horStartTarScale  then
				if (self.horStartTarScale - currentScaleX) >  (self.drawSpeed * dt) then
					WorldNode_setLocalScale(self.hor_start, (currentScaleX + self.drawSpeed*dt), 1)
				else
					WorldNode_setLocalScale(self.hor_start, self.horStartTarScale, 1)
					WorldNode_setEnabled(self.ver_mid, true)
					self.state = LINKLINE_STATE_HOR_START
				end
			end
		end
		
		if self.state == LINKLINE_STATE_HOR_START then
			local currentScaleX, _ = WorldNode_getLocalScale(self.ver_mid)
			if currentScaleX < self.verMidTarScale  then
				if (self.verMidTarScale - currentScaleX) >  (self.drawSpeed * dt) then
					WorldNode_setLocalScale(self.ver_mid, (currentScaleX + self.drawSpeed*dt), 1)
				else
					WorldNode_setLocalScale(self.ver_mid, self.verMidTarScale, 1)
					WorldNode_setEnabled(self.hor_end, true)
					self.state = LINKLINE_STATE_VER_MID
				end
			else
				WorldNode_setLocalScale(self.ver_mid, self.verMidTarScale, 1)
				WorldNode_setEnabled(self.hor_end, true)
				self.state = LINKLINE_STATE_VER_MID
			end
		end
		
		if self.state == LINKLINE_STATE_VER_MID then
			local currentScaleX, _ = WorldNode_getLocalScale(self.hor_end)
			if currentScaleX < self.horEndTarScale  then
				if (self.horEndTarScale - currentScaleX) >  (self.drawSpeed * dt) then
					WorldNode_setLocalScale(self.hor_end, (currentScaleX + self.drawSpeed*dt), 1)
				else
					WorldNode_setLocalScale(self.hor_end, self.horEndTarScale, 1)
					WorldNode_setEnabled(self.end_point, true)
					self.state = LINKLINE_STATE_HOR_END
				end
			end
		end
		
		if self.state == LINKLINE_STATE_HOR_END then
			if self.timer < self.pointDuration then
				self.timer = self.timer + dt
			else
				self.state = LINKLINE_STATE_IDLE
				WorldNode_setLocalScale(self.hor_start, 1, 1)
				WorldNode_setLocalScale(self.ver_mid, 1, 1)
				WorldNode_setLocalScale(self.hor_end, 1, 1)
				WorldNode_setEnabled(self.start_point, false)
				WorldNode_setEnabled(self.end_point, false)
				WorldNode_setEnabled(self.hor_start, false)
				WorldNode_setEnabled(self.ver_mid, false)
				WorldNode_setEnabled(self.hor_end, false)
				self.timer = 0
				self.disabled = true
			end
		end
		
	end
end


