import 'package:flutter/material.dart'; import 'package:sudoku_app/models/game_rank_dto.dart'; import 'package:sudoku_app/services/puzzle_service.dart'; class RankingScreen extends StatefulWidget { // πŸ”½ [μΆ”κ°€] ν™ˆ ν™”λ©΄μ—μ„œ 전달받을 초기 λ‚œμ΄λ„ 이름 final String? initialDifficultyName; const RankingScreen({ super.key, this.initialDifficultyName, // πŸ‘ˆ μƒμ„±μžμ— μΆ”κ°€ }); @override State createState() => _RankingScreenState(); } class _RankingScreenState extends State { final PuzzleService _puzzleService = PuzzleService(); late Future> _rankingFuture; // 8단계 λ‚œμ΄λ„μ— λ§žλŠ” Context ID λ§΅ final Map difficultyContexts = { "μž…λ¬Έ (4x4)": "SUDOKU_4x4_L1", "μ΄ˆκΈ‰ (4x4)": "SUDOKU_4x4_L2", "쉬움 (9x9)": "SUDOKU_9x9_L3", "쀑급 (9x9)": "SUDOKU_9x9_L4", "어렀움 (9x9)": "SUDOKU_9x9_L5", "μ „λ¬Έκ°€ (16x16)": "SUDOKU_16x16_L6", "λ§ˆμŠ€ν„° (16x16)": "SUDOKU_16x16_L7", "μ§€μ˜₯ (16x16)": "SUDOKU_16x16_L8", }; late String _selectedDifficulty; @override void initState() { super.initState(); // πŸ”½ [μˆ˜μ •] // 1. HomeScreenμ—μ„œ 전달받은 값이 μžˆλŠ”μ§€ 확인 String defaultDifficulty = widget.initialDifficultyName ?? "쀑급 (9x9)"; // 2. (μ•ˆμ „μž₯치) 전달받은 값이 맡에 μ—†μœΌλ©΄(예: ν–₯ν›„ λ³€κ²½) κΈ°λ³Έκ°’ μ‚¬μš© if (!difficultyContexts.containsKey(defaultDifficulty)) { defaultDifficulty = "쀑급 (9x9)"; } // 3. λž­ν‚Ή 쑰회 _fetchRanksForDifficulty(defaultDifficulty); } void _fetchRanksForDifficulty(String difficultyName) { setState(() { _selectedDifficulty = difficultyName; _rankingFuture = _puzzleService.fetchRanks('SUDOKU', difficultyContexts[_selectedDifficulty]); }); } // μ‹œκ°„(초)을 'mm:ss' ν˜•μ‹μœΌλ‘œ λ³€ν™˜ String _formatTime(int seconds) { final min = (seconds ~/ 60).toString().padLeft(2, '0'); final sec = (seconds % 60).toString().padLeft(2, '0'); return '$min:$sec'; } // (5 - score)둜 μ €μž₯된 값을 -> "SCORE: 5"둜 λ³€ν™˜ String _formatScore(int? storedScore) { int score = 5 - (storedScore ?? 5); return 'SCORE: $score'; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('μŠ€λ„μΏ  λž­ν‚Ή')), body: Column( children: [ // 1. λ‚œμ΄λ„ 선택 Dropdown Padding( padding: const EdgeInsets.all(16.0), child: DropdownButton( value: _selectedDifficulty, // πŸ‘ˆ initStateμ—μ„œ μ„€μ •λœ κ°’μœΌλ‘œ μ‹œμž‘ isExpanded: true, items: difficultyContexts.keys.map((String difficultyName) { return DropdownMenuItem( value: difficultyName, child: Text(difficultyName), ); }).toList(), onChanged: (String? newValue) { if (newValue != null) { _fetchRanksForDifficulty(newValue); } }, ), ), // 2. λž­ν‚Ή 리슀트 Expanded( child: FutureBuilder>( future: _rankingFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Center(child: CircularProgressIndicator()); } if (snapshot.hasError) { return Center(child: Text('λž­ν‚Ή λ‘œλ”© μ‹€νŒ¨: ${snapshot.error}')); } if (!snapshot.hasData || snapshot.data!.isEmpty) { return const Center(child: Text('λ“±λ‘λœ λž­ν‚Ήμ΄ μ—†μŠ΅λ‹ˆλ‹€.')); } final ranks = snapshot.data!; return ListView.builder( itemCount: ranks.length, itemBuilder: (context, index) { final rank = ranks[index]; return ListTile( leading: Text( '${index + 1}.', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), title: Text(rank.playerName, style: const TextStyle(fontSize: 18)), trailing: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.end, children: [ Text( _formatTime(rank.primaryScore), style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.blue), ), Text( _formatScore(rank.secondaryScore), style: const TextStyle(fontSize: 12, color: Colors.grey), ), ], ), ); }, ); }, ), ), ], ), ); } }