import 'package:flutter/material.dart';

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

class AndroidHeroInfo extends StatelessWidget {
  final String? posterUrl;
  final String embyBaseUrl;
  final Map<String, String>? imageHeaders;
  final String title;
  final double? rating;
  final List<String> metaParts;
  final List<String> genres;
  final List<String> mediaInfoTags;

  const AndroidHeroInfo({
    super.key,
    this.posterUrl,
    this.embyBaseUrl = '',
    this.imageHeaders,
    required this.title,
    this.rating,
    this.metaParts = const [],
    this.genres = const [],
    this.mediaInfoTags = const [],
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          DecoratedBox(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10),
              boxShadow: const [
                BoxShadow(
                  color: Color(0x80000000),
                  blurRadius: 20,
                  offset: Offset(0, 10),
                ),
              ],
            ),
            child: SizedBox(
              width: 120,
              height: 180,
              child: SmartImage(
                url: posterUrl,
                fit: BoxFit.cover,
                borderRadius: 10,
                placeholder: Container(
                  decoration: BoxDecoration(
                    color: const Color(0xFF2A2A4A),
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
                resolveHttpHeaders: (url) =>
                    embyBaseUrl.isNotEmpty && url.startsWith(embyBaseUrl)
                    ? imageHeaders
                    : null,
              ),
            ),
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  title,
                  style: const TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.w700,
                    color: Colors.white,
                    height: 1.2,
                    shadows: [
                      Shadow(
                        color: Color(0xCC000000),
                        blurRadius: 12,
                        offset: Offset(0, 2),
                      ),
                    ],
                  ),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
                if (metaParts.isNotEmpty)
                  Padding(
                    padding: const EdgeInsets.only(top: 6),
                    child: Text(
                      metaParts.join(' · '),
                      style: TextStyle(
                        fontSize: 13,
                        color: Colors.white.withValues(alpha: 0.55),
                        shadows: const [
                          Shadow(
                            color: Color(0xCC000000),
                            blurRadius: 12,
                            offset: Offset(0, 2),
                          ),
                        ],
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                if (rating != null && rating! > 0)
                  Padding(
                    padding: const EdgeInsets.only(top: 8),
                    child: _StarRating(rating: rating!),
                  ),
                if (genres.isNotEmpty)
                  Padding(
                    padding: const EdgeInsets.only(top: 8),
                    child: Wrap(
                      spacing: 6,
                      runSpacing: 6,
                      children: genres
                          .map((g) => _GenrePill(label: g))
                          .toList(),
                    ),
                  ),
                if (mediaInfoTags.isNotEmpty)
                  Padding(
                    padding: const EdgeInsets.only(top: 8),
                    child: Wrap(
                      spacing: 6,
                      runSpacing: 6,
                      children: mediaInfoTags
                          .map((t) => _MediaInfoPill(label: t))
                          .toList(),
                    ),
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class _StarRating extends StatelessWidget {
  final double rating;
  const _StarRating({required this.rating});

  @override
  Widget build(BuildContext context) {
    final stars = rating / 2.0;
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        for (int i = 0; i < 5; i++)
          Icon(
            Icons.star_rounded,
            size: 16,
            color: i < stars.round()
                ? AndroidDetailColors.accent
                : AndroidDetailColors.starEmpty,
          ),
        const SizedBox(width: 6),
        Text(
          rating.toStringAsFixed(1),
          style: TextStyle(
            fontSize: 13,
            fontWeight: FontWeight.w600,
            color: Colors.white.withValues(alpha: 0.8),
            shadows: const [
              Shadow(
                color: Color(0xCC000000),
                blurRadius: 12,
                offset: Offset(0, 2),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

class _GenrePill extends StatelessWidget {
  final String label;
  const _GenrePill({required this.label});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
      decoration: BoxDecoration(
        color: AndroidDetailColors.genrePillBg,
        borderRadius: BorderRadius.circular(14),
        border: Border.all(color: AndroidDetailColors.genrePillBorder),
      ),
      child: Text(
        label,
        style: TextStyle(
          fontSize: 12,
          color: Colors.white.withValues(alpha: 0.65),
        ),
      ),
    );
  }
}

class _MediaInfoPill extends StatelessWidget {
  final String label;
  const _MediaInfoPill({required this.label});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
      decoration: BoxDecoration(
        color: AndroidDetailColors.mediaTagBg,
        borderRadius: BorderRadius.circular(6),
        border: Border.all(color: AndroidDetailColors.mediaTagBorder),
      ),
      child: Text(
        label,
        style: TextStyle(
          fontSize: 11,
          fontWeight: FontWeight.w500,
          color: Colors.white.withValues(alpha: 0.7),
        ),
      ),
    );
  }
}
