export class CreatureSheet extends foundry.appv1.sheets.ActorSheet {
    /**@override */
    static get defaultOptions() {
        return foundry.utils.mergeObject(super.defaultOptions, {
            classes: ['dsa41', 'sheet', 'actor', 'creature'],
            width: 520,
            height: 480,
            tabs: [
                {
                    navSelector: '.sheet-tabs',
                    contentSelector: '.sheet-body',
                    initial: 'meta',
                },
            ],
        });
    }

    /** @override */
    get template() {
        return `systems/DSA_4-1/templates/actor/actor-creature-sheet.hbs`;
    }

    /** @override */
    getData() {

        const context = super.getData();
        const actorData = context.data;

        context.attacks = [];

        actorData.system.attacks.forEach((attack, index) => {
            context.attacks.push({
                namepath: `system.attacks.${index}.name`,
                name: attack.name,
                atpath: `system.attacks.${index}.at`,
                at: attack.at,
                papath: `system.attacks.${index}.pa`,
                pa: attack.pa,
                tppath: `system.attacks.${index}.tp`,
                tp: attack.tp,
                index: index,

            })
        })


        return context;
    }

    activateListeners(html) {
        super.activateListeners(html);

        // Everything below here is only needed if the sheet is editable
        if (!this.isEditable) return;

        html.on('click', '.remove-attack', async (evt) => {
            const {index} = evt.target.dataset;
            let sans = Array.from(this.object.system.attacks);
            sans.splice(index, 1);
            await this.object.update({'system.attacks': sans})
        })

        html.on('click', '.attacks-die.die', async (evt) => {
            const {rollType, rollName, roll} = evt.currentTarget.dataset;
            let r = new Roll(roll, this.actor.getRollData());
            const label = `${rollType} (${rollName})`
            await r.toMessage({
                speaker: ChatMessage.getSpeaker({actor: this.object}),
                flavor: label,
                rollMode: game.settings.get('core', 'rollMode'),
            });
        })

        html.on('click', '.editor .add-attack', async (evt) => {
            const name = html[0].querySelector('#attack_name').value
            const at = html[0].querySelector('#attack_at').value
            const pa = html[0].querySelector('#attack_pa').value
            const tp = html[0].querySelector('#attack_tp').value

            const newAttack = {
                name,
                at,
                pa,
                tp
            }

            await this.object.update({'system.attacks': [...this.object.system.attacks, newAttack]})

            evt.target.parentElement.querySelector('#attack_name').value = ""
            evt.target.parentElement.querySelector('#attack_at').value = ""
            evt.target.parentElement.querySelector('#attack_pa').value = ""
            evt.target.parentElement.querySelector('#attack_tp').value = ""
        })

    }

}
