extends Node3D

@export var _need_water_drop: Sprite3D
@export var _production_timer: Timer
@export var _mesh: MeshInstance3D
@export var _area: Area3D

var needs_water := true
var stage: int = 1
var final_stage: int = 3


func _ready() -> void:
	set_multiplayer_authority(1)
	_need_water_drop.show()


func _on_production_timer_timeout() -> void:
	needs_water = true
	_need_water_drop.show()


@rpc("authority", "call_local", "reliable")
func _water_shared(sender_id: int) -> void:
	if stage >= final_stage:
		return
	stage += 1
	_mesh.material_override.set("shader_parameter/albedo_texture", load("res://assets/sprout%s.png" % stage))
	if sender_id != 0:
		GameState.fetch().player_data[sender_id].chain_water_combo(1)
	if stage == final_stage:
		_area.collision_layer |= 1 << 4


@rpc("any_peer", "call_local", "reliable")
func water(sender_id: int) -> void:
	if needs_water:
		_water_shared.rpc(sender_id)
		_need_water_drop.hide()
		
		if stage != final_stage:
			needs_water = false
			_production_timer.start()


@rpc("any_peer", "call_local", "reliable")
func get_picked_up() -> void:
	stage = 1
	_mesh.material_override.set("shader_parameter/albedo_texture", load("res://assets/sprout%s.png" % stage))
	_area.collision_layer ^= 1 << 4
	needs_water = true
	_need_water_drop.show()


func mark_interactive() -> void:
	_mesh.material_override.set("shader_parameter/outline_color", Color.WHITE)


func mark_non_interactive() -> void:
	_mesh.material_override.set("shader_parameter/outline_color", Color.BLACK)
