class_name GameEventMatcher extends Resource

# match event under certain conditions

# can't be empty
@export var entity: String
# empty means any action
@export var entity_actions: Array[String]
# empty means any scene
@export var scenes: Array[String]
# regex matcher, "[chapter]_[section]", * means any
# e.g. 3_* means any section in chapter 3
# empty means any chapter_section
@export var chapter_section: Array[String]

# check if the event matches the conditions
func match(event: GameEvent) -> bool:
    if !entity or entity != event.entity:
        return false
    if entity_actions.size() > 0 and !entity_actions.has(event.entity_action):
        return false
    if scenes.size() > 0 and !scenes.has(event.scene):
        return false
    if chapter_section.size() > 0:
        var chapter := str(event.packed_time.chapter)
        var section := str(event.packed_time.section)
        var matched := false
        for chapter_section_matcher in chapter_section:
            # regex match
            var parts = chapter_section_matcher.split("_")
            if parts.size() != 2:
                continue
            var chapter_matcher = parts[0]
            var section_matcher = parts[1]
            if chapter_matcher == "*" or chapter_matcher == chapter:
                matched = true
                break
            if section_matcher == "*" or section_matcher == section:
                matched = true
                break
        if !matched:
            return false
    return true