import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../models/data_models.dart';
import '../../providers/app_provider.dart';
import 'book_review_form_page.dart';

/// 书评详情页
class BookReviewDetailPage extends StatefulWidget {
  final BookReview review;
  final String bookId;

  const BookReviewDetailPage({
    super.key,
    required this.review,
    required this.bookId,
  });

  @override
  State<BookReviewDetailPage> createState() => _BookReviewDetailPageState();
}

class _BookReviewDetailPageState extends State<BookReviewDetailPage> {
  late BookReview _review;

  @override
  void initState() {
    super.initState();
    _review = widget.review;
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _refreshReviewData();
  }

  Future<void> _refreshReviewData() async {
    final provider = context.read<AppProvider>();
    final reviews = await provider.getBookReviews(widget.bookId);
    final updatedReview = reviews
        .where((r) => r.id == widget.review.id)
        .firstOrNull;
    if (updatedReview != null && updatedReview.id == _review.id) {
      setState(() {
        _review = updatedReview;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final colors = Theme.of(context).colorScheme;
    return Scaffold(
      backgroundColor: colors.surface,
      appBar: AppBar(
        title: const Text('书评详情'),
        actions: [
          // 编辑按钮
          IconButton(
            icon: const Icon(Icons.edit_outlined),
            onPressed: () => _navigateToEdit(context),
          ),
          const SizedBox(width: 8),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(24),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 书评内容
            Text(
              _review.content,
              style: TextStyle(
                fontSize: 16,
                color: colors.onSurface,
                height: 1.8,
              ),
            ),

            const SizedBox(height: 32),

            // 分隔线
            Container(
              height: 0.5,
              color: colors.outline,
            ),

            const SizedBox(height: 24),

            // 书评人
            _buildInfoRow(
              icon: Icons.person_outline,
              label: '书评人：',
              value: _review.reviewer.isNotEmpty ? _review.reviewer : '匿名',
              colors: colors,
            ),

            const SizedBox(height: 16),

            // 来源
            if (_review.source.isNotEmpty)
              _buildInfoRow(
                icon: Icons.source_outlined,
                label: '来源：',
                value: _review.source,
                colors: colors,
              ),

            if (_review.source.isNotEmpty) const SizedBox(height: 16),

            // 类型
            _buildInfoRow(
              icon: Icons.category_outlined,
              label: '类型：',
              value: _review.typeText,
              colors: colors,
            ),

            const SizedBox(height: 16),

            // 时间
            _buildInfoRow(
              icon: Icons.access_time,
              label: '时间：',
              value: _formatDate(_review.createdAt),
              colors: colors,
            ),
          ],
        ),
      ),
    );
  }

  /// 构建信息行
  Widget _buildInfoRow({
    required IconData icon,
    required String label,
    required String value,
    required ColorScheme colors,
  }) {
    return Row(
      children: [
        Icon(
          icon,
          size: 20,
          color: colors.onSurface.withValues(alpha: 0.4),
        ),
        const SizedBox(width: 12),
        Text(
          label,
          style: TextStyle(
            fontSize: 14,
            color: colors.onSurface.withValues(alpha: 0.4),
          ),
        ),
        const SizedBox(width: 16),
        Expanded(
          child: Text(
            value,
            style: TextStyle(
              fontSize: 15,
              color: colors.onSurface,
            ),
          ),
        ),
      ],
    );
  }

  String _formatDate(DateTime date) {
    return '${date.year}.${date.month.toString().padLeft(2, '0')}.${date.day.toString().padLeft(2, '0')}';
  }

  void _navigateToEdit(BuildContext context) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => BookReviewFormPage(
          bookId: widget.bookId,
          review: _review,
        ),
      ),
    ).then((_) => _refreshReviewData());
  }
}
