export class ActionManager {

    static FREE = "free-action";
    static REGULAR = "regular-action";
    static CONTINUING = "continuing-action";

    static MOVEMENT = "movement-action"
    static DEFENSE = "defense-action"
    static ATTACK = "attack-action"
    static INTERACTION = "interaction";
    static TALENT = "talent-action"
    static SPELL = "spell";
    static DEFAULT = "default"
    static SF = "special-ability"

    constructor(actor) {
        this.actor = actor
    }

    #freeActions = [
        {
            name: "Ausweichen",
            cost: ActionManager.FREE,
            type: ActionManager.DEFENSE,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Rufen",
            cost: ActionManager.FREE,
            type: ActionManager.INTERACTION,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Sich zu Boden fallen lassen",
            cost: ActionManager.FREE,
            type: ActionManager.MOVEMENT,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Waffe oder Gegenstand fallen lassen",
            cost: ActionManager.FREE,
            type: ActionManager.INTERACTION,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "getragenes Artefakt aktivieren",
            cost: ActionManager.FREE,
            type: ActionManager.INTERACTION,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Schnellziehen",
            cost: ActionManager.FREE,
            type: ActionManager.INTERACTION,
            source: ActionManager.SF,
            eval: () => this.#hatSonderfertigkeit("Schnellziehen")
        }
    ]

    #regularActions = [
        {
            name: "Angriffsaktion",
            type: ActionManager.ATTACK,
            cost: ActionManager.REGULAR,
            source: ActionManager.DEFAULT,
            eval: () => this.#hatWaffeinHand()
        },
        {
            name: "Schnellschuss",
            type: ActionManager.INTERACTION,
            cost: ActionManager.CONTINUING,
            source: ActionManager.DEFAULT,
            eval: () => this.#hatFernkampfWaffeinHand()
        },
        {
            name: "Schnellschuss (Scharfschütze)",
            type: ActionManager.INTERACTION,
            cost: ActionManager.CONTINUING,
            source: ActionManager.SF,
            eval: () => this.#hatFernkampfWaffeinHand() && this.#hatSonderfertigkeit("Scharfschütze")
        },
        {
            name: "Abwehraktion",
            type: ActionManager.DEFENSE,
            cost: ActionManager.REGULAR,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Bewegen",
            type: ActionManager.MOVEMENT,
            cost: ActionManager.REGULAR,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Position",
            type: ActionManager.MOVEMENT,
            cost: ActionManager.REGULAR,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Finte",
            type: ActionManager.ATTACK,
            cost: ActionManager.REGULAR,
            source: ActionManager.SF,
            eval: () => this.#hatFernkampfWaffeinHand() && this.#hatSonderfertigkeit("Finte")
        },
        {
            name: "Wuchtschlag",
            type: ActionManager.ATTACK,
            cost: ActionManager.REGULAR,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Wuchtschlag",
            type: ActionManager.ATTACK,
            cost: ActionManager.REGULAR,
            source: ActionManager.SF,
            eval: () => this.#hatSonderfertigkeit("Wuchtschlag")
        },
        {
            name: "Betäubungsschlag",
            type: ActionManager.ATTACK,
            cost: ActionManager.REGULAR,
            source: ActionManager.SF,
            eval: () => this.#hatSonderfertigkeit("Betäubungsschlag")
        }
    ]

    #continuingActions = [
        {
            name: "Orientieren",
            type: ActionManager.TALENT,
            cost: ActionManager.CONTINUING,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Waffe ziehen",
            type: ActionManager.INTERACTION,
            cost: ActionManager.CONTINUING,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Sprinten",
            type: ActionManager.MOVEMENT,
            cost: ActionManager.CONTINUING,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Gegenstand benutzen",
            type: ActionManager.INTERACTION,
            cost: ActionManager.CONTINUING,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Schnellladen (Bogen)",
            type: ActionManager.INTERACTION,
            cost: ActionManager.CONTINUING,
            source: ActionManager.SF,
            eval: () => this.#hatMunition() && this.#hatFernkampfWaffeinHand("Bogen") && this.#hatSonderfertigkeit("Schnellladen (Bogen)")
        },
        {
            name: "Schnellladen (Armbrust)",
            type: ActionManager.INTERACTION,
            cost: ActionManager.CONTINUING,
            source: ActionManager.SF,
            eval: () => this.#hatMunition() && this.#hatFernkampfWaffeinHand("Armbrust") && this.#hatSonderfertigkeit("Schnellladen (Armbrust)")
        },
        {
            name: "Nachladen",
            type: ActionManager.INTERACTION,
            cost: ActionManager.CONTINUING,
            source: ActionManager.DEFAULT,
            eval: () => this.#hatMunition()
        },
        {
            name: "Talenteinsatz",
            type: ActionManager.TALENT,
            cost: ActionManager.CONTINUING,
            source: ActionManager.DEFAULT,
            eval: () => true
        },
        {
            name: "Zaubern",
            type: ActionManager.SPELL,
            cost: ActionManager.CONTINUING,
            source: ActionManager.SF,
            eval: () => this.#hatSonderfertigkeitBeginnendMit("Repräsentation:")
        },
        {
            name: "Liturgie wirken",
            type: ActionManager.SPELL,
            cost: ActionManager.CONTINUING,
            source: ActionManager.SF,
            eval: () => this.#hatSonderfertigkeitBeginnendMit("Liturgiekenntnis")
        }
    ]


    #hatWaffeinHand() {
        const item = this.actor.findEquipmentOnSlot("links") ?? this.actor.findEquipmentOnSlot("rechts")
        return item
    }

    #hatMunition() {
        const item = this.actor.findEquipmentOnSlot("munition")
        const weapon = this.actor.findEquipmentOnSlot("fernkampf")
        return item
    }

    #hatFernkampfWaffeinHand(art) {
        const item = this.actor.findEquipmentOnSlot("fernkampf")
        return item
    }

    #hatSonderfertigkeitBeginnendMit(name) {
        return this.actor.system.sonderfertigkeiten?.find(p => p.name.startsWith(name)) != null
    }

    #hatSonderfertigkeit(name) {
        return this.actor.system.sonderfertigkeiten?.find(p => p.name === name) != null
    }

    evaluate() {
        let actionArray = [...this.#freeActions, ...this.#regularActions, ...this.#continuingActions]
        return actionArray.filter(action => action.eval());
    }

}
