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

remote func move_player(id, position):
	prints("MAIN SAYS:", id, position)
	pass

func _ready():
	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 (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
		if sendblob%20 == 0:
			bad = LaserBad.instance()
			sendblob += 1
		elif sendblob%5 == 0:
			bad = BlobBad.instance()
			sendblob += 1
		else:
			bad = FirstBad.instance()
			sendblob += 1
		
		add_child(bad)
		bad.connect("dead", self, "_on_bad_death")
		bad.health_multi = bad_health_multi
		
		total_bads_spawned += 1
		if $BaddieTimer.wait_time > 0.5:
			$BaddieTimer.wait_time = $BaddieTimer.wait_time * 0.99
		if total_bads_spawned == a_round_of_bads:
			bad_health_multi *= 1.5
		
		updatePoints()
		
		bad.position.x = 1200
		bad.position.y = (randi()%410) + 50
	else:
		total_bads_spawned = 0

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