extends Node

var _async_helper := AsyncHelper.new(self)
onready var _claim_type: OptionButton = find_node("ClaimType")
onready var _stream_type_any: CheckBox = find_node("StreamTypeAny")
onready var _stream_type_video: CheckBox = find_node("StreamTypeVideo")
onready var _stream_type_image: CheckBox = find_node("StreamTypeImage")
onready var _stream_type_document: CheckBox = find_node("StreamTypeDocument")
onready var _duration_equality: OptionButton = find_node("DurationEquality")
onready var _duration: VSlider = find_node("Duration")
onready var _duration_text: Label = find_node("DurationText")
onready var _only_following: CheckButton = find_node("OnlyFollowing")
onready var _language: OptionButton = find_node("Language")


func _ready() -> void:
	_claim_type.connect("item_selected", self, "_on_ClaimType_item_selected")
	for stream_type \
			in [_stream_type_video, _stream_type_image, _stream_type_document]:
		stream_type.connect("toggled", self, "_on_StreamType_toggled")
	_duration.connect("value_changed", self, "_on_Duration_value_changed")
	
	_claim_type.add_item("Channel")
	_claim_type.add_item("Stream")
	_claim_type.add_item("Repost")
	_claim_type.add_item("Collection")
	_claim_type.select(1)
	
	_duration_equality.add_item(">")
	_duration_equality.add_item("<")
	
	_on_ClaimType_item_selected(0)


func get_options() -> Dictionary:
	var options: Dictionary = {
		"claim_type": _get_claim_type(),
		"stream_types": _get_stream_types(),
		"duration": _get_duration(),
		"channel_ids": yield(_get_channel_ids(), "completed"),
	}
	if not _duration.is_editable():
		options.erase("duration")
	
	return options


func _get_claim_type() -> String:
	return _claim_type.get_item_text(_claim_type.get_selected()).to_lower()


func _get_stream_types() -> Array:
	var stream_types: Array = []
	for stream_type \
			in [_stream_type_video, _stream_type_image, _stream_type_document]:
		if stream_type.is_pressed():
			stream_types.append(stream_type.get_text().to_lower())
	return stream_types


func _get_duration() -> String:
	var equality: String = \
			_duration_equality.get_item_text(_duration_equality.get_selected())
	return equality + str(int(_duration.get_value() * 60))


func _get_channel_ids() -> Array:
	var channel_ids: Array = []
	
	var tracker: Dictionary = _async_helper.connect_wrapped(
			get_tree(), "idle_frame", false)
	yield(_async_helper.wait_until_finished([tracker]), "completed")
	if _only_following.is_pressed():
		tracker = _async_helper.connect_wrapped(
				LBRYActions.get_followed_channels(), "completed")
		yield(_async_helper.wait_until_finished([tracker]), "completed")
		var followed_channels: Array = tracker["result"]
		for channel in followed_channels:
			channel_ids.append(channel.rsplit("#", true, 1)[1])
	return channel_ids


func _get_any_languages() -> Array:
	# TODO: what format are the languages meant to be given in? are only a
	# subset of languages valid?
	var languages: Array = []
	
	return languages


func _on_ClaimType_item_selected(_idx: int) -> void:
	# Stream type doesn't apply if "stream" or "repost" are not selected
	if not (_get_claim_type() in ["stream", "repost"]):
		_stream_type_video.set_disabled(true)
		_stream_type_image.set_disabled(true)
		_stream_type_document.set_disabled(true)
		_duration_equality.set_disabled(true)
		_duration.set_editable(false)
		_duration_text.set_self_modulate(Color(1, 1, 1, 0.3))
	else:
		if _stream_type_any.is_pressed() or _stream_type_video.is_pressed():
			_duration_equality.set_disabled(false)
			_duration.set_editable(true)
			_duration_text.set_self_modulate(Color(1, 1, 1, 1))
		_stream_type_video.set_disabled(false)
		_stream_type_image.set_disabled(false)
		_stream_type_document.set_disabled(false)


func _on_StreamType_toggled(_button_pressed: bool) -> void:
	var is_any_stream_type_pressed: bool = false
	var is_video_pressed: bool = false
	for stream_type \
			in [_stream_type_video, _stream_type_image, _stream_type_document]:
		if stream_type.is_pressed():
			is_any_stream_type_pressed = true
			if stream_type.get_text().to_lower() == "video":
				is_video_pressed = true
				break
	
	if is_any_stream_type_pressed:
		_stream_type_any.set_pressed(false)
	else:
		_stream_type_any.set_pressed(true)
	
	if is_video_pressed or not is_any_stream_type_pressed:
		_duration_equality.set_disabled(false)
		_duration.set_editable(true)
		_duration_text.set_self_modulate(Color(1, 1, 1, 1))
	else:
		_duration_equality.set_disabled(true)
		_duration.set_editable(false)
		_duration_text.set_self_modulate(Color(1, 1, 1, 0.3))


func _on_Duration_value_changed(value: float) -> void:
	_duration_text.set_text(str(value) + " mins")
