LayoutMikado = {}
setmetatable(LayoutMikado, Layout)
-- ----------------------------------------------------------------------------
LayoutMikado.__index = function(t, k)

	if rawget(t, k) ~= nil then
		return rawget(t, k)
	elseif LayoutMikado[k] ~= nil then
		return LayoutMikado[k]
	end
	
end
-- ----------------------------------------------------------------------------
function LayoutMikado:setup(base)

	assert(type(base) == "string")		-- base name of the objects
	
	self.mOriginals = {}
	self.mBase = base
	self.mPuzzle = true
	self.mHighlight = nil
	self.mHighlightIndex = nil

end
-- ----------------------------------------------------------------------------
function LayoutMikado:onInitialize()

	Layout.onInitialize(self)
	
	local n = nil
	-- find start index
	-- to compensate for 0 or 1 based indexing
	for i = 0, 100 do
		local varName
		if (i < 10) then varName = self.mBase .. "0" .. i
		else			 varName = self.mBase .. i
		end
		if (_G[varName] ~= nil) then
			n = i
			break		
		end	
	end
	assert(n ~= nil)

	-- find all originals
	for i = n, 100 do
		local varName
		if (i < 10) then varName = self.mBase .. "0" .. i
		else			 varName = self.mBase .. i
		end
		if (_G[varName] == nil) then
			break
		end	
		table.insert(self.mOriginals, _G[varName])
	end
	
end
-- ----------------------------------------------------------------------------
function LayoutMikado:onMouseDown(x, y)

	if (self:isPuzzling()) then
		local stickIndex = nil
		for i = #self.mOriginals, 1, -1 do
			if (isInsideAlpha(self.mOriginals[i], x, y, 0.5)) then
				stickIndex = i
				break
			end	
		end
		
		if (stickIndex ~= nil) then
			-- look if one of sticks above it intersects with the found stick.
			-- if so, it's considered blocked and can't be clicked.	
			local ignore = false
			for i = #self.mOriginals, stickIndex+1, -1 do
				if (intersectsAlpha(self.mOriginals[stickIndex], self.mOriginals[i])) then
					ignore = true
					break
				end
			end
		
			if (not ignore) then
				cursor:green()

				self.mOriginals[stickIndex]:ss(kDone)
				table.remove(self.mOriginals, stickIndex)
				self:playTakenSound()
				
				if (#self.mOriginals == 0) then
					self:solve()
				end				
				
				return true		
			end	
		end
	end
	
	return Layout.onMouseDown(self, x, y)

end
-- ----------------------------------------------------------------------------
function LayoutMikado:onMouseMoved(x, y)
    
    local result = Layout.onMouseMoved(self, x, y)
    
    -- check if we are inside a stick
    local stick = nil
    local stickIndex = nil
    
	for i = #self.mOriginals, 1, -1 do
		if (isInsideAlpha(self.mOriginals[i], x, y, 0.5)) then
			stickIndex = i
			stick = self.mOriginals[i]
			break
		end	
	end
		
	if (stickIndex ~= self.mHighlightIndex) then
		
		-- remove previous highlight
		if (self.mHighlight ~= nil) then

			ghf.cancelAnimation("animHighlightFadeIn" .. self.mHighlightIndex)
			self.mHighlight:setObsolete()
			self.mHighlight = nil

		end
		
		self.mHighlightIndex = stickIndex
			
		-- create new highlight
		if (self.mHighlightIndex ~= nil) then
						
			self.mHighlight = self:newChildSprite( { x=stick:x(), y=stick:y(), image=stick:getImage(), blendMode="additive" } )
			
			self.mHighlight:setAlpha(0)
			self.mHighlight:moveInFrontOf(stick)
			
			self.mHighlight:animate("animHighlightFadeIn" .. self.mHighlightIndex,
				{ delay=150					},
				{ alpha=0.8, duration=150	}
			)
			
		end
	end
    
    return result
    
end
-- ----------------------------------------------------------------------------
function LayoutMikado:onSolve()

	Layout.onSolve(self)
	
	for i, stick in ipairs(self.mOriginals) do
		stick:ss(kDone)
	end
	
	self.mOriginals = {}
	
	if (self.mHighlight ~= nil) then
		self.mHighlight:setObsolete()
		self.mHighlight = nil
		self.mHighlightIndex = nil
	end

end
-- ----------------------------------------------------------------------------
function LayoutMikado:playTakenSound()
	playAudioTrack("GOOD_CLICK_FX")
end