import {PlayerCharacterDataModel} from "../data/character.mjs";
import {GroupDataModel} from "../data/group.mjs";
import {CreatureDataModel} from "../data/creature.mjs";
import {MerchantDataModel} from "../data/merchant.mjs";
import {SkillDataModel} from "../data/skill.mjs";
import {SpellDataModel} from "../data/spell.mjs";
import {AdvantageDataModel} from "../data/advantage.mjs";
import {EquipmentDataModel} from "../data/equipment.mjs";
import {LiturgyDataModel} from "../data/liturgy.mjs";
import {BlessingDataModel} from "../data/blessing.mjs";
import {SpecialAbilityDataModel} from "../data/specialAbility.mjs";
import {ActiveEffectDataModel} from "../data/activeEffect.mjs";
import {ProfessionDataModel} from "../data/profession.mjs";
import {SpeciesDataModel} from "../data/species.mjs";
import {CultureDataModel} from "../data/culture.mjs";
import {Trefferzone, Wunde, Zonenruestung, Zonenwunde} from "../data/trefferzone.mjs";
import {RestingDialog} from "../dialog/restingDialog.mjs";
import {BattleDialog} from "../dialog/battleDialog.mjs";
import {Talent} from "../data/talent.mjs";
import {Character} from "../documents/character.mjs";
import {currency} from "../handlebar-helpers/currency.mjs";
import {DeityDataModel} from "../data/deity.mjs";
import {ItemBrowserDialog} from "../dialog/itemBrowserDialog.mjs";
import {displayRoll} from "../globals/displayRoll.js";
import {evaluateRoll} from "../globals/DSARoll.mjs";

function initGlobalAccess() {

    return {
        Zonenruestung,
        Zonenwunde,
        Trefferzone,
        Wunde,
        RestingDialog,
        BattleDialog,
        ItemBrowserDialog,
        Talent,
        displayCurrency: currency,
        displayRoll,
        evaluateRoll
    }

}

function initDocumentClasses(config) {
    config.Actor.documentClass = Character
}

function initDataModels(config) {
    config.Actor.dataModels = {
        Character: PlayerCharacterDataModel,
        Group: GroupDataModel,
        Creature: CreatureDataModel,
        Merchant: MerchantDataModel,
    }

    config.Item.dataModels = {
        Skill: SkillDataModel,
        Spell: SpellDataModel,
        Advantage: AdvantageDataModel,
        Equipment: EquipmentDataModel,
        Liturgy: LiturgyDataModel,
        Blessing: BlessingDataModel,
        SpecialAbility: SpecialAbilityDataModel,
        ActiveEffect: ActiveEffectDataModel,
        Profession: ProfessionDataModel,
        Spezies: SpeciesDataModel,
        Kultur: CultureDataModel,
        Deity: DeityDataModel,
    }
}

function initCombat(config) {
    config.Combat.initiative = {
        formula: `(@ini.wuerfel)d6 + @ini.aktuell`,
        decimals: 0
    }
}

function initSocketLib() {


    Hooks.on("socketlib.ready", () => {
        let socket = socketlib.registerSystem("DSA_4-1")
        socket.register("removeFromLootTable", removeFromLootTable)
        socket.register("buyFromLootTable", buyFromLootTable)
        if (!game.DSA41) {
            game.DSA41 = {}
        }
        game.DSA41.socket = socket
    })


    async function removeFromLootTable(actorId, itemId) {
        if (actorId && game.actors.get(actorId)) {
            const actor = game.actors.get(actorId)

            return await actor.deleteEmbeddedDocuments('Item', [itemId])
        }
    }

    async function buyFromLootTable(actorId, itemId) {
        if (actorId && game.actors.get(actorId)) {
            const actor = game.actors.get(actorId)
            const item = actor.items.find(p => p.id === itemId)
            if (item.system.quantity != -1) { // -1 means infinite
                if (item.system.quantity > 1) {
                    item.update({'system.quantity': item.system.quantity - 1})
                } else {
                    actor.deleteEmbeddedDocuments('Item', [item._id]) // delete when the quantity is equal to 0
                }
            }
            return true
        }
    }

}

export {
    initSocketLib,
    initGlobalAccess,
    initDocumentClasses,
    initDataModels,
    initCombat,
}