import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:sendtrain/database/database.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';

class MediaContent extends StatelessWidget {
  const MediaContent({super.key, required this.media});

  final MediaItem media;

  @override
  Widget build(BuildContext context) {
    YoutubePlayerController controller = YoutubePlayerController(
        initialVideoId: media.reference,
        flags: const YoutubePlayerFlags(
            autoPlay: false, mute: true, showLiveFullscreenButton: false));

    if (media.type == MediaType.image || media.type == MediaType.location) {
      return Image(image: NetworkImage(media.reference));
    } else if (media.type == MediaType.localImage) {
        return Image.memory(base64Decode(media.reference));
    } else if (media.type == MediaType.youtube) {
      return YoutubePlayer(
        controller: controller,
        aspectRatio: 16 / 9,
      );
    }

    return const Image(image: AssetImage('assets/images/placeholder.jpg'));
  }
}
