const {
    SchemaField,
    NumberField,
    ObjectField,
    StringField,
    HTMLField,
    DocumentIdField,
    ArrayField,
} = foundry.data.fields;

/**
 *
 * @property LE
 * @property AU
 * @property AE
 * @property KE
 */
export const ResourceType = {
    LE: "Lep",
    AU: "Aup",
    AE: "Asp",
    KE: "Kap"
}

export class PlayerCharacterDataModel extends foundry.abstract.TypeDataModel {

    static defineSchema() {
        return {
            name: new StringField(),
            meta: new SchemaField({
                spezies: new StringField(),
                kultur: new StringField(),
                professions: new ArrayField(new StringField()),
                geschlecht: new StringField(),
                haarfarbe: new StringField(),
                groesse: new StringField(),
                augenfarbe: new StringField(),
                geburtstag: new StringField(),
                alter: new StringField(),
                gewicht: new StringField(),
                aussehen: new HTMLField(),
                familie: new HTMLField(),
                titel: new StringField(),
                stand: new StringField(),
                verbindungen: new HTMLField(),
                notizen: new HTMLField(),
            }),
            setEquipped: new NumberField({required: true, initial: 0, max: 3, integer: true}),
            ini: new SchemaField({
                aktuell: new NumberField({required: true, integer: true, initial: 0}),
                mod: new NumberField({required: true, integer: true, initial: 0}),
                wuerfel: new NumberField({required: true, integer: true, initial: 1}),
            }),
            lep: new SchemaField({
                aktuell: new NumberField({required: true, integer: true, initial: 0}),
                mod: new NumberField({required: true, integer: true}),
            }),
            mr: new SchemaField({
                mod: new NumberField({required: true, integer: true}),
            }),
            aup: new SchemaField({
                aktuell: new NumberField({required: true, integer: true, initial: 0}),
                mod: new NumberField({required: true, integer: true}),
            }),
            asp: new SchemaField({
                aktuell: new NumberField({required: true, integer: true, initial: 0}),
                mod: new NumberField({required: true, integer: true}),
            }),
            kap: new SchemaField({
                aktuell: new NumberField({required: true, integer: true, initial: 0}),
                mod: new NumberField({required: true, integer: true}),
            }),
            gs: new SchemaField({
                aktuell: new NumberField({required: true, integer: true, initial: 0}),
                mod: new NumberField({required: true, integer: true}),
            }),
            ws: new NumberField({required: true, integer: true, initial: 0}),
            attribute: new SchemaField({
                mu: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                kl: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                in: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                ch: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                ff: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                ge: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                ko: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                kk: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                at: new SchemaField({
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                pa: new SchemaField({
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                fk: new SchemaField({
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                so: new SchemaField({
                    start: new NumberField({required: true, integer: true}),
                    aktuell: new NumberField({required: true, integer: true}),
                    mod: new NumberField({required: true, integer: true}),
                }),
                gilde: new StringField(),
            }),

            cooldowns: new ArrayField(new SchemaField({
                start: new NumberField({required: true, integer: true}),
                current: new NumberField({required: true, integer: true}),
                type: new StringField({required: true}), // "MANEUVER", "SPELL", "LITURGY", "TALENT"
                data: new ObjectField()
            })),

            kampfwerte: new ArrayField(new SchemaField({
                name: new StringField(),
                at: new NumberField({required: true, integer: true}),
                pa: new NumberField({required: true, integer: true}),
            })),
            notizen: new ArrayField(new SchemaField({
                key: new StringField(),
                notiz: new StringField(),
            })),
            erschoepfung: new SchemaField({ // only with DSA_4-1.optional_erschoepfung
                max: new NumberField({required: true, integer: true}),
                aktuell: new NumberField({required: true, integer: true}),
            }),
            wunden: new SchemaField({
                aktuell: new NumberField({required: true, integer: true}), // only with DSA_4-1.optional_trefferzonen = false
                max: new NumberField({required: true, integer: true}), // only with DSA_4-1.optional_trefferzonen = false
                mod: new NumberField({required: true, integer: true}), // only with DSA_4-1.optional_trefferzonen = false
                armlinks: new NumberField({required: true, integer: true, initial: 0}),
                armrechts: new NumberField({required: true, integer: true, initial: 0}),
                beinlinks: new NumberField({required: true, integer: true, initial: 0}),
                beinrechts: new NumberField({required: true, integer: true, initial: 0}),
                bauch: new NumberField({required: true, integer: true, initial: 0}),
                brust: new NumberField({required: true, integer: true, initial: 0}),
                kopf: new NumberField({required: true, integer: true, initial: 0}),
            }),
            heldenausruestung: new ArrayField(
                new SchemaField({
                    links: new DocumentIdField(),
                    rechts: new DocumentIdField(),
                    brust: new DocumentIdField(),
                    ruecken: new DocumentIdField(),
                    kopf: new DocumentIdField(),
                    fernkampf: new DocumentIdField(),
                    munition: new DocumentIdField(),
                    armlinks: new DocumentIdField(),
                    armrechts: new DocumentIdField(),
                    bauch: new DocumentIdField(),
                    beinlinks: new DocumentIdField(),
                    beinrechts: new DocumentIdField(),
                }), {min: 0, max: 3}
            )
        }
    }

    _initialize(options) {
        super._initialize(options);
    }

    _initializeSource(data, options) {
        if (data.heldenausruestung.length === 0) {
            let sets = [];
            for (let i = 0; i < 3; i++) {
                const preppedSet = {}
                PlayerCharacterDataModel.getSlots().forEach(slot => {
                    preppedSet[slot] = null;
                })
                sets.push(preppedSet);
            }
            data.heldenausruestung = sets
        }
        return super._initializeSource(data, options);
    }

    async _onCreate(data, options, userId) {
    }

    static getSlots() {
        return [
            "links",
            "rechts",
            "brust",
            "ruecken",
            "kopf",
            "fernkampf",
            "munition",
            "armlinks",
            "armrechts",
            "bauch",
            "beinlinks",
            "beinrechts",

        ]
    }

}
