extends Node

export (PackedScene) var FirstBad
export (PackedScene) var BlobBad
export (PackedScene) var LaserBad

var position = Vector2(200, 200)
var total_bads_spawned = 0
var upgrade_cost = 50
var booting = true
var rectangle_opacity = 1

const BADDIE_WAIT_TIME_DEFAULT = 5

func _ready():
	$ColorRect.visible = true
	var mainmenu = preload("res://MainMenu.tscn").instance()
	add_child(mainmenu)
	mainmenu.connect("multiplayer_menu", self, "_open_multiplayer_menu")
	get_tree().paused = true
	$BaddieTimer.wait_time = BADDIE_WAIT_TIME_DEFAULT
	$BaddieTimer.start()
	randomize()
	updatePoints()

var touchy_shooty = false

func _open_multiplayer_menu():
	var multiplayer = preload("res://Networking.tscn").instance()
	add_child(multiplayer)
	get_tree().paused = true

func _process(delta):
	if (Input.is_action_pressed("ctlr_l")):
		if (Input.is_action_pressed("ctlr_r")):
			if (Input.is_action_pressed("ctlr_a")):
				if (Input.is_action_pressed("ctlr_start")):
					get_tree().quit()
	if (Input.is_action_pressed("ui_quit")):
		get_tree().quit()
	updatePoints()
	
	if get_tree().has_network_peer():
		if !get_tree().is_network_server():
			$BaddieTimer.stop()
	
	if (touchy_feely && (abs(touchy_feely.position.x - $Player.position.x) > 1)):
		if (touchy_feely.position.x < 800):
			$Player.moveto(touchy_feely.position)
	
	if (touchy_shooty && $Player.can_shoot):
		$Player.shoot()

	if booting == true:
		$ColorRect.color = Color(0, 0, 0, rectangle_opacity)
		rectangle_opacity -= delta/1.25
		if rectangle_opacity <= 0:
			booting = false
			$ColorRect.visible = false

func _on_bad_death(kill_money):
	$Player.money += kill_money
	updatePoints()

var a_round_of_bads = 20
var sendblob = 1
var bad_health_multi = 1
func BaddieTimer():
	if total_bads_spawned <= a_round_of_bads:
		var bad_type
		var badposition = Vector2()
		if sendblob%20 == 0:
			bad_type = 2
			sendblob += 1
		elif sendblob%5 == 0:
			bad_type = 1
			sendblob += 1
		else:
			bad_type = 0
			sendblob += 1
		
		total_bads_spawned += 1
		if $BaddieTimer.wait_time > 0.5:
			$BaddieTimer.wait_time = $BaddieTimer.wait_time * 0.975
		if total_bads_spawned == a_round_of_bads:
			bad_health_multi *= 1.5
		
		badposition.x = 1200
		badposition.y = (randi()%410) + 50
		if get_tree().is_network_server():
			rpc("spawnBad", bad_type, badposition, bad_health_multi)
		else:
			spawnBad(bad_type, badposition, bad_health_multi)
	else:
		total_bads_spawned = 0

sync func spawnBad(bad_type, position, health_multi):  ### sync func ?
	var bad 
	if bad_type == 0:
		bad = FirstBad.instance()
	if bad_type == 1:
		bad = BlobBad.instance()
	if bad_type == 2:
		bad = LaserBad.instance()
		
	add_child(bad)
	bad.connect("dead", self, "_on_bad_death")
	bad.health_multi = health_multi
	bad.position = position

func _on_PauseButton_pressed():
	$Player.upgradeMenu()

func updatePoints():
	$MoneyDisplay.text = str($Player.money, " points")

func _on_Player_update_display():
	updatePoints()

func _on_Mothership_game_over():
	$ShootButton.visible = false
	$Player.gameOver()

func _on_Player_restart_game():
	for child in self.get_children():
		if (child.has_method("_on_Visibility_screen_exited")):
			child.queue_free()
	total_bads_spawned = 0
	$BaddieTimer.wait_time = BADDIE_WAIT_TIME_DEFAULT
	$ShootButton.visible = true

var touchy_feely

#func _input(event):
#	if (event is InputEventScreenTouch || event is InputEventScreenDrag):
#		if event.position.x < 800:
#			touchy_feely = event
#			prints(event.index)
#		else:
#			touchy_shooty = true
#			

var touchscreen_on = false
func _input(event):
	if (event is InputEventScreenTouch || event is InputEventScreenDrag):
		touchscreen_on = true
		if event.position.x < 800: #&& (abs (event.position.y - $Player.position.y) < 100) :
			$Player.position.x = event.position.x + 100*(event.position.x/800)
			$Player.position.y = event.position.y - 100
			prints(event.index)
		else:
			touchy_shooty = true

func other_shooting_upgrade(id, other_bullet_delay):
	prints("Other player shooting speed upgrade")
	$OtherPlayer.timer.set_wait_time(other_bullet_delay)

func _on_Player_multiplayer_movement(id, position, is_shooting):
	$OtherPlayer.visible = true
	$OtherPlayer.username = str(id)
	$OtherPlayer.position = position
	if is_shooting:
		$OtherPlayer.shoot()
