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

import 'package:minstrel/shared/widgets/track_actions/hide_track_sheet.dart';
import 'package:minstrel/theme/theme_data.dart';

void main() {
  Future<({String reason, String notes})?> openAndPick(
    WidgetTester t, {
    required String chipKey,
    String notes = '',
  }) async {
    Future<({String reason, String notes})?>? future;
    await t.pumpWidget(MaterialApp(
      theme: buildThemeData(),
      home: Builder(builder: (ctx) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                future = HideTrackSheet.show(ctx);
              },
              child: const Text('open'),
            ),
          ),
        );
      }),
    ));
    await t.tap(find.text('open'));
    await t.pumpAndSettle();
    if (chipKey != 'hide_reason_bad_rip') {
      await t.tap(find.byKey(Key(chipKey)));
      await t.pumpAndSettle();
    }
    if (notes.isNotEmpty) {
      await t.enterText(find.byKey(const Key('hide_notes_input')), notes);
    }
    await t.tap(find.byKey(const Key('hide_confirm')));
    await t.pumpAndSettle();
    return future;
  }

  testWidgets('default reason is bad_rip', (t) async {
    final result = await openAndPick(t, chipKey: 'hide_reason_bad_rip');
    expect(result, isNotNull);
    expect(result!.reason, 'bad_rip');
    expect(result.notes, '');
  });

  testWidgets('selecting wrong_tags returns wrong_tags', (t) async {
    final result = await openAndPick(t, chipKey: 'hide_reason_wrong_tags');
    expect(result?.reason, 'wrong_tags');
  });

  testWidgets('notes get trimmed and returned', (t) async {
    final result = await openAndPick(
      t,
      chipKey: 'hide_reason_other',
      notes: '  cracks at 2:13  ',
    );
    expect(result?.reason, 'other');
    expect(result?.notes, 'cracks at 2:13');
  });

  testWidgets('cancel returns null', (t) async {
    Future<({String reason, String notes})?>? future;
    await t.pumpWidget(MaterialApp(
      theme: buildThemeData(),
      home: Builder(builder: (ctx) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () { future = HideTrackSheet.show(ctx); },
              child: const Text('open'),
            ),
          ),
        );
      }),
    ));
    await t.tap(find.text('open'));
    await t.pumpAndSettle();
    await t.tap(find.text('Cancel'));
    await t.pumpAndSettle();
    expect(await future, isNull);
  });
}
