--[[
todo

x make the cloud float above the hotel, rather than next to it
x make the cloud drop small clouds
x switch the small clouds to float above the building
make the small clouds being dropped shoot out of the mouth
]]

CloudBossEnemy = class(CloudEnemy, function(o, x, y, world, ...)
	pd:startList("fromOF")
	pd:addSymbol("cloud_boss_on")
	pd:addFloat(1)
	pd:finish("fromOF")			
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end
	o.settingName = options.settingName or 'cloud_boss_'
	local animation = {'boss_cloud_1.svg', 'boss_cloud_3.svg', 'boss_cloud_3.svg'}
	animation = options.animation or animation
	for i,v in ipairs(animation) do
		if type(v) == "string" then
			animation[i] = SvgLoader(svg_root .. "boss/" .. v, true)
		end
	end
	local shootAnimation = {}
	o.mouthPosition = nil
	for i=1,10 do
		table.insert(shootAnimation, SvgLoader(svg_root .. "boss/boss_cloud_shoot_" .. i .. ".svg", true))
		if i == 8 then
			o.mouthPosition = shootAnimation[8].id.mouth
			o.mouthPosition = Vec2((o.mouthPosition.maxX-o.mouthPosition.minX)/2+o.mouthPosition.minX, (o.mouthPosition.maxY-o.mouthPosition.minY)/2+o.mouthPosition.minY)
		end		
	end
	o.boss_w = animation[1]:getMaxX();
	o.boss_h = animation[1]:getMaxY();

	CloudEnemy.init(o, x, y, world, {h=o.boss_h, w=o.boss_w, animation=cloudAngryAnimation, attackAnimation=cloudAngryAttackAnimation, settingName=o.settingName})
	-- over ride the animation with an svg version of the animation
	o.drawType = 'svg'
	o.idleAnimation = AnimationModel(animation)
	o.shootAnimation = AnimationModel(shootAnimation)
	o.acceleration.y = 0
	-- always move to the right when starting out
	o.moveDirection = 'right'
	o.idleAnimation.loop = false
	o.shootAnimation.loop = false
	o.currentAnimation = o.idleAnimation
	o.moveRate = 4
	o.moveCount = o.moveRate
	o.shootRate = game_settings[o.settingName] or 20
	o.shootCount = o.shootRate
	o:setColor(255, 255, 255, 0)
	o.colorTween = Tweener:addTween(o.tint, {a=255}, {time=2.2, onComplete=function()
		o.tint.a = 255
	end})
	o.healthbar = o.world:add(BossHealthBar())
	o.healthbar:setValue(1)
	bludG.camera:shake(1.5)
end)

function CloudBossEnemy:draw()
	if self.alive then
		self.svg = self.currentAnimation:getSprite()
		self.svg:resetTransform()
		if self.moveDirection == 'left' then
			self.pos.x = self.pos.x+self.w
			self.svg:scale(-1, 1)
		end
		Object.draw(self)
		if self.moveDirection == 'left' then
			self.pos.x = self.pos.x-self.w
		end
	elseif self.exists then
		-- self.svg:resetTransform()
		-- if self.moveDirection == 'left' then
		-- 	self.pos.x = self.pos.x+self.w
		-- 	self.svg:scale(-1, 1)
		-- end
		Object.draw(self)
		-- if self.moveDirection == 'left' then
		-- 	self.pos.x = self.pos.x-self.w
		-- end
	end
end
function CloudBossEnemy:sync()
	if not Enemy.sync(self) then return end
	if not self.alive then return end
	if self.moveRate > 0 then
		self.moveRate = self.moveRate - 1
	else
		self.moveRate = self.moveCount
		self:move()
	end
	if self.shootCount > 0 then
		self.shootCount = self.shootCount - 1
	else
		self.shootCount = self.shootRate
		self.shootAnimation.endCallback = function()
			self.currentAnimation = self.idleAnimation
		end
		self.shootAnimation:startAnimation()
		self.currentAnimation = self.shootAnimation
		-- wait for a little bit before shooting
		self.timeWaits:addRecycle(TimeWait(function()
			self:shootCloud()
		end, .96))
	end
end
function CloudBossEnemy:shootCloud()
	local l = self.world.enemies:addRecycle(CloudEnemy(self.pos.x, self.pos.y, self.world))
	l.pos.x, l.pos.y = self.pos.x, self.pos.y
	if self.moveDirection == 'left' then
		self.mouthPosition.x = self.w - self.mouthPosition.x
		l.pos:add(self.mouthPosition)
		self.mouthPosition.x = self.w - self.mouthPosition.x
	else
		l.pos:add(self.mouthPosition)
	end
	if l:center().x > self.world.rootRoom:center().x then
		l.flip = 1
	end
	pd:startList("fromOF")
	pd:addSymbol("cloud_boss_shoot")
	pd:addFloat(1)
	pd:finish("fromOF")
end
function CloudBossEnemy:move()
	if not self.alive then return end
	local moveSpeed = 100*r_scale
	local limits = self.world:getTowerLimits()
	if self.moveDirection == 'right' then
		self.velocity.x = moveSpeed;
		if self.pos.x > limits.maxX then
			self.moveDirection = 'left'
		end
	else
		self.velocity.x = -moveSpeed;
		if self.pos.x+self.w < limits.minX then
			self.moveDirection = 'right'
		end
	end
	self.idleAnimation:startAnimation()
end

function CloudBossEnemy:update()
	if self.world.over then return end
	local braking = 60
	local bounceDistance = 100
	self.acceleration.x, self.acceleration.y = 0,0
	if retina then braking = braking*2; bounceDistance = bounceDistance*2 end
	if self.velocity.x > 0 then
		self.acceleration.x = -braking
	else
		self.acceleration.x = braking
	end
	-- check to see if we need to avoid the building
	local limits = self.world:getTowerLimits()
	-- limits.minY
	if limits.minY then
		if self:center().y+bounceDistance*1.3 > limits.minY then
			self.acceleration.y = -braking*2
			self.drag.y = braking
		elseif limits.minY - self:center().y > bounceDistance*2 then
			self.acceleration.y = braking*2
			self.drag.y = braking
		end
	end
	CloudEnemy.update(self)
	self.timeWaits:update()
end
function CloudBossEnemy:kill()
	if not self.alive then return end
	self.svg = self.currentAnimation:getSprite()
	self.svg:resetTransform()
	if self.moveDirection == 'left' then
		self.pos.x = self.pos.x+self.w
		self.svg:scale(-1, 1)
	end

	self.svg:explode()
	mainState.particles:addRecycle(ParticleHandler(self:center().x, self:center().y, "particle_data_cloudExplosion.lua", sprites['tiles']['circle.png'], {preroll=.25, lifetime=0.1, cooldown=2}))
	pd:startList("fromOF")
	pd:addSymbol("cloud_boss_kill")
	pd:addFloat(1)
	pd:finish("fromOF")
	self.killb2d = true
	-- can't immediately kill because I want the parts to fade out
	self.alive = false
	Tweener:addTween(self.tint, {a=255}, {time=1, onComplete=function()
		Tweener:addTween(self.tint, {a=0}, {time=2.5})
	end})
	Tweener:addTween(self.pos, {y=self.pos.y+200}, {time=3.5, func='inExpo', onComplete=function()
		self.exists = false
	end})
	-- Object.kill(self)
end
-- spawn above the building, at a random x/y
function CloudBossEnemy:initialPosition()
	self.w, self.h = self.boss_w, self.boss_h
	local limits = self.world:getTowerLimits()
	limits.min = Vec2(limits.minX, limits.minY)
	limits.max = Vec2(limits.maxX, limits.maxY)
	-- spawn above the midpoint of the tower
	local midX = (limits.maxX-limits.minX)/2+limits.maxX-self.w
	self.pos.x, self.pos.y = midX, limits.minY-self.h-100
end