extends Node2D

signal win

const IN_GAME_WAIT_TIME = 10

var pulling: Sprite2D
var mash: Label
var you: Sprite2D
var sad: Sprite2D
var happy: Sprite2D
var mouse: Sprite2D
var shake: ShakePivot
var shake2: ShakePivot
var shake3: ShakePivot
var start_game_timer: Timer
var in_game_timer: Timer
# This used to mean "success/failure" but now means "you succ, failure"
var succfail_timer: Timer 
var success_timer: Timer

var in_minigame = false
var num_clicks = 0

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pulling = $ShakePivot/pulling
	you = $"you!"
	mash = $ShakePivot2/MASH
	mouse = $ShakePivot3/Mouse
	shake = $ShakePivot
	shake2 = $ShakePivot2
	shake3 = $ShakePivot3
	sad = $SAD
	happy = $Happy
	start_game_timer = $StartGame
	in_game_timer = $InGame
	succfail_timer = $SuccessFail
	success_timer = $Success
	

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _on_texture_button_button_up() -> void:
	shake.shake()
	shake2.shake()
	shake3.shake()
	
	if in_minigame:
		num_clicks -= 1
		
		# YOU WIN
		if num_clicks == 0:
			in_minigame = false
			
			# hide non-happy cats
			pulling.visible = false
			mash.visible = false
			mouse.visible = false
			sad.visible = false
			you.visible = false
			
			# show happy cat
			happy.visible = true
			
			in_game_timer.stop()
			in_game_timer.wait_time = IN_GAME_WAIT_TIME
			success_timer.start()
			$"Yippee!".play()


func _on_start_game_timeout() -> void:
	if randf() < 0.1:
		start_game_timer.stop()
		in_game_timer.start()
		
		in_minigame = true
		
		# hide passive cats
		you.visible = false
		sad.visible = false
		happy.visible = false
		
		# show active cat
		pulling.visible = true
		mash.visible = true
		mouse.visible = true
		
		num_clicks = randi_range(5,21)
		#num_clicks = 21


# YOU LOSE
func _on_in_game_timeout() -> void:
	in_minigame = false
	
	# hide non-sad cats
	you.visible = false
	happy.visible = false
	pulling.visible = false
	mash.visible = false
	mouse.visible = false
	
	# show sad cat
	sad.visible = true
	
	in_game_timer.stop()
	succfail_timer.start()
	$"Fart".play()


func _on_success_fail_timeout() -> void:
	sad.visible = false
	happy.visible = false
	you.visible = true
	succfail_timer.stop()
	start_game_timer.start()


func _on_success_timeout() -> void:
	win.emit()
