import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/battle_provider.dart';
import '../providers/settings_provider.dart'; // Import SettingsProvider
import 'main_menu_screen.dart';
import '../game/config/theme_config.dart';
import '../game/config/app_strings.dart';

class SettingsScreen extends StatelessWidget {
  const SettingsScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Text(
            AppStrings.settings,
            style: TextStyle(
              fontSize: ThemeConfig.fontSizeTitle,
              fontWeight: ThemeConfig.fontWeightBold,
              color: ThemeConfig.textColorWhite,
            ),
          ),
          const SizedBox(height: 40),

          // Enemy Animation Toggle
          Consumer<SettingsProvider>(
            builder: (context, settings, child) {
              return SizedBox(
                width: 300,
                child: SwitchListTile(
                  title: const Text(
                    AppStrings.enemyAnimations,
                    style: TextStyle(color: ThemeConfig.textColorWhite),
                  ),
                  value: settings.enableEnemyAnimations,
                  onChanged: (value) {
                    settings.toggleEnemyAnimations(value);
                  },
                  activeColor: ThemeConfig.btnActionActive,
                ),
              );
            },
          ),

          const SizedBox(height: 20),
          const Text(
            'Volume: 100%',
            style: TextStyle(color: ThemeConfig.textColorWhite),
          ),
          const SizedBox(height: 40),

          // Restart Button
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: ThemeConfig.btnRestartBg,
              padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 12),
            ),
            onPressed: () {
              _showConfirmationDialog(
                context,
                title: '${AppStrings.restart} Game?',
                content: 'All progress will be lost. Are you sure?',
                onConfirm: () {
                  context.read<BattleProvider>().initializeBattle();
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('Game Restarted!')),
                  );
                  // Optionally switch tab back to Battle (index 0)
                  // But MainWrapper controls the index.
                  // We can't easily switch tab from here without a callback or Provider.
                  // For now, just restart logic is enough.
                },
              );
            },
            child: const Text('Restart Game'),
          ),
          const SizedBox(height: 20),

          // Return to Main Menu Button
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: ThemeConfig.btnReturnMenuBg,
              padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 12),
            ),
            onPressed: () {
              _showConfirmationDialog(
                context,
                title: '${AppStrings.returnToMenu}?',
                content:
                    'Unsaved progress may be lost. (Progress is saved automatically after each stage)',
                onConfirm: () {
                  Navigator.of(context).pushAndRemoveUntil(
                    MaterialPageRoute(
                      builder: (context) => const MainMenuScreen(),
                    ),
                    (route) => false,
                  );
                },
              );
            },
            child: const Text(AppStrings.returnToMenu),
          ),
        ],
      ),
    );
  }

  void _showConfirmationDialog(
    BuildContext context, {
    required String title,
    required String content,
    required VoidCallback onConfirm,
  }) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(title),
        content: Text(content),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text(AppStrings.cancel),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              onConfirm();
            },
            child: const Text(
              AppStrings.confirm,
              style: TextStyle(color: Colors.red),
            ),
          ),
        ],
      ),
    );
  }
}
