extends Control

@export var seed: int = 0

@onready var otherview: int = 0
@onready var selfview: int = 0
@onready var trueview: int = 0

# variables for nodes
@onready var roll_dice: TextureButton = $Roll/Roll_dice
@onready var psyline_other: Control = $Psy_line_other
@onready var psyline_self: Control = $Psy_line_self
@onready var psyline_true: Control = $Psy_line_true
@onready var traits: Control = $Traits
@onready var keep_psy: CheckBox = $Roll/Keep_psy
@onready var keep_traits: CheckBox = $Roll/Keep_traits

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	lets_roll(seed)
	roll_dice.pressed.connect(_on_roll_dice_pressed)
	
func _process(delta: float) -> void:
	# Roll new result"
	if Input.is_action_just_pressed("roll"):
		lets_roll(seed)
	
	# Exit the app
	if Input.is_action_just_pressed("ui_cancel"):
		get_tree().quit()

func _on_roll_dice_pressed() -> void:
	lets_roll(seed)

func lets_roll(chosen_seed: int) -> void:
	# Random psy profiles
	var possible_psy: int = $Psy_line_other.resources.size()
	
	var random = RandomNumberGenerator.new()
	random.seed = chosen_seed
	
	# Randomize psychology
	if not keep_psy.button_pressed:
		random.randomize()
		var result_other: int = random.randi_range(0, possible_psy - 1)
		psyline_other.choice.select(result_other)
		psyline_other.on_item_selected(result_other)
		
		random.randomize()
		var result_self: int = random.randi_range(0, possible_psy - 1)
		psyline_self.choice.select(result_self)
		psyline_self.on_item_selected(result_self)

		random.randomize()
		var result_true: int = random.randi_range(0, possible_psy - 1)
		psyline_true.choice.select(result_true)
		psyline_true.on_item_selected(result_true)
	
	# Randomize trait
	if not keep_traits.button_pressed:
		traits.populate()