import 'package:flutter_test/flutter_test.dart';
import 'package:pokeguess/core/config/app_constants.dart';
import 'package:pokeguess/domain/entities/pokemon.dart';
import 'package:pokeguess/domain/game/game_engine.dart';
import 'package:pokeguess/domain/game/game_state.dart';

const _engine = GameEngine();

Pokemon _poke(String name) => Pokemon(
      name: name, id: 1, type1: PokemonType.normal,
      hp: 1, atk: 1, def: 1, spd: 1,
    );

GameState _playing(String name, {bool shiny = false}) => _engine.startRound(
      const GameState(), _poke(name), isShiny: shiny);

void main() {
  test('newGame réinitialise vies/score/session, garde le bestScore', () {
    const s = GameState(
        lives: 1, currentScore: 50, sessionCorrectCount: 7, bestScore: 99);
    final r = _engine.newGame(s);
    expect(r.lives, 3);
    expect(r.currentScore, 0);
    expect(r.sessionCorrectCount, 0);
    expect(r.bestScore, 99);
    expect(r.status, GameStatus.loading);
  });

  test('startRound place le Pokémon et passe en playing', () {
    final r = _playing('pikachu', shiny: true);
    expect(r.currentPokemon?.name, 'pikachu');
    expect(r.isShiny, true);
    expect(r.isHintUsed, false);
    expect(r.status, GameStatus.playing);
  });

  test('bonne réponse: +10 points et roundWon', () {
    final o = _engine.submitGuess(_playing('pikachu'), 'pikachu');
    expect(o.result, GuessResult.correct);
    expect(o.state.currentScore, 10);
    expect(o.state.sessionCorrectCount, 1);
    expect(o.state.status, GameStatus.roundWon);
    expect(o.state.currentPokemon?.isCaught, true);
  });

  test('bonne réponse shiny: +20 points', () {
    final o = _engine.submitGuess(_playing('pikachu', shiny: true), 'pikachu');
    expect(o.state.currentScore, 20);
  });

  test('comparaison insensible aux accents et à la casse', () {
    final o = _engine.submitGuess(_playing('Dracaufeu'), 'dracaufeu');
    expect(o.result, GuessResult.correct);
    final o2 = _engine.submitGuess(_playing('Électhor'), 'electhor');
    expect(o2.result, GuessResult.correct);
  });

  test('mauvaise réponse: -1 vie, reste playing tant qu\'il reste des vies', () {
    final o = _engine.submitGuess(_playing('pikachu'), 'salameche');
    expect(o.result, GuessResult.wrong);
    expect(o.state.lives, 2);
    expect(o.state.status, GameStatus.playing);
  });

  test('mauvaise réponse à 1 vie: gameOver', () {
    const start = GameState(lives: 1, status: GameStatus.playing);
    final round = _engine.startRound(start, _poke('pikachu'), isShiny: false);
    final o = _engine.submitGuess(round, 'faux');
    expect(o.result, GuessResult.gameOver);
    expect(o.state.lives, 0);
    expect(o.state.status, GameStatus.gameOver);
  });

  test('le cheat "pikachu" n\'existe plus: faux nom sur un autre Pokémon = wrong', () {
    final o = _engine.submitGuess(_playing('bulbizarre'), 'pikachu');
    expect(o.result, GuessResult.wrong);
  });

  test('bonus: indice tous les 5, skip tous les 10', () {
    var s = const GameState(sessionCorrectCount: 4, status: GameStatus.playing);
    s = _engine.startRound(s, _poke('pikachu'), isShiny: false);
    final o = _engine.submitGuess(s, 'pikachu'); // 5e bonne réponse
    expect(o.state.sessionCorrectCount, 5);
    expect(o.state.hints, appConstantsHints + 1);
  });

  test('useHint consomme un indice', () {
    final s = _playing('pikachu');
    final r = _engine.useHint(s);
    expect(r.isHintUsed, true);
    expect(r.hints, 2);
  });

  test('useHint sans indice restant ne change rien', () {
    final s = _playing('pikachu').copyWith(hints: 0);
    final r = _engine.useHint(s);
    expect(r.isHintUsed, false);
    expect(r.hints, 0);
  });

  test('useSkip décrémente les skips', () {
    final s = _playing('pikachu');
    final r = _engine.useSkip(s);
    expect(r.skips, 2);
  });

  test('bonus: skip tous les 10 bonnes réponses', () {
    var s = const GameState(sessionCorrectCount: 9, status: GameStatus.playing);
    s = _engine.startRound(s, _poke('pikachu'), isShiny: false);
    final o = _engine.submitGuess(s, 'pikachu'); // 10e bonne réponse
    expect(o.state.sessionCorrectCount, 10);
    expect(o.state.skips, AppConstants.startingSkips + 1);
  });

  test('submitGuess renvoie invalid quand la manche n\'est pas en cours', () {
    const s = GameState(status: GameStatus.loading); // aucun pokemon, pas en playing
    final o = _engine.submitGuess(s, 'pikachu');
    expect(o.result, GuessResult.invalid);
    expect(identical(o.state, s), true);
  });
}

/// Valeur attendue de hints au démarrage (miroir d'AppConstants.startingHints = 3).
const appConstantsHints = 3;
