const {HandlebarsApplicationMixin, DocumentSheetV2} = foundry.applications.api
const {ActorSheetV2} = foundry.applications.sheets

export class StandaloneSkills extends HandlebarsApplicationMixin(ActorSheetV2) {

    /** @inheritDoc */
    static DEFAULT_OPTIONS = {
        position: {width: 520, height: 480},
        classes: ['dsa41', 'sheet', 'actor', 'character', 'standalone', 'skills'],
        tag: 'form',
        actions: {
            rollCombatSkill: StandaloneSkills.#rollCombatSkill,
            rollSkill: StandaloneSkills.#rollSkill,
            openEmbeddedDocument: StandaloneSkills.#openEmbeddedDocument,

        }
    }

    /** @inheritDoc */
    static PARTS = {
        form: {
            template: `systems/DSA_4-1/templates/actor/character/standalone/skills.hbs`
        }
    }

    _actor = null

    constructor(actor) {
        super(actor)
        this._actor = actor
        this.render(true)
    }

    static async #rollCombatSkill(event, target) {
        this._actor?.sheet.options.actions.rollCombatSkill.bind(this)(event, target)
    }

    static async #rollSkill(event, target) {
        this._actor?.sheet.options.actions.rollSkill.bind(this)(event, target)
    }

    static async #openEmbeddedDocument(event, target) {
        this._actor?.sheet.options.actions.openEmbeddedDocument.bind(this)(event, target)
    }

    _configureRenderOptions(options) {
        super._configureRenderOptions(options)

        options.window.title = `${this.document.name}: Talente`

        return options
    }

    async _prepareContext(context, options, object) {
        const actorData = this.document
        context.system = actorData.system
        context.flags = actorData.flags
        context.derived = this.document.system
        context.originalName = actorData.name
        context.name = context.derived.name ?? actorData.name
        context.effects = actorData.effects ?? []

        const prepareEigenschaftRoll = (actorData, name) => {
            if (name && name !== "*") {
                return actorData.system.attribute[name.toLowerCase()].aktuell
            } else {
                return 0
            }
        }

        context.skills = {};
        context.flatSkills = [];

        actorData.itemTypes.Skill.forEach((item, index) => {

                const talentGruppe = item.system.gruppe;
                const eigenschaften = Object.values(item.system.probe);
                const werte = [
                    {name: eigenschaften[0], value: prepareEigenschaftRoll(actorData, eigenschaften[0])},
                    {name: eigenschaften[1], value: prepareEigenschaftRoll(actorData, eigenschaften[1])},
                    {name: eigenschaften[2], value: prepareEigenschaftRoll(actorData, eigenschaften[2])}
                ]
                if (context.skills[talentGruppe] == null) {
                    context.skills[talentGruppe] = [];
                }
                const obj = {
                    type: "talent",
                    gruppe: talentGruppe,
                    name: item.name.replace(/Sprachen kennen/g, "Sprache:").replace(/Lesen\/Schreiben/g, "Schrift: "),
                    taw: "" + item.system.taw,
                    tawPath: `system.items.${index}.taw`,
                    werte,
                    rollEigenschaft1: werte[0].value,
                    rollEigenschaft2: werte[1].value,
                    rollEigenschaft3: werte[2].value,
                    eigenschaft1: werte[0].name,
                    eigenschaft2: werte[1].name,
                    eigenschaft3: werte[2].name,
                    probe: `(${eigenschaften.join("/")})`,
                    id: item._id,
                    at: item.system.at,
                    pa: item.system.pa,
                    komplexität: item.system.komplexität,
                    fav: item.getFlag("DSA_4-1", "favourite")
                };

                if (talentGruppe === "Kampf") {

                    if (item.system.pa != null) { // has no parry value so it must be ranged talent (TODO: but it isnt as there can be combatstatistics which has no pa value assigned to)
                        obj.at = item.system.at + context.derived.at.aktuell
                        obj.pa = item.system.pa + context.derived.pa.aktuell
                    } else {
                        obj.at = item.system.at + context.derived.fk.aktuell
                    }
                }

                context.skills[talentGruppe].push(obj);
                context.flatSkills.push(obj);
            }
        )

        return context
    }

    _onRender(context, options) {
    }
}