

part of 'player_page.dart';

extension _PlayerPageTopBar on _PlayerPageState {
  String _buildTitleText(PlaybackSession session) {
    final idx = session.context.itemIndexNumber;
    final name = session.context.itemName;
    final series = session.context.seriesName;
    if (series != null && series.isNotEmpty) {
      if (idx != null) return '$series  ${formatEpisodeNumber(idx)}  $name';
      return '$series  $name';
    }
    if (idx != null) return '${formatEpisodeNumber(idx)}  $name';
    return name;
  }

  MediaQualityInfo? _activeQualityInfo(PlaybackSession session) {
    final srcId = session.context.mediaSourceId;
    for (final src in session.context.allMediaSources) {
      if (src.Id == srcId) return MediaQualityInfo.fromMediaSource(src);
    }
    if (session.context.allMediaSources.isNotEmpty) {
      return MediaQualityInfo.fromMediaSource(
        session.context.allMediaSources.first,
      );
    }
    return null;
  }

  List<Widget> _buildTopBar(PlaybackSession session) {
    if (_pip.isPip) {
      return [
        IconButton(
          onPressed: _goBack,
          icon: const Icon(Icons.arrow_back, color: Colors.white),
          tooltip: '退出播放',
        ),
        const Expanded(child: SizedBox.shrink()),
        IconButton(
          onPressed: () => unawaited(_pip.toggleAlwaysOnTop()),
          icon: Icon(
            _pip.isAlwaysOnTop ? Icons.push_pin : Icons.push_pin_outlined,
            color: _pip.isAlwaysOnTop ? Colors.white : Colors.white54,
            size: 20,
          ),
          tooltip: _pip.isAlwaysOnTop ? '取消置顶' : '置顶',
        ),
        IconButton(
          onPressed: () => unawaited(windowManager.close()),
          icon: const Icon(Icons.close, color: Colors.white, size: 20),
          tooltip: '关闭',
        ),
      ];
    }
    return [
      IconButton(
        onPressed: _goBack,
        icon: const Icon(Icons.arrow_back, color: Colors.white),
      ),
      const SizedBox(width: 4),
      Expanded(
        child: SizedBox(
          height: kMinInteractiveDimension,
          child: Align(
            alignment: Alignment.centerLeft,
            child: Text(
              _buildTitleText(session),
              style: const TextStyle(color: Colors.white, fontSize: 15),
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ),
      ),
      const SizedBox(width: 8),
      Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              QualityBadge(quality: _activeQualityInfo(session)),
              const SizedBox(width: 8),
              NetworkSpeedIndicator(monitor: session.speedMonitor),
              if (!_fullscreen.isFullscreen &&
                  !_pip.isPip &&
                  _isDesktopWindow) ...[
                const SizedBox(width: 8),
                const WindowControls.forDarkSurface(),
              ],
            ],
          ),
          const SizedBox(height: 6),
          const PlayerClockText(),
        ],
      ),
    ];
  }
}
