extends Node

@export var current_chapter := 0
@export var current_section := 0
@export var game_seconds_all := 0
@export var game_seconds_current := 0

var timer: Timer

func _ready() -> void:
	timer = Timer.new()
	timer.wait_time = 1
	timer.one_shot = false
	timer.timeout.connect(_on_timer_timeout)
	add_child(timer)
	timer.start()


func _on_timer_timeout():
	game_seconds_all += 1
	game_seconds_current += 1
	GlobalConfigManager.config.game_total_seconds += 1


func pack_current_time():
	var packed_time = PackedTime.new()
	packed_time.time = Time.get_datetime_string_from_system(false, true)
	packed_time.chapter = current_chapter
	packed_time.section = current_section
	packed_time.game_archive_id = ArchiveManager.current_archive_id
	packed_time.game_seconds_all = game_seconds_all
	packed_time.game_seconds_current = game_seconds_current


# for log use
func get_concise_timemark() -> String:
	var hour := game_seconds_current / 3600 as int
	var minute := (game_seconds_current % 3600) / 60 as int
	var second := game_seconds_current % 60
	return (
		"r"
		+ str(ArchiveManager.current_archive_id)
		+ "_c"
		+ str(current_chapter)
		+ "_s"
		+ str(current_section)
		+ " "
		+ str(hour)
		+ ":"
		+ str(minute)
		+ ":"
		+ str(second)
	)

func get_data_res() -> Resource:
	return pack_current_time()

func load_data_res(data: Resource) -> void:
	var packed_time = data as PackedTime
	game_seconds_all = packed_time.game_seconds_all
	game_seconds_current = packed_time.game_seconds_current
	current_chapter = packed_time.chapter
	current_section = packed_time.section
