import type { PartyMember } from '../game'

export type PveCombatState = {
  party: PartyMember[]
  resource: number
  enemyHealth: number
  cooldowns: Record<string, number>
  elapsedTicks: number
  castsTowardFree: number
  freeCastReady: boolean
}

export function createPveCombatState<TEncounter extends { maxHealth: number }>({
  partyTemplate,
  maxResource,
  encounter,
  enemyCount,
}: {
  partyTemplate: readonly PartyMember[]
  maxResource: number
  encounter: TEncounter
  enemyCount: number
}): PveCombatState {
  return {
    party: partyTemplate.map((member) => ({ ...member })),
    resource: maxResource,
    enemyHealth: encounter.maxHealth * enemyCount,
    cooldowns: {},
    elapsedTicks: 0,
    castsTowardFree: 0,
    freeCastReady: false,
  }
}

export function createPveRoguelikeRunStart<TEncounter extends { maxHealth: number }>({
  partyTemplate,
  maxResource,
  encounters,
  initialEncounterIndex,
  enemyCount,
}: {
  partyTemplate: readonly PartyMember[]
  maxResource: number
  encounters: readonly TEncounter[]
  initialEncounterIndex: number
  enemyCount: number
}) {
  return {
    combatState: createPveCombatState({
      partyTemplate,
      maxResource,
      encounter: encounters[initialEncounterIndex],
      enemyCount,
    }),
    defaults: {
      roguelikeStage: 1,
      selectedId: partyTemplate[0].id,
      encounterIndex: initialEncounterIndex,
      status: 'playing' as const,
      paused: false,
      targetGroup: 0 as const,
      reward: null,
      rewardError: '',
      lootRolls: [],
      showEndLog: false,
      floatingTexts: [],
      roguelikeUpgrades: [],
      upgradeChoices: [],
      marathonBossesDefeated: 0,
      resourceSpent: 0,
      log: { text: 'A new run begins.', tone: 'system' as const },
    },
  }
}
