import 'package:flutter/material.dart';
import '../l10n/app_localizations.dart';
import '../theme/app_colors.dart';
import '../theme/app_text_styles.dart';
import '../services/encouragement_service.dart';
import 'focus_screen.dart';
import 'history_screen.dart';
import 'settings_screen.dart';

/// Home Screen - Loads default duration from settings
class HomeScreen extends StatefulWidget {
  final EncouragementService encouragementService;

  const HomeScreen({
    super.key,
    required this.encouragementService,
  });

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _defaultDuration = 25;

  @override
  void initState() {
    super.initState();
    _loadDefaultDuration();
  }

  Future<void> _loadDefaultDuration() async {
    final duration = await SettingsScreen.getDefaultDuration();
    setState(() {
      _defaultDuration = duration;
    });
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;

    return Scaffold(
      backgroundColor: AppColors.background,
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // App Title
              Text(
                l10n.appTitle,
                style: AppTextStyles.appTitle,
              ),

              const SizedBox(height: 60),

              // Duration Display
              Container(
                padding: const EdgeInsets.symmetric(
                  horizontal: 32,
                  vertical: 16,
                ),
                decoration: BoxDecoration(
                  color: AppColors.white,
                  borderRadius: BorderRadius.circular(16),
                ),
                child: Text(
                  l10n.minutesValue(_defaultDuration, l10n.minutes(_defaultDuration)),
                  style: const TextStyle(
                    fontFamily: 'Nunito',
                    fontSize: 28,
                    fontWeight: FontWeight.w600,
                    color: AppColors.textPrimary,
                  ),
                ),
              ),

              const SizedBox(height: 60),

              // Start Focusing Button
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: () async {
                    final result = await Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => FocusScreen(
                          durationMinutes: _defaultDuration,
                          encouragementService: widget.encouragementService,
                        ),
                      ),
                    );
                    // Reload duration when returning
                    if (result == true || mounted) {
                      _loadDefaultDuration();
                    }
                  },
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(l10n.startFocusing),
                      const SizedBox(width: 8),
                      Icon(
                        Icons.play_arrow,
                        color: AppColors.white,
                      ),
                    ],
                  ),
                ),
              ),

              const SizedBox(height: 24),

              // Helper Text
              Text(
                l10n.tapDistractionAnytime,
                style: AppTextStyles.helperText,
                textAlign: TextAlign.center,
              ),

              const Spacer(),

              // Bottom Navigation (simplified for MVP)
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  TextButton.icon(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const HistoryScreen(),
                        ),
                      );
                    },
                    icon: const Icon(Icons.bar_chart),
                    label: Text(l10n.history),
                  ),
                  TextButton.icon(
                    onPressed: () async {
                      await Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const SettingsScreen(),
                        ),
                      );
                      // Reload duration after settings
                      _loadDefaultDuration();
                    },
                    icon: const Icon(Icons.settings),
                    label: Text(l10n.settings),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
