class_name GameStateManager extends Node

var placing_drone : bool = false

var level_timer : float = 0.0

var level_number : int = 0

var level_started : bool = false
var level_complete : bool = false
var game_over : bool = false

## Game Rules ##############################################################

# Nectar levels - Depleted by snails, increased by bee nectar deposits
# This is capped out at 10... Seems like a good amount
# This has to be at least 1 at all times or the bees dont know wtf is going on
var flower_nectar_level_charge : int = 0
var flower_nectar_level_charge_required : int = 10
var max_flower_nectar_level : int = 10
var flower_nectar_level : int = 10 : 
	get:
		return flower_nectar_level
	set(value):
		if value > max_flower_nectar_level:
			flower_nectar_level = 10
		elif value < 1:
			flower_nectar_level = 1
		else:
			flower_nectar_level = value

# Snails
var spawn_snails : bool = false
var snail_spawn_interval : float = 10.0

var gathered_nectar : int = 0 :
	get:
		return gathered_nectar
	set(value):
		gathered_nectar = value
		if gathered_nectar > required_nectar and !level_complete:
			game_win()

var required_nectar : int = 100
var level_par : int = 2
var drones_used : int = 0
var bees_available : int = 0
var dead_bees : int = 0 : 
	get:
		return dead_bees
	set(value):
		dead_bees = value
		if dead_bees >= bees_available and !game_over:
			game_lose()

var level_points : int = 0 :
	get:
		return required_nectar * 100 - round(level_timer) * 10 - drones_used * 100

var judge_level_par : int = 0 :
	get:
		## Return an amount of points based on the difference between the number of drones_used and the level_par 
		## Using more drones than the par is bad and should result in less points 
		var diff : int = drones_used - level_par
		if diff > 0:
			return -diff * 100
		else:
			return 0

func _process(delta : float) -> void:
	if level_started and !level_complete and !game_over:
		level_timer += delta

## For every 10 times the bees deposit nectar it will charge 
# the nectar level of the flowers by 1 
func pollenate() -> void:
	flower_nectar_level_charge += 1
	if flower_nectar_level_charge >= flower_nectar_level_charge_required:
		flower_nectar_level += 1
		flower_nectar_level_charge = 0

func bee_died() -> void:
	dead_bees += 1

# Add the nectar to the total gathered nectar
func add_nectar(nectar : int) -> void:
	pollenate()
	gathered_nectar += nectar

func add_drone() -> void:
	drones_used += 1

func remove_drone() -> void:
	drones_used -= 1

func game_start() -> void:
	level_started = true

func game_win() -> void:
	Log.pr("Game win")
	level_complete = true
	HighScoreMgr.add_honey(gathered_nectar)
	HighScoreMgr.update_highscore(level_number, level_points)
	HighScoreMgr.add_dead_bees(dead_bees)
	HighScoreMgr.save()

func game_lose() -> void:
	Log.pr("Game lose")
	game_over = true
	HighScoreMgr.add_dead_bees(dead_bees)
	HighScoreMgr.save()

func reset() -> void:
	level_timer = 0.0
	level_started = false
	level_complete = false
	gathered_nectar = 0
	drones_used = 0
	dead_bees = 0
	spawn_snails = false
