import 'package:flutter/material.dart'; import 'package:sudoku_app/models/sudoku_game_dto.dart'; import 'package:sudoku_app/models/sudoku_theme.dart'; import 'package:sudoku_app/screens/game_screen.dart'; import 'package:sudoku_app/screens/ranking_screen.dart'; import 'package:sudoku_app/services/puzzle_service.dart'; import 'package:sudoku_app/services/identity_service.dart'; // πŸ‘ˆ ID μ„œλΉ„μŠ€ μž„ν¬νŠΈ import 'package:sudoku_app/widgets/ad_banner_widget.dart'; class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State { // 8단계 λ‚œμ΄λ„ double _difficultyLevel = 4.0; // 1.0 ~ 8.0 (κΈ°λ³Έκ°’ Level 4: 쀑급 9x9) final List levelLabels = [ "μž…λ¬Έ (4x4)", "μ΄ˆκΈ‰ (4x4)", "쉬움 (9x9)", "쀑급 (9x9)", "어렀움 (9x9)", "μ „λ¬Έκ°€ (16x16)", "λ§ˆμŠ€ν„° (16x16)", "μ§€μ˜₯ (16x16)" ]; late String _selectedThemeName; bool isLoading = false; final PuzzleService _puzzleService = PuzzleService(); final IdentityService _identityService = IdentityService(); // πŸ‘ˆ ID μ„œλΉ„μŠ€ μ΄ˆκΈ°ν™” @override void initState() { super.initState(); _selectedThemeName = AppThemes.random; // κΈ°λ³Έ ν…Œλ§ˆ '랜덀' } Future _startGame() async { setState(() { isLoading = true; }); try { // 1. λ‚œμ΄λ„ κ°’(String) 전달 final String difficulty = _difficultyLevel.round().toString(); // 2. μ„œλ²„μ—μ„œ κ²Œμž„ 데이터 κ°€μ Έμ˜€κΈ° final SudokuGameDto gameData = await _puzzleService.startGame(difficulty); // 3. λ‘œμ»¬μ—μ„œ μ•±-고유 ID와 μ €μž₯된 이름 κ°€μ Έμ˜€κΈ° final String userId = await _identityService.getOrCreateUserId(); final String? userName = await _identityService.getSavedUserName(); if (mounted) { Navigator.push( context, MaterialPageRoute( builder: (context) => GameScreen( gameData: gameData, themeName: _selectedThemeName, userId: userId, // πŸ‘ˆ ID 전달 userName: userName, // πŸ‘ˆ 이름 전달 ), ), ); } } catch (e) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('κ²Œμž„ λ‘œλ”© μ‹€νŒ¨: $e')), ); } } finally { if (mounted) { setState(() { isLoading = false; }); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('μŠ€λ„μΏ  κ²Œμž„')), body: LayoutBuilder( // λΉ„μœ¨ 기반 λ ˆμ΄μ•„μ›ƒ builder: (context, constraints) { // λ„ˆλΉ„/높이 λΉ„μœ¨ λ³€μˆ˜ (0.6 = λ„ˆλΉ„κ°€ λ†’μ΄μ˜ 60%λ₯Ό λ„˜μ§€ μ•Šλ„λ‘ 함) const double maxContentRatio = 0.6; final double constrainedWidth = constraints.maxHeight * maxContentRatio; return Center( child: ConstrainedBox( constraints: BoxConstraints(maxWidth: constrainedWidth), child: Column( children: [ Expanded( child: Center( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // 1. λ‚œμ΄λ„ 선택 (8단계) const Text("λ‚œμ΄λ„", style: TextStyle(fontSize: 18)), Text( levelLabels[_difficultyLevel.round() - 1], style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold, color: Colors.blue), ), Slider( value: _difficultyLevel, min: 1.0, max: 8.0, divisions: 7, // 8단계 label: levelLabels[_difficultyLevel.round() - 1], onChanged: (newValue) => setState(() { _difficultyLevel = newValue; }), ), const SizedBox(height: 20), // 2. ν…Œλ§ˆ 선택 (String 기반) const Text("ν…Œλ§ˆ", style: TextStyle(fontSize: 18)), DropdownButton( value: _selectedThemeName, items: AppThemes.selectableThemeNames.map((themeName) { return DropdownMenuItem( value: themeName, child: Text(themeName, style: const TextStyle(fontSize: 20)), ); }).toList(), onChanged: (themeName) { if (themeName != null) { setState(() { _selectedThemeName = themeName; }); } }, ), const SizedBox(height: 30), if (isLoading) const CircularProgressIndicator() else ElevatedButton( onPressed: _startGame, style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 15)), child: const Text('κ²Œμž„ μ‹œμž‘', style: TextStyle(fontSize: 18)), ), const SizedBox(height: 10), // "λž­ν‚Ή 보기" λ²„νŠΌ TextButton( onPressed: () { // ν˜„μž¬ μŠ¬λΌμ΄λ”μ˜ λ‚œμ΄λ„ 이름(String)을 κ°€μ Έμ˜΄ final String currentDifficultyName = levelLabels[_difficultyLevel.round() - 1]; Navigator.push( context, MaterialPageRoute( builder: (context) => RankingScreen( initialDifficultyName: currentDifficultyName, ), ), ); }, child: const Text('λž­ν‚Ή 보기'), ), ], ), ), ), ), const AdBannerWidget(), ], ), ), ); }, ), ); } }