extends Sprite2D

signal fruit_sliced

# Fruit types and their properties
enum FruitType {APPLE, BANANA, ORANGE, WATERMELON}

var fruit_type = FruitType.APPLE
var is_sliced = false

# Movement properties
var velocity = Vector2.ZERO
var gravity = 800.0
var rotation_speed = 0.0

# Preload textures
var textures = {
	FruitType.APPLE: {
		"whole": preload("res://assets/images/apple_small.png"),
		"half1": preload("res://assets/images/apple_half_1_small.png"),
		"half2": preload("res://assets/images/apple_half_2_small.png")
	},
	FruitType.BANANA: {
		"whole": preload("res://assets/images/banana_small.png"),
		"half1": preload("res://assets/images/banana_half_1_small.png"),
		"half2": preload("res://assets/images/banana_half_2_small.png")
	},
	FruitType.ORANGE: {
		"whole": preload("res://assets/images/orange_small.png"),
		"half1": preload("res://assets/images/orange_half_1_small.png"),
		"half2": preload("res://assets/images/orange_half_2_small.png")
	},
	FruitType.WATERMELON: {
		"whole": preload("res://assets/images/watermelon_small.png"),
		"half1": preload("res://assets/images/watermelon_half_1_small.png"),
		"half2": preload("res://assets/images/watermelon_half_2_small.png")
	}
}

# Splash textures based on fruit type
var splash_textures = {
	FruitType.APPLE: preload("res://assets/images/splash_red_small.png"),
	FruitType.BANANA: preload("res://assets/images/splash_yellow_small.png"),
	FruitType.ORANGE: preload("res://assets/images/splash_orange_small.png"),
	FruitType.WATERMELON: preload("res://assets/images/splash_red_small.png")
}

# Preload the fruit piece scene
var FruitPiece = preload("res://scenes/fruit_piece.tscn")

func _ready():
	# Add to FRUIT group
	add_to_group("FRUIT")
	
	# Randomly select fruit type
	fruit_type = randi() % FruitType.size()
	texture = textures[fruit_type]["whole"]
	
	# Add random rotation
	rotation = randf_range(-PI/4, PI/4)
	rotation_speed = randf_range(-2, 2)
	
	# Set initial velocity (random x, upward y)
	velocity = Vector2(randf_range(-100, 100), -400)

func _process(delta):
	if not is_sliced:
		# Update position based on velocity
		position += velocity * delta
		
		# Apply gravity to velocity
		velocity.y += gravity * delta
		
		# Update rotation
		rotation += rotation_speed * delta
		
		# Remove if off screen
		if position.y > 800:
			queue_free()

func slice(slash_pos: Vector2):
	# TODO: Implement fruit slicing (Lecture 13)
	pass
