import 'package:flutter/material.dart';
import 'package:forui/forui.dart';

import '../../../core/contracts/library.dart';
import '../../../shared/widgets/auto_dismiss_menu.dart';
import '../widgets/stream_selector_actions.dart';

const _dropdownMenuMaxHeight = 320.0;


class AndroidStreamSelectorRow extends StatelessWidget {

  final List<EmbyRawMediaSource> sources;
  final int selectedSourceIdx;
  final ValueChanged<int>? onSelectSourceIndex;

  final List<EmbyRawMediaStream> subtitles;
  final List<EmbyRawMediaStream> audios;
  final int? selectedSubtitleIdx;
  final int selectedAudioIdx;
  final ValueChanged<int?> onSelectSubtitleIndex;
  final ValueChanged<int> onSelectAudioIndex;

  const AndroidStreamSelectorRow({
    super.key,
    this.sources = const [],
    this.selectedSourceIdx = 0,
    this.onSelectSourceIndex,
    required this.subtitles,
    required this.audios,
    required this.selectedSubtitleIdx,
    required this.selectedAudioIdx,
    required this.onSelectSubtitleIndex,
    required this.onSelectAudioIndex,
  });

  @override
  Widget build(BuildContext context) {
    final buttons = <Widget>[];

    final onSelectSource = onSelectSourceIndex;
    if (sources.length > 1 && onSelectSource != null) {
      buttons.add(
        _SelectorButton(
          icon: Icons.video_file_outlined,
          label: versionSummaryLabel(sources, selectedSourceIdx),
          menuChildrenBuilder: () => buildVersionMenuButtons(
            sources: sources,
            selectedSourceIdx: selectedSourceIdx,
            onSelectSource: onSelectSource,
          ),
        ),
      );
    }

    if (subtitles.length > 1) {
      buttons.add(
        _SelectorButton(
          icon: Icons.subtitles_outlined,
          label: subtitleSummaryLabel(subtitles, selectedSubtitleIdx),
          menuChildrenBuilder: () => buildSubtitleMenuButtons(
            subtitles: subtitles,
            selectedSubtitleIdx: selectedSubtitleIdx,
            onSelectSubtitle: onSelectSubtitleIndex,
          ),
        ),
      );
    }

    if (audios.length > 1) {
      buttons.add(
        _SelectorButton(
          icon: Icons.audiotrack_outlined,
          label: audioSummaryLabel(audios, selectedAudioIdx),
          menuChildrenBuilder: () => buildAudioMenuButtons(
            audios: audios,
            selectedAudioIdx: selectedAudioIdx,
            onSelectAudio: onSelectAudioIndex,
          ),
        ),
      );
    }

    if (buttons.isEmpty) return const SizedBox.shrink();

    return SizedBox(
      height: 36,
      child: ListView.separated(
        scrollDirection: Axis.horizontal,
        padding: const EdgeInsets.symmetric(horizontal: 16),
        itemCount: buttons.length,
        separatorBuilder: (_, _) => const SizedBox(width: 8),
        itemBuilder: (_, index) => buttons[index],
      ),
    );
  }
}

class _SelectorButton extends StatelessWidget {
  final IconData icon;
  final String label;
  final List<FItem> Function() menuChildrenBuilder;

  const _SelectorButton({
    required this.icon,
    required this.label,
    required this.menuChildrenBuilder,
  });

  @override
  Widget build(BuildContext context) {
    return FPopoverMenu(
      menuBuilder: autoDismissMenuBuilder,
      menuAnchor: Alignment.topCenter,
      childAnchor: Alignment.bottomCenter,
      maxHeight: _dropdownMenuMaxHeight,
      menu: [FItemGroup(children: menuChildrenBuilder())],
      builder: (context, controller, child) => GestureDetector(
        onTap: controller.toggle,
        child: child,
      ),
      child: Material(
        color: Colors.white.withValues(alpha: 0.08),
        borderRadius: BorderRadius.circular(10),
        clipBehavior: Clip.antiAlias,
        child: DecoratedBox(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            border: Border.all(color: Colors.white.withValues(alpha: 0.15)),
          ),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 10),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(
                  icon,
                  size: 16,
                  color: Colors.white.withValues(alpha: 0.8),
                ),
                const SizedBox(width: 6),
                ConstrainedBox(
                  constraints: const BoxConstraints(maxWidth: 140),
                  child: Text(
                    label,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 12,
                      color: Colors.white.withValues(alpha: 0.85),
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
                const SizedBox(width: 2),
                Icon(
                  Icons.arrow_drop_down,
                  size: 18,
                  color: Colors.white.withValues(alpha: 0.6),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
