#背包实现
extends VBoxContainer   

var _hand_outro: Tween
var _label_outro: Tween
#@onready var item_bar = $ItemBar
@onready var prev = $ItemBar/Prev
@onready var prop = $ItemBar/Use/Prop
@onready var hand = $ItemBar/Use/Hand
@onready var next = $ItemBar/Next
@onready var label = $Label
@onready var timer = $Label/Timer


func _ready() -> void:
	#SceneManager.inventory.add_item(preload("res://items/1014_yaoshi.tres"))
	#SceneManager.inventory.add_item(preload("res://items/3014_yaoshi.tres"))
	hand.hide()
	hand.modulate.a = 0.0
	label.hide()
	label.modulate.a = 0.0
	
	SceneManager.inventory.changed.connect(_update_ui)
	_update_ui(true)

	
#设置在屏幕中点击任意位置，互动手图案消失
func _input(event: InputEvent) -> void:
	if  event.is_action_pressed("click") and SceneManager.inventory.active_item:
		SceneManager.inventory.set_deferred("active_item", true)
		
		_hand_outro = create_tween()
		_hand_outro.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_SINE).set_parallel()
		_hand_outro.tween_property(hand, "scale", Vector2.ONE * 0.5, 0.15) #Vector2.ONE * 0.5 设置手缩放的大小.0.15表示消失的速度
		_hand_outro.tween_property(hand, "modulate:a", 0.0, 0.15)
		_hand_outro.chain().tween_callback(hand.hide)
		
		
		
func _update_ui(is_init = false): #is_inte = false 让背包物品在最开始出现时没有动画效果
	var count = SceneManager.inventory.get_item_count()
	prev.disabled = count < 2
	next.disabled = count < 2
	visible = count > 0
	
	var item = SceneManager.inventory.get_current_item()
	if not item:
		return
	else:
		label.text = item.description
		prop.texture = item.prop_texture		
		#item_bar.modulate.a = 1
		#print("prop")

#添加背包物品在左右滑动时的弹出动画效果

	if is_init:
		return
		
	var tween := create_tween()
	tween.set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_BACK)
	tween.tween_property(prop, "scale", Vector2.ONE, 0.15).from(Vector2.ZERO)
	
	_show_label()
	
func _show_label() -> void:
	if _label_outro and _label_outro.is_valid():
		_label_outro.kill()
		_label_outro = null
	label.show()
	var tween = create_tween()
	tween.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_SINE)
	tween.tween_property(label,"modulate:a",1.0,0.2)
	tween.tween_callback(timer.start)
	
		

func _on_prev_pressed() -> void:
	SoundManager.play_sfx("interact")
	SceneManager.inventory.select_prev()
	print("prev1")

func _on_next_pressed() -> void:
	SoundManager.play_sfx("interact")
	SceneManager.inventory.select_next()
	print("next2")
	
@onready var use = $ItemBar/Use

		
func _on_use_pressed() -> void:
	if use.button_mask == MOUSE_BUTTON_MASK_LEFT:
		SceneManager.inventory.active_item = SceneManager.inventory.get_current_item()
		print("use3")
		
		if _hand_outro and _hand_outro.is_valid():
			_hand_outro.kill()
			_hand_outro = null
		hand.show()
		var tween = create_tween()
		tween.set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_BACK).set_parallel()
		tween.tween_property(hand, "scale", Vector2.ONE, 0.15).from(Vector2.ZERO)
		tween.tween_property(hand, "modulate:a", 1.0, 0.15)
		
		_show_label()
		
		
func _on_timer_timeout():
	_label_outro = create_tween()
	_label_outro.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_SINE)
	_label_outro.tween_property(label,"modulate:a",0.0,0.2)
	_label_outro.chain().tween_callback(label.hide)
