import 'package:flutter/material.dart';

import '../../core/contracts/library.dart';
import '../mappers/media_image_url.dart';
import '../mappers/tmdb_image_selector.dart';
import 'smart_image.dart';


class EpisodeThumb extends StatelessWidget {
  final EmbyRawItem episode;
  final String seriesId;
  final String baseUrl;
  final String token;
  final String? tmdbStillUrl;
  final double? aspectRatio;
  final double borderRadius;
  final int maxWidth;
  final IconData? fallbackIcon;
  final BoxFit fit;

  const EpisodeThumb({
    super.key,
    required this.episode,
    required this.seriesId,
    required this.baseUrl,
    required this.token,
    this.tmdbStillUrl,
    this.aspectRatio,
    this.borderRadius = 0,
    this.maxWidth = 480,
    this.fallbackIcon = Icons.movie,
    this.fit = BoxFit.cover,
  });

  @override
  Widget build(BuildContext context) {
    final embyThumbUrl = EmbyImageUrl.thumb(
      baseUrl: baseUrl,
      token: token,
      item: episode,
      maxWidth: maxWidth,
    );
    final primaryUrl = tmdbStillUrl ?? embyThumbUrl;
    final fallbackUrls = TmdbImageSelector.buildFallbackUrls(
      primaryUrl: primaryUrl,
      candidates: [
        embyThumbUrl,
        EmbyImageUrl.backdrop(
          baseUrl: baseUrl,
          token: token,
          item: episode,
          maxWidth: maxWidth,
        ),
        EmbyImageUrl.fanart(baseUrl: baseUrl, token: token, itemId: seriesId),
        EmbyImageUrl.primaryById(
          baseUrl: baseUrl,
          token: token,
          itemId: seriesId,
          maxHeight: maxWidth,
        ),
      ],
    );
    return SmartImage(
      url: primaryUrl,
      fallbackUrls: fallbackUrls,
      aspectRatio: aspectRatio,
      borderRadius: borderRadius,
      fallbackIcon: fallbackIcon,
      fit: fit,
    );
  }
}
