
extends Node2D

const MIN_X = 0
const MIN_Y = 0
const MAX_X = 800
const MAX_Y = 600

var trajectory = Vector2(0,1)
var speed = 50.0
var speed_derivative = 0.0

export(String) var fish_name = ""
export(String) var fish_species = "basic_fish"
export(float) var fish_hunger = 0.0
export(float) var fish_ammonia_stress = 0.0

var global = null

func _ready():
	set_child_node_properties()
	set_process(true)
	global = get_node("/root/Global")
	fish_hunger = get_fish_species_property("max_hunger")
	fish_ammonia_stress = get_fish_species_property("ammonia_tolerance")

func _process(delta):
	set_position(next_wander(delta))

func next_wander(delta):
	var current_pos = get_position()
	
	if (current_pos.x < MIN_X and trajectory.x < 0 or current_pos.x > MAX_X and trajectory.x > 0):
		speed_derivative = -100
	
	if current_pos.y < MIN_Y:
		trajectory.y = 1
	elif current_pos.y > MAX_Y:
		trajectory.y = -1
	
	trajectory = slightly_random(trajectory, delta)
	
	speed_derivative += delta * rand_range(-100.0, 100.0)
	speed += delta * speed_derivative
	
	if (speed_derivative < -200.0):
		speed_derivative = -50.0
	elif (speed_derivative > 200.0):
		speed_derivative = 0.0
	
	if (speed < -0.2):
		speed = -speed * 2
		trajectory = -trajectory
		speed_derivative = 50.0
	if (speed > 110):
		speed = 100.0
		speed_derivative = 0.0
	
	if trajectory.x < -0.5:
		get_node("sprite").set_flip_h(false)
	elif trajectory.x > 0.5:
		get_node("sprite").set_flip_h(true)
	
	return get_position() + delta * trajectory * speed

func slightly_random(vec, delta):
	var x = (vec.x + delta * rand_range(-2.0, 2.0)) * 1.005
	var y = vec.y + delta * rand_range(-2.0, 2.0)
	var V = Vector2(x, y)
	return V.normalized()

func set_child_node_properties():
	get_node("nametag").text = fish_name
	get_node("canvas/fish_menu/rename_input").text = fish_name

func update_fish_name(new_name):
	fish_name = new_name
	set_child_node_properties()

func get_fish_species_property(key):
	return global.get_fish_species(fish_species)[key]
	
func calculate_temperature_health(x):
	if (x <= 5.0):
		return 1
	else:
		return 1 - min(1, (x - 5) * (x - 5) / 10)

func calculate_ph_health(x):
	if (x <= 0.5):
		return 1
	else:
		return 1 - min(1, (x - 0.5) * (x - 0.5))

func calculate_salinity_health(x):
	if (x <= 0.05):
		return 1
	else:
		return 1 - min(1, (x - 0.05) * (x - 0.05) * 10)

func get_general_fish_health():
	var hunger_pct = fish_hunger / get_fish_species_property("max_hunger")
	var ammonia_pct = fish_ammonia_stress / get_fish_species_property("ammonia_tolerance")
	var temperature_diff = abs(get_node("..").aquarium_temperature - get_fish_species_property("preferred_temperature"))
	var ph_diff = abs(get_node("..").aquarium_ph - get_fish_species_property("preferred_ph"))
	var salinity_diff = abs(get_node("..").aquarium_salinity - get_fish_species_property("preferred_salinity"))
	
	var temperature_health = calculate_temperature_health(temperature_diff)
	var ph_health = calculate_ph_health(ph_diff)
	var salinity_health = calculate_salinity_health(salinity_diff)
	
	return hunger_pct * ammonia_pct * temperature_health * ph_health * salinity_health

