import 'dart:io' show Platform;

import 'package:flutter/material.dart';
import '../../../providers/player_engine_override_provider.dart';
import '../../../shared/widgets/setting_select.dart';


class PlayerEngineRow extends StatelessWidget {
  final PlayerEngineOverride value;
  final ValueChanged<PlayerEngineOverride> onChanged;


  final bool? isAndroid;

  const PlayerEngineRow({
    super.key,
    required this.value,
    required this.onChanged,
    this.isAndroid,
  });


  static List<PlayerEngineOverride> visibleOptions({required bool isAndroid}) {
    return PlayerEngineOverride.values
        .where((v) => v != PlayerEngineOverride.media3 || isAndroid)
        .toList();
  }


  static PlayerEngineOverride coerceValue(
    PlayerEngineOverride value, {
    required bool isAndroid,
  }) {
    if (value == PlayerEngineOverride.media3 && !isAndroid) {
      return PlayerEngineOverride.auto;
    }
    return value;
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final onAndroid = isAndroid ?? Platform.isAndroid;

    final options = visibleOptions(isAndroid: onAndroid);
    final coercedValue = coerceValue(value, isAndroid: onAndroid);

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
      child: Row(
        children: [
          Expanded(
            child: Text(
              '播放引擎',
              style: theme.textTheme.bodyMedium?.copyWith(
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          SettingSelect<PlayerEngineOverride>(
            value: coercedValue,
            options: options,
            labelOf: (v) => v.label,
            onChanged: (v) {
              if (v != null) onChanged(v);
            },
          ),
        ],
      ),
    );
  }
}
