
extends Button

var currentType = 0
var TypeIndex
var EntryIndex
var ComponentLabel
var PhrasePlayer

signal ButtonClicked(entryType, entryValue, entryString)

const Entries = [
	[
		"Increase",
		"Strengthen",
		"Run",
		"Make"
	],
	[
		"Cut",
		"Defund",
		"Dismantle",
		"Undermine",
		"Repeal",
		"Reject",
		"Eliminate"
	],
	[
		"Tax Increase",
		"Death Tax",
		"Obamacare",
		"Entitlements",
		"Regulations",
		"Gun Control",
		"Evil Government"
	],
	[
		"Rebuilding Our Military",
		"Homes and Families Safe",
		"Guantanemo Bay",
		"Job Creators",
		"Balanced Budget",
		"Peace Through Strength",
		"The Consitution",
		"Free Enterprise System",
	]
]

var InitialPosition = Vector2(0, 0)
var DestPosition = Vector2(0, 0)
var TravelTime = 0
var CurrentTime = 0
var RotationSpeed = 0
var CurrentRotation = 0

func _ready():
	set_process(true)
	
	CurrentRotation = (randf() * 2) - 1
	set_rotation(CurrentRotation)
	RotationSpeed = (randf() * 0.5) - .25
	
	TravelTime = randi() % 10 + 4
	
	PhrasePlayer = get_node("PhrasePlayer")
	
	var position = Vector2(0, 0)
	var entrySide = randi() % 4 # 0 left, 1 right, 2 top, 3 bottom
	if entrySide <= 1:
		InitialPosition.y = randi() % 600
		DestPosition.y = 600 - InitialPosition.y
		if entrySide == 0:
			InitialPosition.x = -300
			DestPosition.x = 1074
		else:
			InitialPosition.x = 1074
			DestPosition.x = -300
	else:
		InitialPosition.x = randi() % 1024
		DestPosition.x = 1024 - InitialPosition.x
		if entrySide == 2:
			InitialPosition.y = -130
			DestPosition.y = 650
		else:
			InitialPosition.y = 650
			DestPosition.y = -130
		
	set_pos(InitialPosition)

func setType(type):
	ComponentLabel = get_node("Label")
	TypeIndex = type
	EntryIndex = randi() % Entries[TypeIndex].size()
	ComponentLabel.set_text(Entries[TypeIndex][EntryIndex])

func _process(delta):
	CurrentTime += delta
	if CurrentTime > TravelTime:
		queue_free()
	
	var InterpPosition = InitialPosition + (DestPosition - InitialPosition) * (CurrentTime / TravelTime)
	set_pos(InterpPosition)
	
	CurrentRotation += RotationSpeed * delta
	set_rotation(CurrentRotation)


func _on_TextComponentButton_pressed():
	PhrasePlayer.play(Entries[TypeIndex][EntryIndex])
	self.emit_signal("ButtonClicked", TypeIndex, EntryIndex, Entries[TypeIndex][EntryIndex])
