import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:forui/forui.dart';
import 'package:smplayer/shared/widgets/setting_select.dart';

void main() {
  Widget host({
    required String value,
    required ValueChanged<String?> onChanged,
  }) {
    return MaterialApp(
      home: FTheme(
        data: FThemes.zinc.light.desktop,
        child: Scaffold(
          body: Center(
            child: SettingSelect<String>(
              value: value,
              options: const ['a', 'b', 'c'],
              labelOf: (v) => 'label-$v',
              onChanged: onChanged,
            ),
          ),
        ),
      ),
    );
  }

  testWidgets('触发行显示选中项 label', (tester) async {
    await tester.pumpWidget(host(value: 'b', onChanged: (_) {}));
    await tester.pumpAndSettle();
    expect(find.text('label-b'), findsWidgets);
  });

  testWidgets('选另一项触发 onChanged', (tester) async {
    String? picked;
    await tester.pumpWidget(host(value: 'a', onChanged: (v) => picked = v));
    await tester.pumpAndSettle();

    await tester.tap(find.byType(SettingSelect<String>));
    await tester.pumpAndSettle();

    await tester.tap(find.text('label-c').last);
    await tester.pumpAndSettle();

    expect(picked, 'c');
  });

  testWidgets('Select 可放在 Row 的 trailing 位置', (tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: FTheme(
          data: FThemes.zinc.light.desktop,
          child: Scaffold(
            body: SizedBox(
              width: 480,
              child: Row(
                children: [
                  const Expanded(child: Text('设置项')),
                  SettingSelect<String>(
                    value: 'b',
                    options: const ['a', 'b', 'c'],
                    labelOf: (v) => 'label-$v',
                    onChanged: (_) {},
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();

    expect(find.text('label-b'), findsWidgets);
    expect(tester.takeException(), isNull);
  });
}
