import 'package:flutter/material.dart';

import '../../../core/contracts/playback.dart';
import '../../../core/infra/player_engine/player_engine.dart';
import 'panel_toggle_affordance.dart';

class SubtitleTracksButton extends StatelessWidget {
  final PlayerEngine player;
  final List<EmbySubtitleTrack> embySubtitles;
  final bool isPanelOpen;
  final VoidCallback onTogglePanel;

  const SubtitleTracksButton({
    super.key,
    required this.player,
    required this.embySubtitles,
    required this.isPanelOpen,
    required this.onTogglePanel,
  });

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<PlayerTracks>(
      key: ObjectKey(player),
      stream: player.stream.tracks,
      initialData: player.state.tracks,
      builder: (_, snapshot) {
        final hasSubtitles =
            (snapshot.data?.embeddedSubtitles.isNotEmpty ?? false) ||
            embySubtitles.isNotEmpty;
        if (!hasSubtitles) return const SizedBox.shrink();
        return PanelToggleAffordance(
          isOpen: isPanelOpen,
          onTap: onTogglePanel,
          tooltip: '字幕',
          child: const Icon(
            Icons.subtitles_outlined,
            color: Color(0xFFFFFFFF),
            size: 28,
          ),
        );
      },
    );
  }
}

class AudioTracksButton extends StatelessWidget {
  final PlayerEngine player;
  final bool isPanelOpen;
  final VoidCallback onTogglePanel;

  const AudioTracksButton({
    super.key,
    required this.player,
    required this.isPanelOpen,
    required this.onTogglePanel,
  });

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<PlayerTracks>(
      key: ObjectKey(player),
      stream: player.stream.tracks,
      initialData: player.state.tracks,
      builder: (_, snapshot) {
        if ((snapshot.data?.embeddedAudio.length ?? 0) <= 1) {
          return const SizedBox.shrink();
        }
        return PanelToggleAffordance(
          isOpen: isPanelOpen,
          onTap: onTogglePanel,
          tooltip: '音轨',
          child: const Icon(
            Icons.multitrack_audio,
            color: Color(0xFFFFFFFF),
            size: 28,
          ),
        );
      },
    );
  }
}
