extends Node2D
class_name Fishin

enum FishType {
	GAR,
	SALMON,
	BASS,
	HUMUHUMUNUKUNUKUAPUA_A,
	CATFSIH,
	DOGFISH,
	ANGLERFISH,
	MOBY_DICK,
	LEAPING_SMACKEREL,
}

signal win(fish_type: FishType)
signal fish_chosen(fish_sprite: Sprite2D, fish_name: String)

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 fish_choice
var gar 
var salmon 
var bass
var humuhumunukunukuapua_a
var catfsih
var dogfish
var pupperfish
var anglerfish
var moby_dick
var leaping_smackerel

var in_minigame = false
var num_clicks = 0

var fishes = [FishType.GAR, FishType.SALMON, FishType.BASS, FishType.HUMUHUMUNUKUNUKUAPUA_A, FishType.CATFSIH, FishType.DOGFISH, FishType.ANGLERFISH, FishType.MOBY_DICK, FishType.LEAPING_SMACKEREL]

# 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
	
	get_window().title = "Relaxing Fishing Game 🙂"

	gar = $Happy/Gar
	salmon = $Happy/SockeyeSalmon
	bass = $Happy/LargemouthBass
	humuhumunukunukuapua_a = $"Happy/Humuhumunukunukuapua'a"
	catfsih = $Happy/Catfsih
	dogfish = $Happy/Dogfish
	pupperfish = $Happy/Pupperfish
	anglerfish  = $Happy/Anglerfish
	moby_dick = $"Happy/Moby-Dick"
	leaping_smackerel = $Happy/LeapingSmackerel
	

# 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
			
			# show fish
			fish_choice = randi_range(0,len(fishes))
			
			match fish_choice:
				FishType.GAR: 
					gar.visible = true
					fish_chosen.emit(gar.get_node("GarSprite"), "Gar :)")
					$"Yippee!".play()
				FishType.SALMON: 
					salmon.visible = true
					fish_chosen.emit(salmon.get_node("SalmonSprite"), "Sockeye Salmon")
					$"Yippee!".play()
				FishType.BASS: 
					bass.visible = true
					fish_chosen.emit(bass.get_node("LargemouthBassSprite"), "Largemouth Bass")
					$"Yippee!".play()
				FishType.HUMUHUMUNUKUNUKUAPUA_A: 
					humuhumunukunukuapua_a.visible = true
					fish_chosen.emit(humuhumunukunukuapua_a.get_node("Sprite2D"), "Humuhumunukunukuapua'a")
					$"Yippee!".play()
				FishType.CATFSIH: 
					catfsih.visible = true
					fish_chosen.emit(catfsih.get_node("CatfishSprite"), "Catfish :3")
					$Meow.play()
				FishType.DOGFISH: 
					dogfish.visible = true
					fish_chosen.emit(dogfish.get_node("DogfishSprite"), "Dogfish 🐶")
					$Bark.play()
				FishType.ANGLERFISH: 
					anglerfish.visible = true
					fish_chosen.emit(anglerfish.get_node("AnglerfishSprirtre"), "Anglerfish")
					$"Yippee!".play()
				FishType.MOBY_DICK: 
					moby_dick.visible = true
					fish_chosen.emit(moby_dick.get_node("Moby-DickSprite"), "Moby Dick")
					$"Moby Dick".play()
					success_timer.wait_time *= 2
				FishType.LEAPING_SMACKEREL: 
					leaping_smackerel.visible = true
					fish_chosen.emit(leaping_smackerel.get_node("LeapingSmackerelSprite"), "LEAPING SMACKEREL! WAHOO!")
					$"THE LEAPING SMACKEREL!".play()
					success_timer.wait_time *= 2
			
			in_game_timer.stop()
			in_game_timer.wait_time = IN_GAME_WAIT_TIME
			success_timer.start()
			
			get_window().title = "Yippee!"
			


func _on_start_game_timeout() -> void:
	if randf() < 0.2:
		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,10)
		#num_clicks = 21
		
		get_window().title = "MASH MASH MASH MASH MASH"


# 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()
	
	get_window().title = "🙂👎"


func _on_success_fail_timeout() -> void:
	gar.visible = false
	salmon.visible = false
	bass.visible = false
	humuhumunukunukuapua_a.visible = false
	catfsih.visible = false
	dogfish.visible = false
	pupperfish.visible = false
	anglerfish.visible = false
	moby_dick.visible = false
	leaping_smackerel.visible = false
	sad.visible = false
	happy.visible = false
	you.visible = true
	succfail_timer.stop()
	start_game_timer.start()
	
	get_window().title = "Relaxing Fishing Game 🙂"


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