import 'package:audio_service/audio_service.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:finamp/l10n/app_localizations.dart';

import '../../services/music_player_background_task.dart';

class PlayerButtons extends StatelessWidget {
  const PlayerButtons({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final audioHandler = GetIt.instance<MusicPlayerBackgroundTask>();

    return StreamBuilder<PlaybackState>(
      stream: audioHandler.playbackState,
      builder: (context, snapshot) {
        final PlaybackState? playbackState = snapshot.data;
        final colorScheme = Theme.of(context).colorScheme;

        return Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          textDirection: TextDirection.ltr,
          children: [
            IconButton(
              tooltip: playbackState?.shuffleMode == AudioServiceShuffleMode.all
                  ? AppLocalizations.of(context)!.playbackOrderShuffledTooltip
                  : AppLocalizations.of(context)!.playbackOrderLinearTooltip,
              icon: _getShufflingIcon(
                playbackState == null
                    ? AudioServiceShuffleMode.none
                    : playbackState.shuffleMode,
                colorScheme.primary,
              ),
              onPressed: playbackState != null
                  ? () async {
                      if (playbackState.shuffleMode ==
                          AudioServiceShuffleMode.all) {
                        await audioHandler.setShuffleMode(
                          AudioServiceShuffleMode.none,
                        );
                      } else {
                        await audioHandler.setShuffleMode(
                          AudioServiceShuffleMode.all,
                        );
                      }
                    }
                  : null,
              iconSize: 22,
            ),
            IconButton(
              tooltip: AppLocalizations.of(context)!.skipToPrevious,
              icon: const Icon(Icons.skip_previous_rounded),
              onPressed: playbackState != null
                  ? () async => await audioHandler.skipToPrevious()
                  : null,
              iconSize: 38,
              style: IconButton.styleFrom(
                backgroundColor: colorScheme.surfaceVariant.withOpacity(0.44),
              ),
            ),
            SizedBox.square(
              dimension: 68,
              child: IconButton.filled(
                tooltip: AppLocalizations.of(context)!.togglePlayback,
                onPressed: playbackState != null
                    ? () async {
                        if (playbackState.playing) {
                          await audioHandler.pause();
                        } else {
                          await audioHandler.play();
                        }
                      }
                    : null,
                icon: Icon(
                  playbackState == null || playbackState.playing
                      ? Icons.pause
                      : Icons.play_arrow,
                  size: 42,
                ),
              ),
            ),
            IconButton(
              tooltip: AppLocalizations.of(context)!.skipToNext,
              icon: const Icon(Icons.skip_next_rounded),
              onPressed: playbackState != null
                  ? () async => audioHandler.skipToNext()
                  : null,
              iconSize: 38,
              style: IconButton.styleFrom(
                backgroundColor: colorScheme.surfaceVariant.withOpacity(0.44),
              ),
            ),
            IconButton(
              tooltip: playbackState?.repeatMode == AudioServiceRepeatMode.all
                  ? AppLocalizations.of(context)!.loopModeAllTooltip
                  : playbackState?.repeatMode == AudioServiceRepeatMode.one
                      ? AppLocalizations.of(context)!.loopModeOneTooltip
                      : AppLocalizations.of(context)!.loopModeNoneTooltip,
              icon: _getRepeatingIcon(
                playbackState == null
                    ? AudioServiceRepeatMode.none
                    : playbackState.repeatMode,
                colorScheme.primary,
              ),
              onPressed: playbackState != null
                  ? () async {
                      // Cyles from none -> all -> one
                      if (playbackState.repeatMode ==
                          AudioServiceRepeatMode.none) {
                        await audioHandler.setRepeatMode(
                          AudioServiceRepeatMode.all,
                        );
                      } else if (playbackState.repeatMode ==
                          AudioServiceRepeatMode.all) {
                        await audioHandler.setRepeatMode(
                          AudioServiceRepeatMode.one,
                        );
                      } else {
                        await audioHandler.setRepeatMode(
                          AudioServiceRepeatMode.none,
                        );
                      }
                    }
                  : null,
              iconSize: 22,
            ),
          ],
        );
      },
    );
  }

  Widget _getRepeatingIcon(
    AudioServiceRepeatMode repeatMode,
    Color iconColour,
  ) {
    if (repeatMode == AudioServiceRepeatMode.all) {
      return Icon(Icons.repeat, color: iconColour);
    } else if (repeatMode == AudioServiceRepeatMode.one) {
      return Icon(Icons.repeat_one, color: iconColour);
    } else {
      return const Icon(Icons.repeat);
    }
  }

  Icon _getShufflingIcon(
    AudioServiceShuffleMode shuffleMode,
    Color iconColour,
  ) {
    if (shuffleMode == AudioServiceShuffleMode.all) {
      return Icon(Icons.shuffle, color: iconColour);
    } else {
      return const Icon(Icons.shuffle);
    }
  }
}
