import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:share_plus/share_plus.dart';

import 'models/stream_session.dart';

String? watchShareUrl(StreamSession session) {
  return session.shareUrl ?? session.youtubeWatchUrl ?? session.watchPageUrl;
}

String watchShareLabel(StreamSession session) {
  if (session.platform == 'youtube') {
    return 'Guarda la diretta su YouTube';
  }
  return 'Guarda la diretta su Match Live TV';
}

Future<void> shareWatchLink(
  BuildContext context, {
  required StreamSession session,
  required String title,
}) async {
  final url = watchShareUrl(session);
  if (url == null || url.isEmpty) {
    if (context.mounted) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Link diretta non ancora disponibile')),
      );
    }
    return;
  }
  await Share.share(
    '${watchShareLabel(session)}:\n$url',
    subject: title,
  );
}

Future<void> copyWatchLink(BuildContext context, StreamSession session) async {
  final url = watchShareUrl(session);
  if (url == null || url.isEmpty) {
    if (context.mounted) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Link diretta non ancora disponibile')),
      );
    }
    return;
  }
  await Clipboard.setData(ClipboardData(text: url));
  if (context.mounted) {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('Link copiato')),
    );
  }
}
