
---- Fibers are built by Alis in free roaming mode ----
-------------------------------------------------------------------------------

Fibers = {
	points = {}, 
	prepoint = nil, 	-- the last point visited in the circle before starting this fiber group
	drawcount = 0
}

function Fibers:pre(p)
	self.prepoint = p
end

function Fibers:add(x, y)
	if self.prepoint then
		table.insert(self.points, {x, y})
	end
end

function Fibers:stop()
	self.points = {}
	self.prepoint = nil
end

function Fibers:isStartHorizontal()
	return not(self.points[1][1] == self.points[2][1])
end

function Fibers:eachLine(f)
	if not self.points[1] then return end
	local nextpoint, hor, s = nil, self:isStartHorizontal(), table.getn(self.points)
	for i=1, s-1 do
		if f(self.points[i], self.points[i+1], hor) == false then return false end
		hor = not hor
	end
end

function Fibers:draw()
	if not self.prepoint then return end
	-- draw fibers
	self:eachLine(function(p1, p2)
		screen:drawLine(p1[1], p1[2], p2[1], p2[2],
			Color.new(150+self.drawcount*5, self.drawcount*10, 0, 150))
	end)
	-- mark pre
	if math.random(5) == 1 then
		local x, y, s = self.prepoint[1], self.prepoint[2], math.random(2, 5)
		local c = Color.new(255, 120, self.drawcount*10+60, 255-(s-3)*20)
		screen:drawLine(x-s, y, x, y-s, c)
		screen:drawLine(x, y-s, x+s, y, c)
		screen:drawLine(x+s, y, x, y+s, c)
		screen:drawLine(x, y+s, x-s, y, c)
	end
	self.drawcount = self.drawcount + 1
	if self.drawcount > 20 then self.drawcount = 0 end
end

function Fibers:changeLast(x, y)
	if not self.prepoint then return end
	self.points[table.getn(self.points)][1] = x
	self.points[table.getn(self.points)][2] = y
end

function Fibers:resolve(postpoint)
	if not self.prepoint then return end
	--determine cutting points
	local preindex, postindex = -1, -1
	for i = 1, table.getn(Circle.points) do
		if Circle.points[i] == self.prepoint then preindex = i end
		if Circle.points[i] == postpoint then postindex = i end
		if preindex > 0 and postindex > 0 then break end 
	end
	-- build the new circle
	local newcircle = {}
	if preindex < postindex then -- normal case
		for i = 1, preindex do
			table.insert(newcircle, Circle.points[i])
		end
		for i = 1, table.getn(self.points) do
			table.insert(newcircle, self.points[i])
		end
		for i = postindex, table.getn(Circle.points) do
			table.insert(newcircle, Circle.points[i])
		end
	else -- jumping over endpoint
		for i = 1, table.getn(self.points) do
			table.insert(newcircle, self.points[i])
		end
		for i = postindex, preindex do
			table.insert(newcircle, Circle.points[i])
		end
	end
	-- remove doubly specified points
	removeSuperfluous(newcircle)
	-- determine new current Point for Alis
	local lastindex = getIndex(newcircle, Circle.points[postindex]) - 1
	if lastindex == -1 then 
		lastindex = getIndex(newcircle, self.points[table.getn(self.points)-1])
	end
	if lastindex <= 0 then lastindex = table.getn(newcircle) + lastindex end
	-- done
	
	self.points = {}
	self.prepoint = nil
	
	if Circle:setPoints(newcircle) then
		sfx.closecircle:play()
		return Circle.points[lastindex]
	else
		sfx.breakfiber:play()
		return nil
	end
end
