fan = {
	force = 1500,
	rotV = 2,
}
function fan:upd(dt)
	--key control fan
	local rotV = fan.rotV
	if love.keyboard.isDown("left") then
		self.angle = self.angle - rotV * dt
		self.dx = math.cos(self.angle)
		self.dy = math.sin(self.angle)
	end
	if love.keyboard.isDown("right") then
		self.angle = self.angle + rotV * dt
		self.dx = math.cos(self.angle)
		self.dy = math.sin(self.angle)
	end
	if love.keyboard.isDown("up") then
		self.strength = 4 * self.force
	elseif love.keyboard.isDown("down") then
		self.strength = 0.5 * self.force
	else
		self.strength = 2 * self.force
	end
end
function fan:reset()
	self.angle = 0
	self.dx = 1
	self.dy = 0
	self.strength = 2 * self.force
end

petBubs = {}
maxScore = 0
minScore = 2

function petBubs:new(x, y, r, id)
	local pBub = { x = x, y = y, r = r, id = id }
	pBub.collider = world:newCircleCollider(x, y, r)
	pBub.collider:setType("static")
	pBub.collider:setCollisionClass("PetBubble")
	function pBub:draw()
		if PetBubImg then
			local sc = 13 / PetBubImg:getWidth()
			love.graphics.draw(
				PetBubImg,
				self.x,
				self.y,
				nil,
				sc,
				sc,
				PetBubImg:getWidth() / 2,
				PetBubImg:getHeight() / 2
			)
		else
			love.graphics.setColor(1, 1, 1)
			love.graphics.circle("line", x, y, r)
		end
	end
	pBub.collider:setObject(pBub)
	table.insert(self, pBub)
	return pBub
end

function petBubs:reset(levelData)
	for index, value in ipairs(self) do
		if value.collider then
			value.collider:destroy()
			self[index] = nil
		end
	end
	for i, pos in ipairs(levelData.petBubsPos) do
		petBubs:new(pos[1], pos[2], 5)
	end
	updTableId(self)
	maxScore = #self
	minScore = levelData.minScore or 0
end

walls = {
	Wall = {
		draw = function(self)
			-- love.graphics.setColor(1, 1, 1)
			-- love.graphics.rectangle("fill", self.x, self.y, self.w, self.h)
			love.graphics.draw(self.img, self.quad, self.x, self.y, nil, self.scale, self.scale, self.toX, self.toY)
		end,
	},
	Tube = {
		draw = function(self)
			-- love.graphics.setColor(1, 1, 1)
			-- love.graphics.rectangle("line", self.x, self.y, self.w, self.h)
			local img = TubeEImg
			if self.dirX > 0 then
				img = TubeEImg
			elseif self.dirX < 0 then
				img = TubeWImg
			elseif self.dirY > 0 then
				img = TubeSImg
			elseif self.dirY < 0 then
				img = TubeNImg
			end
			love.graphics.draw(img, self.x, self.y)
		end,
	},
	Spike = {
		draw = function(self)
			-- love.graphics.setColor(1, 0, 0)
			-- love.graphics.rectangle("fill", self.x, self.y, self.w, self.h)

			love.graphics.draw(self.img, self.quad, self.x, self.y, nil, self.scale, self.scale, self.toX, self.toY)
		end,
	},
	Turner = {
		draw = function(self)
      local rot =0

			if self.dirX > 0 then
				rot=math.pi/2
			elseif self.dirX < 0 then
        rot=-math.pi/2
			elseif self.dirY > 0 then
        rot=math.pi
			elseif self.dirY < 0 then
        rot=0
			end
			local animId = math.ceil(AnimT % 2 + 0.01)
			local img = TurnerAnim[animId]
			if img then
				love.graphics.draw(img, self.cX, self.cY, rot, 1, 1, img:getWidth() / 2, img:getHeight() / 2)
			else
				love.graphics.setColor(1, 1, 1)
				love.graphics.polygon("line", self.x, self.y, self.w, self.h, self.tgX, self.tgY)
			end
		end,
	},
}
function walls_reset(levelData)
	for key, typeWalls in pairs(walls) do
		for index, value in ipairs(typeWalls) do
			if value.collider then
				value.collider:destroy()
				typeWalls[index] = nil
			end
		end
	end
	for i, wall in ipairs(levelData.structures) do
		newWallLike(wall[1], wall[2], wall[3], wall[4], wall[5], wall[6], wall[7], wall[8])
	end
end

function newWallLike(x, y, w, h, class, tgX, tgY, f)
	--dir="0=E,1=S,2=W,4=N"
	local wall = { x = x, y = y, w = w, h = h, tgX = tgX, tgY = tgY }
	if class then
		if class == "Turner" then
			wall.collider = world:newPolygonCollider({ x, y, w, h, tgX, tgY })
			if x == w then
				wall.dirX, wall.dirY = (x - tgX) / math.abs(x - tgX), 0
			elseif y == h then
				wall.dirX, wall.dirY = 0, (y - tgY) / math.abs(y - tgY)
			else
				wall.dirX, wall.dirY = 0, 0
			end
			wall.cX = (x + w + tgX) / 3
			wall.cY = (y + h + tgY) / 3
			wall.force = f or 5000
		else
			wall.collider = world:newRectangleCollider(x, y, w, h)
			--Texture
			if w > h then
				--horizonal
				local img = ImgHs[class]
				if img then
					wall.img = img
					wall.scale = img:getHeight() / (2 * h)
					wall.toX, wall.toY = 0, h / 2
					wall.quad = love.graphics.newQuad(0, 0, w / wall.scale, img:getHeight(), img)
				end
			else
				--vertical
				local img = ImgVs[class]
				if img then
					wall.img = img
					wall.scale = img:getWidth() / (2 * w)
					wall.toX, wall.toY = w / 2, 0
					wall.quad = love.graphics.newQuad(0, 0, img:getWidth(), h / wall.scale, img)
				end
			end
		end
		wall.collider:setType("static")
		wall.collider:setObject(wall)
		wall.collider:setCollisionClass(class)
		if class == "Tube" then
			wall.is_active = false
			tubeMovCo = nil
			-- wall.collider:setCollisionClass("Wall")
			wall.dirX, wall.dirY = 0, 0
			if (tgX - x) ~= 0 then
				wall.dirX = (tgX - x) / math.abs(tgX - x)
			end
			if (tgY - y) ~= 0 then
				wall.dirY = (tgY - y) / math.abs(tgY - y)
			end
			-- local tbX1, tbY1, tbX2, tbY2 = 0, 0, 1, 0
			-- if wall.dirX == 0 then
			-- 	tbX1, tbX2 = x, x + w
			-- 	tbY1 = y + h / 2 + wall.dirY * h / 2
			-- 	tbY2 = tbY1
			-- elseif wall.dir==0 then
			-- 	tbY1, tbY2 = y, y + h
			-- 	tbX1 = x + w / 2 + wall.dirX * w / 2
			-- 	tbX2 = tbX1
			-- end
			-- local tubeEntry = world:newLineCollider(tbX1, tbY1, tbX2, tbY2)
			-- tubeEntry:setCollisionClass("Tube")
			--    tubeEntry:setType('static')
			-- world:addJoint("RevoluteJoint", wall.collider, tubeEntry,50,50,true)
		end
		table.insert(walls[class], wall)
	end
	return wall
end
function walls_upd(dt)
	local tube = walls.Tube[1]
	if tube and not tube.is_active and playBub.score >= minScore then
		local tgX, tgY = tube.tgX, tube.tgY
		local oX = tube.x

		local oY = tube.y
		if not tubeMovCo then
			tubeMovCo = coroutine.create(function()
				local prog = 0
				while prog < 1 do
					prog = math.min(prog + dt, 1)
					tube.x = prog * tgX + (1 - prog) * oX
					tube.y = prog * tgY + (1 - prog) * oY
					tube.collider:setPosition(tube.x + tube.w / 2, tube.y + tube.h / 2)
					coroutine.yield()
				end
				tube.is_active = true
			end)
		end
		coRun(tubeMovCo)
	end
end
function drawEnv()
	--Draw wall
	for key, wallType in pairs(walls) do
		for i, wall in ipairs(wallType) do
			wallType.draw(wall)
		end
	end
	--Draw petite Bubble
	for i, pBub in ipairs(petBubs) do
		pBub:draw()
	end
end
