import 'package:flutter/material.dart';

import '../../../shared/widgets/app_dialog.dart';
import 'android_detail_colors.dart';

class AndroidOverviewSection extends StatelessWidget {
  final String overview;

  const AndroidOverviewSection({super.key, required this.overview});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(16, 8, 16, 12),
      child: LayoutBuilder(
        builder: (context, constraints) {
          final textStyle = TextStyle(
            fontSize: 14,
            color: Colors.white.withValues(alpha: 0.6),
            height: 1.65,
          );
          final textSpan = TextSpan(text: overview, style: textStyle);
          final tp = TextPainter(
            text: textSpan,
            maxLines: 1,
            textDirection: TextDirection.ltr,
          )..layout(maxWidth: constraints.maxWidth);
          final hasOverflow = tp.didExceedMaxLines;

          return GestureDetector(
            onTap: hasOverflow ? () => _showOverviewDialog(context) : null,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  overview,
                  style: textStyle,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
                if (hasOverflow) ...[
                  const SizedBox(height: 6),
                  const Text(
                    '阅读全部',
                    style: TextStyle(
                      fontSize: 13,
                      color: AndroidDetailColors.accent,
                    ),
                  ),
                ],
              ],
            ),
          );
        },
      ),
    );
  }

  void _showOverviewDialog(BuildContext context) {
    showAppRawDialog<void>(
      context,
      builder: (context) => ColoredBox(
        color: const Color(0xFF1E1E2E),
        child: Padding(
          padding: const EdgeInsets.fromLTRB(24, 24, 24, 16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text(
                '简介',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w600,
                  color: Colors.white,
                ),
              ),
              const SizedBox(height: 16),
              Flexible(
                child: SingleChildScrollView(
                  child: Text(
                    overview,
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.white.withValues(alpha: 0.75),
                      height: 1.7,
                    ),
                  ),
                ),
              ),
              const SizedBox(height: 12),
              Align(
                alignment: Alignment.centerRight,
                child: TextButton(
                  onPressed: () => Navigator.of(context).pop(),
                  child: const Text('关闭'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
