// SPDX-FileCopyrightText: 2022 Johannes Loher
//
// SPDX-License-Identifier: MIT

import {
    getActorUpdateDataGetter,
    getCompendiumMigrator,
    getSceneUpdateDataGetter,
    migrateActors,
    migrateCompendiums,
    migrateItems,
    migrateScenes,
} from "./migrationHelpers";

import type { DS4SpellDataSourceData } from "../item/spell/spell-data-source";

async function migrate(): Promise<void> {
    await migrateItems(getItemUpdateData);
    await migrateActors(getActorUpdateData);
    await migrateScenes(getSceneUpdateData);
    await migrateCompendiums(migrateCompendium);
}

function getItemUpdateData(itemData: Partial<foundry.data.ItemData["_source"]>) {
    if (itemData.type !== "spell") return;
    // @ts-expect-error spellCategory is removed with this migration
    const spellCategory: string | undefined = itemData.data?.spellCategory;
    const spellGroups = migrateSpellCategory(spellCategory);

    // @ts-expect-error bonus is removed with this migration
    const bonus: string | undefined = itemData.data?.bonus;
    const spellModifier = migrateBonus(bonus);

    const updateData: Record<string, unknown> = {
        data: {
            spellGroups,
            "-=spellCategory": null,
            spellModifier,
            "-=bonus": null,
        },
    };
    return updateData;
}

function migrateSpellCategory(spellCategory: string | undefined): DS4SpellDataSourceData["spellGroups"] {
    const spellGroups = {
        lightning: false,
        earth: false,
        water: false,
        ice: false,
        fire: false,
        healing: false,
        light: false,
        air: false,
        transport: false,
        damage: false,
        shadow: false,
        protection: false,
        mindAffecting: false,
        demonology: false,
        necromancy: false,
        transmutation: false,
        area: false,
    };
    switch (spellCategory) {
        case "healing": {
            spellGroups.healing = true;
            break;
        }
        case "fire": {
            spellGroups.fire = true;
            break;
        }
        case "ice": {
            spellGroups.ice = true;
            break;
        }
        case "light": {
            spellGroups.light = true;
            break;
        }
        case "darkness": {
            spellGroups.shadow = true;
            break;
        }
        case "mindAffecting": {
            spellGroups.mindAffecting = true;
            break;
        }
        case "electricity": {
            spellGroups.lightning = true;
            break;
        }
    }
    return spellGroups;
}

function migrateBonus(bonus: string | undefined): DS4SpellDataSourceData["spellModifier"] {
    const spellModifier = { numerical: 0, complex: "" };
    if (bonus) {
        if (Number.isNumeric(bonus)) {
            spellModifier.numerical = +bonus;
        } else {
            spellModifier.complex = bonus;
        }
    }
    return spellModifier;
}

const getActorUpdateData = getActorUpdateDataGetter(getItemUpdateData);
const getSceneUpdateData = getSceneUpdateDataGetter(getActorUpdateData);
const migrateCompendium = getCompendiumMigrator({ getItemUpdateData, getActorUpdateData, getSceneUpdateData });

export const migration = {
    migrate,
    migrateCompendium,
};