CursedBook = {}
CursedBook.type = "CursedBook"
CursedBook.supertype = Entity.type
CursedBook.display = Animation:new(animation.cursed_book.flying)
setmetatable(CursedBook, Entity)

function CursedBook:new(x,y)
	local o = Entity:new(x,y)

	-- behaviour
	o.pos = {x = x, y = y}
	o.speed = 0.01
	o.range = 20
	o.target = {x = x, y = y}

	o.status = 0
	-- 0 - sleep
	-- 1 - getting up
	-- 2 - flying
	-- 3 - attack windup
	-- 4 - attack
	o.spawn_range = 100
	o.attack_range = 50

	-- animations
	o.body = Animation:new(animation.cursed_book.spawn)
	o.body.speed = 0
	o.sprite_tint = {0.7,0.7,0.7}
	o:centerOffset(o.body)
	o:createBox(o.body)

	-- light
	local light_data = {}
	light_data.radius = 500
	light_data.shine_radius = 0
	light_data.flicker = nil
	light_data.color = nil
	o.light = Light:new(o.pos.x,o.pos.y,light_data)

	o:id()

	setmetatable(o, self)
	self.__index = self
	return o
end

function CursedBook:doLogic()
	print(self.status)
	self.target.x = main_player.pos.x - main_player.target_offset.x
	self.target.y = main_player.pos.y - main_player.target_offset.y

	local distance_x = self.target.x - self.pos.x
	local distance_y = self.target.y - self.pos.y
	local angle = getAngleFromVector(distance_x,distance_y)
	local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)

	if self.status == 0 then
		if distance < self.spawn_range then
			self.status = 1
		end
	elseif self.status == 2 then
		if distance < self.range then
			self.vel.x = 0
			self.vel.y = 0
		else
			self.vel.x = math.cos(angle)*self.speed*distance
			self.vel.y = math.sin(angle)*self.speed*distance
		end
	elseif self.status == 2 then
		if distance < self.attack_range then
			--self.status = 3
		end
	elseif self.status == 4 then

	end

	if self.isFlying then
		local random_x = math.random(-4, 4)/100
		local random_y = math.random(-4, 4)/100
		self.vel.x = self.vel.x + random_x
		self.vel.y = self.vel.y + random_y
	end
end

function CursedBook:handleAnimation()
	if self.status == 1 then
		if self.body.path == "assets/entities/cursed_book/spawn" then
			self.body.speed = 1
			local tint = 0.7 + 0.3 * (self.body.frame-1)/#self.body.frames
			self.sprite_tint = {tint,tint,tint}
			if self.body.frame == #self.body.frames then
				self.status = 2
				self.isFlying = true
				self.body = self.body:change(animation.cursed_book.flying)
				self.sprite_tint = {1,1,1}
				--self:getBoundingBox(self.body,2,2,-2,-2)
				self:centerOffset(self.body)
			end
		end
	elseif self.status == 3 then
		if self.body.path == "assets/entities/cursed_book/flying" then
			self.body = self.body:change(animation.cursed_book.attack_transition)
			self.body.speed = 1
			--self:centerOffset(self.body)
			if self.body.frame == #self.body.frames then
				self.status = 4
				self.body = self.body:change(animation.cursed_book.attack_loop)
				self:centerOffset(self.body)
			end
		end
	end
	self.body:animate()
	self:draw(self.body)
end

function CursedBook:doPhysics()
	-- horizontal collision
	self:moveX(
		self.vel.x,
		function()
			self.vel.x = 0
		end
	)
	-- vertical collision
	self:moveY(
		self.vel.y,
		function()
			self.vel.y = 0
		end
	)
	-- final position
	self:adjustLight()
end

function CursedBook:debug()
	-- draw center GREEN
	love.graphics.setColor(0,1,0)
	love.graphics.circle("line", -Camera.pos.x + self.pos.x, -Camera.pos.y + self.pos.y, self.spawn_range)
	love.graphics.setColor(1,0,0)
	love.graphics.circle("line", -Camera.pos.x + self.pos.x, -Camera.pos.y + self.pos.y, self.attack_range)
	Entity.debug(self)
end
