import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

import '../widgets/section_container.dart';

class ExternalLinksSection extends StatelessWidget {
  final String? imdbId;
  final int? tmdbId;
  final String? tmdbMediaType;
  final int? traktId;
  final bool embedded;


  final Widget? leading;

  const ExternalLinksSection({
    super.key,
    this.imdbId,
    this.tmdbId,
    this.tmdbMediaType,
    this.traktId,
    this.embedded = true,
    this.leading,
  });

  bool get _hasAnyLink => imdbId != null || tmdbId != null || traktId != null;

  @override
  Widget build(BuildContext context) {
    if (!_hasAnyLink) return const SizedBox.shrink();
    return SectionContainer(
      title: '外部链接',
      embedded: embedded,
      leading: leading,
      child: Wrap(
        spacing: 8,
        runSpacing: 8,
        children: [
          if (imdbId != null)
            _LinkPill(
              label: 'IMDb',
              dotColor: const Color(0xFFF5C518),
              onTap: () =>
                  launchUrl(Uri.parse('https://www.imdb.com/title/$imdbId')),
            ),
          if (tmdbId != null)
            _LinkPill(
              label: 'TMDB',
              dotColor: const Color(0xFF01B4E4),
              onTap: () => launchUrl(
                Uri.parse(
                  'https://www.themoviedb.org/${tmdbMediaType ?? 'movie'}/$tmdbId',
                ),
              ),
            ),
          if (traktId != null)
            _LinkPill(
              label: 'Trakt',
              dotColor: const Color(0xFFED1C24),
              onTap: () => launchUrl(
                Uri.parse(
                  'https://trakt.tv/${tmdbMediaType == 'tv' ? 'shows' : 'movies'}/$traktId',
                ),
              ),
            ),
        ],
      ),
    );
  }
}

class _LinkPill extends StatelessWidget {
  final String label;
  final Color dotColor;
  final VoidCallback onTap;

  const _LinkPill({
    required this.label,
    required this.dotColor,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.white.withValues(alpha: 0.06),
      borderRadius: BorderRadius.circular(8),
      child: InkWell(
        borderRadius: BorderRadius.circular(8),
        onTap: onTap,
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            border: Border.all(color: Colors.white.withValues(alpha: 0.06)),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                width: 6,
                height: 6,
                decoration: BoxDecoration(
                  color: dotColor,
                  shape: BoxShape.circle,
                ),
              ),
              const SizedBox(width: 8),
              Text(
                label,
                style: TextStyle(
                  fontSize: 13,
                  color: Colors.white.withValues(alpha: 0.65),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
