import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/auth_provider.dart';
import '../providers/evaluation_provider.dart';
import '../widgets/course_card.dart';
import 'progress_screen.dart';
import 'settings_screen.dart';

/// Home screen displaying course list and evaluation controls
///
/// Shows list of courses that need evaluation
/// Provides batch evaluation functionality
class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

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

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
    // Defer loading until after the first frame
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _loadCourses();
    });
  }

  Future<void> _loadCourses() async {
    if (!mounted) return;

    final authProvider = Provider.of<AuthProvider>(context, listen: false);
    final evaluationProvider = Provider.of<EvaluationProvider>(
      context,
      listen: false,
    );

    print('🔍 _loadCourses called');
    print('🔍 authProvider.connection: ${authProvider.connection}');
    print('🔍 authProvider.isAuthenticated: ${authProvider.isAuthenticated}');

    // Set connection for evaluation provider
    if (authProvider.connection != null) {
      print('🔍 Setting connection and loading courses...');
      evaluationProvider.setConnection(authProvider.connection);
      final success = await evaluationProvider.loadCourses();
      print('🔍 loadCourses result: $success');
      print('🔍 courses count: ${evaluationProvider.courses.length}');
      if (!success) {
        print('❌ Error: ${evaluationProvider.errorMessage}');
      }
    } else {
      print('❌ No connection available');
    }
  }

  Future<void> _handleRefresh() async {
    await _loadCourses();
  }

  Future<void> _handleBatchEvaluation() async {
    final evaluationProvider = Provider.of<EvaluationProvider>(
      context,
      listen: false,
    );

    // Check if there are pending courses
    if (evaluationProvider.pendingCourses.isEmpty) {
      if (!mounted) return;
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('没有待评课程')));
      return;
    }

    // Navigate to progress screen
    if (!mounted) return;
    Navigator.of(
      context,
    ).push(MaterialPageRoute(builder: (context) => const ProgressScreen()));

    // Start concurrent batch evaluation
    await evaluationProvider.startConcurrentBatchEvaluation();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('课程评教'),
        actions: [
          IconButton(
            icon: const Icon(Icons.refresh),
            onPressed: _handleRefresh,
            tooltip: '刷新',
          ),
          IconButton(
            icon: const Icon(Icons.settings),
            onPressed: () {
              Navigator.of(context).push(
                MaterialPageRoute(builder: (context) => const SettingsScreen()),
              );
            },
            tooltip: '设置',
          ),
        ],
      ),
      body: Consumer<EvaluationProvider>(
        builder: (context, evaluationProvider, child) {
          if (evaluationProvider.state == EvaluationState.loading) {
            return const Center(child: CircularProgressIndicator());
          }

          if (evaluationProvider.state == EvaluationState.error) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.error_outline,
                    size: 64,
                    color: Theme.of(context).colorScheme.error,
                  ),
                  const SizedBox(height: 16),
                  Text(
                    evaluationProvider.errorMessage ?? '加载失败',
                    style: Theme.of(context).textTheme.bodyLarge,
                    textAlign: TextAlign.center,
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton.icon(
                    onPressed: _handleRefresh,
                    icon: const Icon(Icons.refresh),
                    label: const Text('重试'),
                  ),
                ],
              ),
            );
          }

          final courses = evaluationProvider.courses;
          final pendingCount = evaluationProvider.pendingCourses.length;
          final evaluatedCount = evaluationProvider.evaluatedCourses.length;

          if (courses.isEmpty) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.check_circle_outline,
                    size: 64,
                    color: Theme.of(context).colorScheme.primary,
                  ),
                  const SizedBox(height: 16),
                  Text(
                    '暂无待评课程',
                    style: Theme.of(context).textTheme.headlineSmall,
                  ),
                  const SizedBox(height: 8),
                  Text(
                    '下拉刷新以检查新课程',
                    style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                      color: Theme.of(context).colorScheme.onSurfaceVariant,
                    ),
                  ),
                ],
              ),
            );
          }

          return Column(
            children: [
              // Statistics card
              Container(
                margin: const EdgeInsets.all(16),
                padding: const EdgeInsets.all(16),
                decoration: BoxDecoration(
                  color: Theme.of(context).colorScheme.primaryContainer,
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    _buildStatItem(
                      context,
                      '总计',
                      courses.length.toString(),
                      Icons.list_alt,
                    ),
                    _buildStatItem(
                      context,
                      '待评',
                      pendingCount.toString(),
                      Icons.pending_actions,
                    ),
                    _buildStatItem(
                      context,
                      '已评',
                      evaluatedCount.toString(),
                      Icons.check_circle,
                    ),
                  ],
                ),
              ),

              // Course list
              Expanded(
                child: RefreshIndicator(
                  onRefresh: _handleRefresh,
                  child: ListView.builder(
                    padding: const EdgeInsets.symmetric(horizontal: 16),
                    itemCount: courses.length,
                    itemBuilder: (context, index) {
                      final course = courses[index];
                      return CourseCard(course: course);
                    },
                  ),
                ),
              ),
            ],
          );
        },
      ),
      floatingActionButton: Consumer<EvaluationProvider>(
        builder: (context, evaluationProvider, child) {
          final hasPending = evaluationProvider.pendingCourses.isNotEmpty;
          final isEvaluating =
              evaluationProvider.state == EvaluationState.evaluating;

          if (!hasPending || isEvaluating) {
            return const SizedBox.shrink();
          }

          return FloatingActionButton.extended(
            onPressed: _handleBatchEvaluation,
            icon: const Icon(Icons.play_arrow),
            label: const Text('批量评教'),
          );
        },
      ),
    );
  }

  Widget _buildStatItem(
    BuildContext context,
    String label,
    String value,
    IconData icon,
  ) {
    return Column(
      children: [
        Icon(icon, color: Theme.of(context).colorScheme.onPrimaryContainer),
        const SizedBox(height: 4),
        Text(
          value,
          style: Theme.of(context).textTheme.headlineSmall?.copyWith(
            color: Theme.of(context).colorScheme.onPrimaryContainer,
            fontWeight: FontWeight.bold,
          ),
        ),
        Text(
          label,
          style: Theme.of(context).textTheme.bodySmall?.copyWith(
            color: Theme.of(context).colorScheme.onPrimaryContainer,
          ),
        ),
      ],
    );
  }
}
