import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../theme/theme_extension.dart';
import 'player_provider.dart';

class PlayerBar extends ConsumerWidget {
  const PlayerBar({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final fs = Theme.of(context).extension<FabledSwordTheme>()!;
    final mediaItem = ref.watch(mediaItemProvider).valueOrNull;
    final playback = ref.watch(playbackStateProvider).valueOrNull;
    if (mediaItem == null) return const SizedBox.shrink();

    return Material(
      color: fs.iron,
      child: InkWell(
        onTap: () => context.push('/now-playing'),
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          child: Row(children: [
            Container(width: 48, height: 48, color: fs.slate),
            const SizedBox(width: 12),
            Expanded(child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(mediaItem.title, maxLines: 1, overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: fs.parchment, fontSize: 14)),
                Text(mediaItem.artist ?? '', maxLines: 1, overflow: TextOverflow.ellipsis,
                    style: TextStyle(color: fs.ash, fontSize: 12)),
              ],
            )),
            IconButton(
              icon: Icon(playback?.playing == true ? Icons.pause : Icons.play_arrow, color: fs.parchment),
              onPressed: () {
                final h = ref.read(audioHandlerProvider);
                if (playback?.playing == true) { h.pause(); } else { h.play(); }
              },
            ),
            IconButton(
              icon: Icon(Icons.skip_next, color: fs.parchment),
              onPressed: () => ref.read(audioHandlerProvider).skipToNext(),
            ),
          ]),
        ),
      ),
    );
  }
}
