import 'package:asciineverdie/l10n/app_localizations.dart';
import 'package:asciineverdie/src/core/model/game_state.dart';
import 'package:asciineverdie/src/features/game/game_play_screen.dart';
import 'package:asciineverdie/src/features/game/game_session_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '../helpers/mock_factories.dart';

/// 테스트용 MaterialApp 래퍼 (localization 포함)
Widget _buildTestApp(Widget child) {
  return MaterialApp(
    localizationsDelegates: L10n.localizationsDelegates,
    supportedLocales: L10n.supportedLocales,
    home: child,
  );
}

GameState _createTestState() {
  return GameState.withSeed(
    seed: 42,
    traits: const Traits(
      name: 'TestHero',
      race: 'Elf',
      klass: 'Mage',
      level: 5,
      motto: 'Test Motto',
      guild: '',
    ),
    stats: const Stats(
      str: 10,
      con: 12,
      dex: 14,
      intelligence: 16,
      wis: 11,
      cha: 9,
      hpMax: 50,
      mpMax: 40,
    ),
    progress: const ProgressState(
      task: ProgressBarState(position: 500, max: 1000),
      quest: ProgressBarState(position: 300, max: 600),
      plot: ProgressBarState(position: 1800, max: 3600),
      exp: ProgressBarState(position: 500, max: 1500),
      encumbrance: ProgressBarState(position: 5, max: 20),
      currentTask: TaskInfo(caption: 'Battling a Goblin', type: TaskType.kill),
      plotStageCount: 2,
      questCount: 3,
    ),
  );
}

GameSessionController _createController() {
  return GameSessionController(
    progressService: MockFactories.createProgressService(),
    saveManager: FakeSaveManager(),
    tickInterval: const Duration(seconds: 10), // 느린 틱
  );
}

void main() {
  testWidgets('GamePlayScreen renders 3-panel layout', (tester) async {
    final controller = _createController();
    addTearDown(() async {
      await controller.pause(saveOnStop: false);
      controller.dispose();
    });

    await controller.startNew(_createTestState(), isNewGame: false);

    await tester.pumpWidget(
      _buildTestApp(
        GamePlayScreen(controller: controller, forceDesktopLayout: true),
      ),
    );

    // AppBar 타이틀 확인 (L10n 사용) - ASCII NEVER DIE
    expect(find.textContaining('ASCII NEVER DIE'), findsOneWidget);

    // 3패널 헤더 확인
    expect(find.text('Character Sheet'), findsOneWidget);
    expect(find.text('Equipment'), findsOneWidget);
    expect(find.text('Plot Development'), findsOneWidget);
    expect(find.text('Quests'), findsOneWidget);

    // 테스트 완료 후 정리
    await controller.pause(saveOnStop: false);
  });

  testWidgets('GamePlayScreen shows character traits', (tester) async {
    final controller = _createController();
    addTearDown(() async {
      await controller.pause(saveOnStop: false);
      controller.dispose();
    });

    await controller.startNew(_createTestState(), isNewGame: false);

    await tester.pumpWidget(
      _buildTestApp(
        GamePlayScreen(controller: controller, forceDesktopLayout: true),
      ),
    );

    // Traits 섹션 확인
    expect(find.text('Traits'), findsOneWidget);
    expect(find.text('TestHero'), findsOneWidget);
    expect(find.text('Elf'), findsOneWidget);
    expect(find.text('Mage'), findsOneWidget);

    await controller.pause(saveOnStop: false);
  });

  testWidgets('GamePlayScreen shows stats', (tester) async {
    final controller = _createController();
    addTearDown(() async {
      await controller.pause(saveOnStop: false);
      controller.dispose();
    });

    await controller.startNew(_createTestState(), isNewGame: false);

    await tester.pumpWidget(
      _buildTestApp(
        GamePlayScreen(controller: controller, forceDesktopLayout: true),
      ),
    );

    // Stats 섹션 확인 (스크롤로 인해 화면 밖에 있을 수 있음)
    expect(find.text('Stats'), findsOneWidget);
    expect(find.text('STR', skipOffstage: false), findsOneWidget);
    expect(find.text('CON', skipOffstage: false), findsOneWidget);

    await controller.pause(saveOnStop: false);
  });

  testWidgets('GamePlayScreen shows current task caption', (tester) async {
    final controller = _createController();
    addTearDown(() async {
      await controller.pause(saveOnStop: false);
      controller.dispose();
    });

    await controller.startNew(_createTestState(), isNewGame: false);

    await tester.pumpWidget(
      _buildTestApp(
        GamePlayScreen(controller: controller, forceDesktopLayout: true),
      ),
    );

    // 현재 태스크 캡션 확인 (퀘스트 목록과 하단 패널에 표시됨)
    expect(find.text('Battling a Goblin'), findsAtLeast(1));

    await controller.pause(saveOnStop: false);
  });

  testWidgets('GamePlayScreen shows progress bars', (tester) async {
    final controller = _createController();
    addTearDown(() async {
      await controller.pause(saveOnStop: false);
      controller.dispose();
    });

    await controller.startNew(_createTestState(), isNewGame: false);

    await tester.pumpWidget(
      _buildTestApp(
        GamePlayScreen(controller: controller, forceDesktopLayout: true),
      ),
    );

    // LinearProgressIndicator가 여러 개 표시되는지 확인
    expect(find.byType(LinearProgressIndicator), findsAtLeast(4));

    await controller.pause(saveOnStop: false);
  });

  testWidgets('Loading state shows CircularProgressIndicator', (tester) async {
    final controller = _createController();
    addTearDown(() {
      controller.dispose();
    });

    // 상태 없이 시작 (startNew 호출 안 함)
    await tester.pumpWidget(
      _buildTestApp(
        GamePlayScreen(controller: controller, forceDesktopLayout: true),
      ),
    );

    // 로딩 인디케이터 확인
    expect(find.byType(CircularProgressIndicator), findsOneWidget);
  });
}
