colors = require('colors')

function setup(scene)
	scene.width = 1920
	scene.height = 1080
end

function draw(scene)
	local gap = 10
	local count = 0
	for k,v in pairs(colors) do
		count = count + 1
	end
	local item_width = (scene.width - gap*(count+1)) / count
	local item_height = 100 

	local rectangles = {}
	local i = 0
	for k, v in pairs(colors) do
		rect = scene:new_rectangle()
		rect.height = item_height 
		rect.width = item_width 
		rect.background = v
		rect.x = item_width * i + gap * (i+1)
		rect.y = scene.height - rect.height - gap 
		scene:add_rectangle(rect)
		rectangles[i] = rect
		i = i + 1
	end

	scene:wait(1000)

	local animations = {}
	for k,v in pairs(rectangles) do
		animations[#animations+1] = v:animate_height{
			to = scene.height - gap * 2,
			duration_ms = 1000,
			delay_ms = 1000/count*k,
			easing = "cubic_inout"
		}
		animations[#animations+1] = v:animate_y{
			to = gap,
			duration_ms = 1000,
			delay_ms = 1000/count*k,
			easing = "cubic_inout"
		}
	end

	scene:animate(
		table.unpack(animations)
	)

	local animations = {}
	for k,v in pairs(rectangles) do
		animations[#animations+1] = v:animate_height{
			to = item_height,
			duration_ms = 1000,
			delay_ms = 1000/count*(count-k),
			easing = "cubic_inout"
		}
	end

	scene:animate(
		table.unpack(animations)
	)

	local animations = {}
	for i=0,count-2 do
		animations[#animations+1] = rectangles[i]:animate_x{
			to = scene.width - gap - item_width,
			duration_ms = 1000,
			delay_ms = 1000/count*(count-i),
			easing = "cubic_inout"
		}
		animations[#animations+1] = rectangles[i]:animate_background{
			to = {0,0,0,0},
			duration_ms = 1000,
			delay_ms = 1000/count*(count-i),
			easing = "cubic_inout"
		}
	end

	scene:animate(
		table.unpack(animations)
	)

	scene:animate(
		rectangles[count-1]:animate_x{
			to = gap,
			duration_ms = 1000,
			easing = "cubic_inout"
		},
		rectangles[count-1]:animate_width{
			to = scene.width - gap*2,
			duration_ms = 1000,
			easing = "cubic_inout"
		},
		rectangles[count-1]:animate_height{
			to = scene.height - gap*2,
			duration_ms = 1000,
			easing = "cubic_inout"
		}
	)

	scene:wait(1000)

	c = scene:new_circle()
	c.radius = 0
	c.background =	colors.aqua 
	c.x = scene.width / 2
	c.y = scene.height / 2
	scene:add_circle(c)

	scene:animate(
		c:animate_radius{
			to = math.sqrt(scene.width * scene.width + scene.height * scene.height),
			duration_ms = 1000,
			easing = "cubic_inout"
		}
	)
end
