extends Button

export(Color) var color_normal := Color(1, 1, 1)
export(Color) var color_hovered := Color(0.5, 0.5, 0.5)
export(Color) var color_pressed := Color(0.5, 0, 0.5)
export(float) var line_thickness: float = 2

# All the Comment nodes that are direct replies to this one
var _comment_nodes: Array = []
var _current_color: Color = color_normal


func _ready() -> void:
	connect("mouse_entered", self, "_on_mouse_entered")
	connect("mouse_exited", self, "_on_mouse_exited")
	connect("button_down", self, "_on_button_down")


func _draw() -> void:
	if len(_comment_nodes) == 0:
		return
	
	var y_offset := int(get_global_position().y)
	var marker_y_positions: Array = []
	for comment in _comment_nodes:
		marker_y_positions.append(comment.get_marker_y_position() - y_offset)
	var thread_y_end: int = marker_y_positions.max()
	
	# Draw the main line
	var x_right := int(get_size().x)
	var x_mid := int(get_size().x / 2)
	draw_line(Vector2(x_mid, 0), Vector2(x_mid, thread_y_end), _current_color,
			line_thickness)
	
	# Draw markers
	for marker_pos in marker_y_positions:
		draw_line(Vector2(x_mid, marker_pos), Vector2(x_right, marker_pos),
				_current_color, line_thickness)


func set_visible(vis: bool) -> void:
	_current_color = color_normal if not vis else _current_color
	.set_visible(vis)


func draw_thread_line(comment_nodes: Array) -> void:
	_comment_nodes = comment_nodes
	update()


func _on_mouse_entered() -> void:
	_current_color = color_hovered
	update()


func _on_mouse_exited() -> void:
	_current_color = color_normal
	update()


func _on_button_down() -> void:
	_current_color = color_pressed
	update()
