import {LiturgyData} from "../../data/miracle/liturgyData.mjs";

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

export class StandaloneLiturgies extends HandlebarsApplicationMixin(ActorSheetV2) {

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

        }
    }

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

    _actor = null

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

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

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

    _configureRenderOptions(options) {
        super._configureRenderOptions(options)

        options.window.title = `${this.document.name}: Segnungen und Liturgien`

        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 ?? []
        context.liturgies = [];
        context.blessings = [];

        actorData.itemTypes.Blessing.forEach((item, index) => {
            context.blessings.push({
                deity: item.system.gottheit,
                value: item.system.wert
            })
        })
        actorData.itemTypes.Liturgy.forEach((item, index) => {

            context.blessings.forEach(({deity, value}) => {
                let insertObject = context.liturgies.find(p => p.deity === deity);
                if (!insertObject) {
                    insertObject = {
                        deity: deity,
                        lkp: value,
                        O: [],
                        I: [],
                        II: [],
                        III: [],
                        IV: [],
                        V: [],
                        VI: [],
                        VII: [],
                        VIII: [],
                        "NA": [],
                        countO: 1,
                        countI: 1,
                        countII: 1,
                        countIII: 1,
                        countIV: 1,
                        countV: 1,
                        countVI: 1,
                        countVII: 1,
                        countVIII: 1,
                        countNA: 0,
                        total: 3,

                    }
                    context.liturgies.push(insertObject);
                }

                // sort by rank
                const rankData = LiturgyData.getRankOfLiturgy(item.system, deity)
                if (rankData) {
                    let {index, name, lkp, mod, costKaP} = rankData;

                    insertObject["count" + name] = insertObject["count" + name] + 1;

                    insertObject[name]?.push({
                        id: item._id,
                        name: item.name,
                        lkpReq: lkp,
                        lkpMod: mod,
                        costKaP,
                        fav: item.getFlag("DSA_4-1", "favourite"),
                        rank: index,  // get effective liturgy rank based on deity
                        liturgiekenntnis: deity,
                    })
                    insertObject.total = insertObject.total + 2;

                }
            })
        })


        // clean up counter
        Object.values(context.liturgies).forEach((litObject) => {

            if (litObject.I.length === 0) litObject.countI = false;
            if (litObject.II.length === 0) litObject.countII = false;
            if (litObject.III.length === 0) litObject.countIII = false;
            if (litObject.IV.length === 0) litObject.countIV = false;
            if (litObject.V.length === 0) litObject.countV = false;
            if (litObject.VI.length === 0) litObject.countVI = false;
            if (litObject.VII.length === 0) litObject.countVII = false;
            if (litObject.VIII.length === 0) litObject.countVIII = false;
            if (litObject.NA.length === 0) litObject.countNA = false;


        })

        context.hasLiturgies = context.blessings.length > 0;

        return context
    }

    _onRender(context, options) {
    }
}