import 'package:flutter/material.dart';
import 'package:aesthetica_wallpaper/models/puzzle_game.dart';
import 'package:aesthetica_wallpaper/screens/puzzle/puzzle_game_screen.dart';
import 'package:aesthetica_wallpaper/screens/puzzle/simple_puzzle_game_screen.dart';

/// 拼图游戏菜单页面
class PuzzleMenuScreen extends StatefulWidget {
  const PuzzleMenuScreen({super.key});

  @override
  State<PuzzleMenuScreen> createState() => _PuzzleMenuScreenState();
}

class _PuzzleMenuScreenState extends State<PuzzleMenuScreen> {
  GameDifficulty _selectedDifficulty = GameDifficulty.easy;
  GameMode _selectedMode = GameMode.classic;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [Colors.purple.shade400, Colors.blue.shade400],
          ),
        ),
        child: SafeArea(
          child: Column(
            children: [
              // 顶部栏
              _buildAppBar(),

              Expanded(
                child: SingleChildScrollView(
                  padding: const EdgeInsets.all(20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      // 标题
                      _buildTitle(),
                      const SizedBox(height: 40),

                      // 游戏模式选择
                      _buildModeSelection(),
                      const SizedBox(height: 30),

                      // 难度选择
                      _buildDifficultySelection(),
                      const SizedBox(height: 40),

                      // 开始游戏按钮
                      _buildStartButton(),
                      const SizedBox(height: 20),

                      // 统计信息
                      _buildStats(),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildAppBar() {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Row(
        children: [
          IconButton(
            icon: const Icon(Icons.arrow_back, color: Colors.white),
            onPressed: () => Navigator.pop(context),
          ),
          const Spacer(),
          IconButton(
            icon: const Icon(Icons.leaderboard, color: Colors.white),
            onPressed: () {
              // TODO: 显示排行榜
            },
          ),
          IconButton(
            icon: const Icon(Icons.emoji_events, color: Colors.white),
            onPressed: () {
              // TODO: 显示成就
            },
          ),
        ],
      ),
    );
  }

  Widget _buildTitle() {
    return Column(
      children: [
        const Icon(Icons.extension, size: 80, color: Colors.white),
        const SizedBox(height: 16),
        const Text(
          '拼图游戏',
          style: TextStyle(
            fontSize: 36,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        const SizedBox(height: 8),
        Text(
          '挑战你的智力，赢取独家壁纸',
          style: TextStyle(fontSize: 16, color: Colors.white.withValues(alpha: 0.9)),
        ),
      ],
    );
  }

  Widget _buildModeSelection() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white.withValues(alpha: 0.2),
        borderRadius: BorderRadius.circular(20),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '游戏模式',
            style: TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          ),
          const SizedBox(height: 16),
          _buildModeOption(
            GameMode.classic,
            '经典模式',
            '滑动拼图块到空格',
            Icons.grid_4x4,
          ),
          const SizedBox(height: 12),
          _buildModeOption(
            GameMode.casual,
            '休闲模式',
            '交换任意两个拼图块',
            Icons.swap_horiz,
          ),
          const SizedBox(height: 12),
          _buildModeOption(
            GameMode.challenge,
            '挑战模式',
            '限时完成，获得更多奖励',
            Icons.timer,
          ),
        ],
      ),
    );
  }

  Widget _buildModeOption(
    GameMode mode,
    String title,
    String description,
    IconData icon,
  ) {
    final isSelected = _selectedMode == mode;
    return InkWell(
      onTap: () {
        setState(() {
          _selectedMode = mode;
        });
      },
      borderRadius: BorderRadius.circular(12),
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: isSelected
              ? Colors.white.withValues(alpha: 0.3)
              : Colors.white.withValues(alpha: 0.1),
          borderRadius: BorderRadius.circular(12),
          border: Border.all(
            color: isSelected ? Colors.white : Colors.transparent,
            width: 2,
          ),
        ),
        child: Row(
          children: [
            Icon(icon, color: Colors.white, size: 32),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    description,
                    style: TextStyle(
                      fontSize: 12,
                      color: Colors.white.withValues(alpha: 0.8),
                    ),
                  ),
                ],
              ),
            ),
            if (isSelected) const Icon(Icons.check_circle, color: Colors.white),
          ],
        ),
      ),
    );
  }

  Widget _buildDifficultySelection() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white.withValues(alpha: 0.2),
        borderRadius: BorderRadius.circular(20),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '选择难度',
            style: TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          ),
          const SizedBox(height: 16),
          Row(
            children: GameDifficulty.values.map((difficulty) {
              return Expanded(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 4),
                  child: _buildDifficultyChip(difficulty),
                ),
              );
            }).toList(),
          ),
        ],
      ),
    );
  }

  Widget _buildDifficultyChip(GameDifficulty difficulty) {
    final isSelected = _selectedDifficulty == difficulty;
    return InkWell(
      onTap: () {
        setState(() {
          _selectedDifficulty = difficulty;
        });
      },
      borderRadius: BorderRadius.circular(12),
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 16),
        decoration: BoxDecoration(
          color: isSelected ? Colors.white : Colors.white.withValues(alpha: 0.1),
          borderRadius: BorderRadius.circular(12),
        ),
        child: Column(
          children: [
            Text(
              difficulty.description,
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: isSelected ? Colors.purple : Colors.white,
              ),
            ),
            const SizedBox(height: 4),
            Text(
              difficulty.label,
              style: TextStyle(
                fontSize: 12,
                color: isSelected
                    ? Colors.purple.withValues(alpha: 0.7)
                    : Colors.white.withValues(alpha: 0.7),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStartButton() {
    return ElevatedButton(
      onPressed: _startGame,
      style: ElevatedButton.styleFrom(
        backgroundColor: Colors.white,
        foregroundColor: Colors.purple,
        padding: const EdgeInsets.symmetric(vertical: 20),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
        elevation: 8,
      ),
      child: const Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(Icons.play_arrow, size: 32),
          SizedBox(width: 8),
          Text(
            '开始游戏',
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
        ],
      ),
    );
  }

  Widget _buildStats() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white.withValues(alpha: 0.2),
        borderRadius: BorderRadius.circular(20),
      ),
      child: Column(
        children: [
          const Text(
            '游戏统计',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: Colors.white,
            ),
          ),
          const SizedBox(height: 16),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              _buildStatItem('已完成', '0', Icons.check_circle),
              _buildStatItem('最佳时间', '--:--', Icons.timer),
              _buildStatItem('三星评价', '0', Icons.star),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildStatItem(String label, String value, IconData icon) {
    return Column(
      children: [
        Icon(icon, color: Colors.white, size: 32),
        const SizedBox(height: 8),
        Text(
          value,
          style: const TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          label,
          style: TextStyle(fontSize: 12, color: Colors.white.withValues(alpha: 0.8)),
        ),
      ],
    );
  }

  void _startGame() {
    // 选择图片或使用简化版测试
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('选择游戏类型'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ListTile(
              leading: const Icon(Icons.image),
              title: const Text('图片拼图'),
              subtitle: const Text('使用真实图片（开发中）'),
              onTap: () {
                Navigator.pop(context);
                _startImagePuzzle();
              },
            ),
            ListTile(
              leading: const Icon(Icons.palette),
              title: const Text('颜色拼图'),
              subtitle: const Text('使用颜色块测试'),
              onTap: () {
                Navigator.pop(context);
                _startColorPuzzle();
              },
            ),
          ],
        ),
      ),
    );
  }

  void _startImagePuzzle() {
    // 使用默认图片
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => PuzzleGameScreen(
          imagePath: 'assets/images/nature/nature1.png',
          difficulty: _selectedDifficulty,
          mode: _selectedMode,
        ),
      ),
    );
  }

  void _startColorPuzzle() {
    // 使用简化版颜色拼图
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => SimplePuzzleGameScreen(
          difficulty: _selectedDifficulty,
          mode: _selectedMode,
        ),
      ),
    );
  }
}
