export function highestRoguelikeRoundAfterDefeat(currentRecord: number, reachedRound: number): number {
  const normalizedRecord = Math.max(0, Math.floor(Number(currentRecord) || 0));
  const normalizedRound = Math.max(1, Math.floor(Number(reachedRound) || 1));
  return Math.max(normalizedRecord, normalizedRound);
}

export function highestEndlessBossKillsAfterDefeat(currentRecord: number, bossKills: number): number {
  const normalizedRecord = Math.max(0, Math.floor(Number(currentRecord) || 0));
  const normalizedKills = Math.max(0, Math.floor(Number(bossKills) || 0));
  return Math.max(normalizedRecord, normalizedKills);
}

export interface HockeyHealingRecord {
  returns: number;
  durationSeconds: number;
}

export function bestHockeyHealingRecord(
  currentReturns: number,
  currentDurationSeconds: number,
  runReturns: number,
  runDurationSeconds: number,
): HockeyHealingRecord {
  const current: HockeyHealingRecord = {
    returns: Math.max(0, Math.floor(Number(currentReturns) || 0)),
    durationSeconds: Math.max(0, Number(currentDurationSeconds) || 0),
  };
  const run: HockeyHealingRecord = {
    returns: Math.max(0, Math.floor(Number(runReturns) || 0)),
    durationSeconds: Math.max(0, Number(runDurationSeconds) || 0),
  };
  return run.returns > current.returns || run.returns === current.returns && run.durationSeconds > current.durationSeconds
    ? run
    : current;
}

export function hockeyPvpRecordAfterMatch(currentWins: number, currentLosses: number, won: boolean) {
  const wins = Math.max(0, Math.floor(Number(currentWins) || 0));
  const losses = Math.max(0, Math.floor(Number(currentLosses) || 0));
  return won ? { wins: wins + 1, losses } : { wins, losses: losses + 1 };
}

export interface BlockbreakerRecords {
  bricks: number;
  durationSeconds: number;
  score: number;
}

export function bestBlockbreakerRecords(
  currentBricks: number,
  currentDurationSeconds: number,
  currentScore: number,
  runBricks: number,
  runDurationSeconds: number,
  runScore: number,
): BlockbreakerRecords {
  return {
    bricks: Math.max(
      Math.max(0, Math.floor(Number(currentBricks) || 0)),
      Math.max(0, Math.floor(Number(runBricks) || 0)),
    ),
    durationSeconds: Math.max(
      Math.max(0, Number(currentDurationSeconds) || 0),
      Math.max(0, Number(runDurationSeconds) || 0),
    ),
    score: Math.max(
      Math.max(0, Math.floor(Number(currentScore) || 0)),
      Math.max(0, Math.floor(Number(runScore) || 0)),
    ),
  };
}

export interface AetherAssaultRecord {
  score: number;
  wave: number;
  durationSeconds: number;
}

export function bestAetherAssaultRecord(
  currentScore: number,
  currentWave: number,
  currentDurationSeconds: number,
  runScore: number,
  runWave: number,
  runDurationSeconds: number,
): AetherAssaultRecord {
  const current = {
    score: Math.max(0, Math.floor(Number(currentScore) || 0)),
    wave: Math.max(0, Math.floor(Number(currentWave) || 0)),
    durationSeconds: Math.max(0, Number(currentDurationSeconds) || 0),
  };
  const run = {
    score: Math.max(0, Math.floor(Number(runScore) || 0)),
    wave: Math.max(0, Math.floor(Number(runWave) || 0)),
    durationSeconds: Math.max(0, Number(runDurationSeconds) || 0),
  };
  return run.score > current.score
    || run.score === current.score && run.wave > current.wave
    || run.score === current.score && run.wave === current.wave && run.durationSeconds > current.durationSeconds
    ? run
    : current;
}
