import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:smplayer/features/player/widgets/android/android_bottom_transport_controls.dart';

import '../../../../helpers/fake_player_engine.dart';

void main() {
  testWidgets('renders prev play next order', (tester) async {
    final player = FakePlayerEngine(playing: true);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          backgroundColor: Colors.black,
          body: Center(
            child: AndroidBottomTransportControls(
              player: player,
              hasPrev: true,
              hasNext: true,
              onPrevItem: () {},
              onNextItem: () {},
            ),
          ),
        ),
      ),
    );

    expect(find.byIcon(Icons.skip_previous), findsOneWidget);
    expect(find.byIcon(Icons.pause), findsOneWidget);
    expect(find.byIcon(Icons.skip_next), findsOneWidget);

    final prevCenter = tester.getCenter(find.byIcon(Icons.skip_previous));
    final playCenter = tester.getCenter(find.byIcon(Icons.pause));
    final nextCenter = tester.getCenter(find.byIcon(Icons.skip_next));

    expect(prevCenter.dx < playCenter.dx, isTrue);
    expect(playCenter.dx < nextCenter.dx, isTrue);

    await player.dispose();
  });

  testWidgets('invokes prev/next callbacks and toggles playback', (
    tester,
  ) async {
    final player = FakePlayerEngine(playing: true);
    var prevTaps = 0;
    var nextTaps = 0;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: AndroidBottomTransportControls(
              player: player,
              hasPrev: true,
              hasNext: true,
              onPrevItem: () => prevTaps++,
              onNextItem: () => nextTaps++,
            ),
          ),
        ),
      ),
    );

    await tester.tap(find.byIcon(Icons.skip_previous));
    await tester.tap(find.byIcon(Icons.skip_next));
    await tester.tap(find.byIcon(Icons.pause));
    await tester.pump();

    expect(prevTaps, 1);
    expect(nextTaps, 1);
    expect(player.pauseCalls, 1);
    expect(player.playCalls, 0);

    await player.dispose();
  });

  testWidgets('disables prev/next when unavailable', (tester) async {
    final player = FakePlayerEngine(playing: true);
    var prevTaps = 0;
    var nextTaps = 0;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: AndroidBottomTransportControls(
              player: player,
              hasPrev: false,
              hasNext: false,
              onPrevItem: () => prevTaps++,
              onNextItem: () => nextTaps++,
            ),
          ),
        ),
      ),
    );

    final prevBtn = tester.widget<IconButton>(
      find.widgetWithIcon(IconButton, Icons.skip_previous),
    );
    final nextBtn = tester.widget<IconButton>(
      find.widgetWithIcon(IconButton, Icons.skip_next),
    );

    expect(prevBtn.onPressed, isNull);
    expect(nextBtn.onPressed, isNull);

    await player.dispose();
  });
}
