import {importCharacter} from "../xml-import/xml-import.mjs";
import {LiturgyData} from "../data/miracle/liturgydata.mjs";

export class Character extends Actor {

    import() {
        let input = document.createElement('input')
        input.type = 'file'
        input.accept = '.xml'
        input.onchange = e => {
            importCharacter(this.id, e.target.files[0])
        }
        input.click()
    }

    /**
     * @override
     * Augment the actor source data with additional dynamic data. Typically,
     * you'll want to handle most of your calculated/derived data in this step.
     * Data calculated in this step should generally not exist in template.json
     * (such as ability modifiers rather than ability scores) and should be
     * available both inside and outside of character sheets (such as if an actor
     * is queried and has a roll executed directly from it).
     */
    prepareDerivedData() {

        if (this.type === "character") {
            const actorData = this;
            const systemData = actorData.system;

            const mu = systemData.attribute.mu.aktuell + systemData.attribute.mu.mod;
            const kl = systemData.attribute.kl.aktuell + systemData.attribute.kl.mod;
            const _in = systemData.attribute.in.aktuell + systemData.attribute.in.mod;
            const ch = systemData.attribute.ch.aktuell + systemData.attribute.ch.mod;

            const ff = systemData.attribute.ff.aktuell + systemData.attribute.ff.mod;
            const ge = systemData.attribute.ge.aktuell + systemData.attribute.ge.mod;
            const ko = systemData.attribute.kk.aktuell + systemData.attribute.ko.mod;
            const kk = systemData.attribute.kk.aktuell + systemData.attribute.kk.mod;


            systemData.lep.max = Math.round((ko + ko + kk) / 2) + systemData.lep.mod;
            systemData.aup.max = Math.round((mu + ko + ge) / 2) + systemData.aup.mod;
            systemData.asp.max = Math.round((mu + _in + ch) / 2) + systemData.asp.mod;


            systemData.at = Math.round((mu + ge + kk) / 5);
            systemData.pa = Math.round((_in + ge + kk) / 5);
            systemData.fk = Math.round((_in + ff + kk) / 5);

            systemData.ini.aktuell = Math.round((mu + mu + _in + ge) / 5) + systemData.ini.mod;
            systemData.mr.aktuell = Math.round((mu + kl + ko) / 5) + systemData.mr.mod;

            systemData.rs = 0;
            systemData.be = 0;

            // map current set to RS and BE

            const ausruestung = systemData.heldenausruestung[systemData.setEquipped];
            if (ausruestung) {
                if (ausruestung.brust) {
                    systemData.rs += systemData.parent.items.get(ausruestung.brust).system.armorValue ?? 0
                    systemData.be += systemData.parent.items.get(ausruestung.brust).system.armorHandicap ?? 0
                }
                if (ausruestung.bauch) {
                    systemData.rs += systemData.parent.items.get(ausruestung.bauch).system.armorValue ?? 0
                    systemData.be += systemData.parent.items.get(ausruestung.bauch).system.armorHandicap ?? 0
                }
                if (ausruestung.ruecken) {
                    systemData.rs += systemData.parent.items.get(ausruestung.ruecken).system.armorValue ?? 0
                    systemData.be += systemData.parent.items.get(ausruestung.ruecken).system.armorHandicap ?? 0
                }
                if (ausruestung.armlinks) {
                    systemData.rs += systemData.parent.items.get(ausruestung.armlinks).system.armorValue ?? 0
                    systemData.be += systemData.parent.items.get(ausruestung.armlinks).system.armorHandicap ?? 0
                }
                if (ausruestung.armrechts) {
                    systemData.rs += systemData.parent.items.get(ausruestung.armrechts).system.armorValue ?? 0
                    systemData.be += systemData.parent.items.get(ausruestung.armrechts).system.armorHandicap ?? 0
                }
                if (ausruestung.beinlinks) {
                    systemData.rs += systemData.parent.items.get(ausruestung.beinlinks).system.armorValue ?? 0
                    systemData.be += systemData.parent.items.get(ausruestung.beinlinks).system.armorHandicap ?? 0
                }
                if (ausruestung.beinrechts) {
                    systemData.rs += systemData.parent.items.get(ausruestung.beinrechts).system.armorValue ?? 0
                    systemData.be += systemData.parent.items.get(ausruestung.beinrechts).system.armorHandicap ?? 0
                }
            }


            systemData.kap.max = 0;

            // evaluate deities for KaP

            const deities = systemData.parent.items.filter(p => p.type === "Blessing")
            deities?.forEach((deity) => {
                if (LiturgyData.alverans.includes(deity.system.gottheit)) {
                    systemData.kap.max = 24;
                } else if (systemData.kap.max === 0) {
                    systemData.kap.max += 12;
                }
            }, 0)

        }


    }

    /**
     * Augment the basic Item data model with additional dynamic data.
     */
    prepareData() {
        super.prepareData();
        this.prepareEmbeddedDocuments();
    }

    getRollData() {
        const data = super.getRollData();

        if (this.type !== 'character' && this.type !== 'creature') return;

        if (data.attribute) {
            for (let [k, v] of Object.entries(data.attribute)) {
                data[k] = foundry.utils.deepClone(v);
            }
        }

        // move sonderfertigkeiten into data, if it isn't in data the actor doesn't have that sonderfertigkeit

        data.sf = {}
        if (data.sonderfertigkeiten) {
            data.sonderfertigkeiten.forEach(sf => {
                data.sf[sf.name] = sf.auswahl;
            })
            delete data.sonderfertigkeiten;
        }

        return data;
    }
}
