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

import {
  getSceneMigrator,
  migrateCollection,
  migrateCompendiums,
  getCompendiumMigrator,
  getActorMigrator,
} from "./migrationHelpers.js";

/** @type {import("./migration.js").Migration["migrate"]} */
async function migrate() {
  const itemsResult = await migrateCollection(game.items, migrateItem);
  const actorsResult = await migrateCollection(game.actors, migrateActor);
  const scenesResult = await migrateCollection(game.scenes, migrateScene);
  const compendiumsResult = await migrateCompendiums(migrateCompendium);
  return itemsResult === "error" ||
    actorsResult === "error" ||
    scenesResult === "error" ||
    compendiumsResult === "error"
    ? "error"
    : "success";
}

/** @type {import('./migrationHelpers.js').Migrator<Item>} */
async function migrateItem(item) {
  if (item.type === "spell") {
    const spellCategory = item.system?.spellCategory;
    const spellGroups = migrateSpellCategory(spellCategory);
    const bonus = itemData.system?.bonus;
    const spellModifier = migrateBonus(bonus);
    await item.update({
      system: {
        spellGroups,
        "-=spellCategory": null,
        spellModifier,
        "-=bonus": null,
      },
    });
  }
}

/**
 * Migrate a spell category to spell groups.
 * @param {string | undefined} spellCategory                                                            The spell category
 * @returns {import("../documents/item/spell/spell-data-source").DS4SpellDataSourceData["spellGroups"]} The spell groups for the given category
 */
function migrateSpellCategory(spellCategory) {
  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;
}

/**
 * Migrate a spell bonus to a spell modifier.
 * @param {string | undefined} bonus                                                                        The spell bonus
 * @returns {import("../documents/item/spell/spell-data-source").DS4SpellDataSourceData["spellModifier"]}   The spell modifier
 */
function migrateBonus(bonus) {
  const spellModifier = { numerical: 0, complex: "" };
  if (bonus) {
    if (Number.isNumeric(bonus)) {
      spellModifier.numerical = +bonus;
    } else {
      spellModifier.complex = bonus;
    }
  }
  return spellModifier;
}

const migrateActor = getActorMigrator(migrateItem);
const migrateScene = getSceneMigrator(migrateActor);
const migrateCompendium = getCompendiumMigrator({ migrateItem, migrateActor, migrateScene });

/** @type {import("./migration.js").Migration} */
export const migration = {
  migrate,
  migrateCompendium,
};
