content: /*
   *  This file is part of BlackHole (https://github.com/Sangwan5688/BlackHole).
   * 
   * BlackHole is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * BlackHole is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with BlackHole.  If not, see <http://www.gnu.org/licenses/>.
   * 
   * Copyright (c) 2021-2022, Ankit Sangwan
   */
  
  import 'package:audio_service/audio_service.dart';
  import 'package:blackhole/CustomWidgets/add_playlist.dart';
  import 'package:blackhole/Helpers/add_mediaitem_to_queue.dart';
  import 'package:blackhole/Helpers/mediaitem_converter.dart';
  import 'package:blackhole/Screens/Common/song_list.dart';
  import 'package:blackhole/Screens/Search/albums.dart';
  import 'package:blackhole/Screens/Search/search.dart';
  import 'package:blackhole/Services/youtube_services.dart';
  import 'package:flutter/cupertino.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter_gen/gen_l10n/app_localizations.dart';
  import 'package:share_plus/share_plus.dart';
  import 'package:url_launcher/url_launcher.dart';
  
  class SongTileTrailingMenu extends StatefulWidget {
    final Map data;
    final bool isPlaylist;
    final Function(Map)? deleteLiked;
    const SongTileTrailingMenu({
      super.key,
      required this.data,
      this.isPlaylist = false,
      this.deleteLiked,
    });
  
    @override
    _SongTileTrailingMenuState createState() => _SongTileTrailingMenuState();
  }
  
  class _SongTileTrailingMenuState extends State<SongTileTrailingMenu> {
    @override
    Widget build(BuildContext context) {
      final MediaItem mediaItem = MediaItemConverter.mapToMediaItem(widget.data);
      return PopupMenuButton(
        icon: Icon(
          Icons.more_vert_rounded,
          color: Theme.of(context).iconTheme.color,
        ),
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(15.0),
          ),
        ),
        itemBuilder: (context) => [
          if (widget.isPlaylist && widget.deleteLiked != null)
            PopupMenuItem(
              value: 6,
              child: Row(
                children: [
                  const Icon(
                    Icons.delete_rounded,
                  ),
                  const SizedBox(
                    width: 10.0,
                  ),
                  Text(
                    AppLocalizations.of(
                      context,
                    )!
                        .remove,
                  ),
                ],
              ),
            ),
          PopupMenuItem(
            value: 2,
            child: Row(
              children: [
                Icon(
                  Icons.playlist_play_rounded,
                  color: Theme.of(context).iconTheme.color,
                  size: 26.0,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.playNext),
              ],
            ),
          ),
          PopupMenuItem(
            value: 1,
            child: Row(
              children: [
                Icon(
                  Icons.queue_music_rounded,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.addToQueue),
              ],
            ),
          ),
          PopupMenuItem(
            value: 0,
            child: Row(
              children: [
                Icon(
                  Icons.playlist_add_rounded,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.addToPlaylist),
              ],
            ),
          ),
          PopupMenuItem(
            value: 4,
            child: Row(
              children: [
                Icon(
                  Icons.album_rounded,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.viewAlbum),
              ],
            ),
          ),
          if (mediaItem.artist != null)
            ...mediaItem.artist.toString().split(', ').map(
                  (artist) => PopupMenuItem(
                    value: artist,
                    child: SingleChildScrollView(
                      scrollDirection: Axis.horizontal,
                      child: Row(
                        children: [
                          Icon(
                            Icons.person_rounded,
                            color: Theme.of(context).iconTheme.color,
                          ),
                          const SizedBox(width: 10.0),
                          Text(
                            '${AppLocalizations.of(context)!.viewArtist} ($artist)',
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
          PopupMenuItem(
            value: 3,
            child: Row(
              children: [
                Icon(
                  Icons.share_rounded,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.share),
              ],
            ),
          ),
        ],
        onSelected: (value) {
          switch (value) {
            case 3:
              Share.share(widget.data['perma_url'].toString());
              break;
  
            case 4:
              Navigator.push(
                context,
                PageRouteBuilder(
                  opaque: false,
                  pageBuilder: (_, __, ___) => SongsListPage(
                    listItem: {
                      'type': 'album',
                      'id': mediaItem.extras?['album_id'],
                      'title': mediaItem.album,
                      'image': mediaItem.artUri,
                    },
                  ),
                ),
              );
              break;
            case 6:
              widget.deleteLiked!(widget.data);
              break;
            case 0:
              AddToPlaylist().addToPlaylist(context, mediaItem);
              break;
            case 1:
              addToNowPlaying(context: context, mediaItem: mediaItem);
              break;
            case 2:
              playNext(mediaItem, context);
              break;
            default:
              Navigator.push(
                context,
                PageRouteBuilder(
                  opaque: false,
                  pageBuilder: (_, __, ___) => AlbumSearchPage(
                    query: value.toString(),
                    type: 'Artists',
                  ),
                ),
              );
              break;
          }
        },
      );
    }
  }
  
  class YtSongTileTrailingMenu extends StatefulWidget {
    final Map data;
    const YtSongTileTrailingMenu({super.key, required this.data});
  
    @override
    _YtSongTileTrailingMenuState createState() => _YtSongTileTrailingMenuState();
  }
  
  class _YtSongTileTrailingMenuState extends State<YtSongTileTrailingMenu> {
    @override
    Widget build(BuildContext context) {
      return PopupMenuButton(
        icon: Icon(
          Icons.more_vert_rounded,
          color: Theme.of(context).iconTheme.color,
        ),
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(15.0),
          ),
        ),
        itemBuilder: (context) => [
          PopupMenuItem(
            value: 0,
            child: Row(
              children: [
                Icon(
                  CupertinoIcons.search,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(
                  width: 10.0,
                ),
                Text(
                  AppLocalizations.of(
                    context,
                  )!
                      .searchHome,
                ),
              ],
            ),
          ),
          PopupMenuItem(
            value: 1,
            child: Row(
              children: [
                Icon(
                  Icons.playlist_play_rounded,
                  color: Theme.of(context).iconTheme.color,
                  size: 26.0,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.playNext),
              ],
            ),
          ),
          PopupMenuItem(
            value: 2,
            child: Row(
              children: [
                Icon(
                  Icons.queue_music_rounded,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.addToQueue),
              ],
            ),
          ),
          PopupMenuItem(
            value: 3,
            child: Row(
              children: [
                Icon(
                  Icons.playlist_add_rounded,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.addToPlaylist),
              ],
            ),
          ),
          PopupMenuItem(
            value: 4,
            child: Row(
              children: [
                Icon(
                  Icons.video_library_rounded,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.watchVideo),
              ],
            ),
          ),
          PopupMenuItem(
            value: 5,
            child: Row(
              children: [
                Icon(
                  Icons.share_rounded,
                  color: Theme.of(context).iconTheme.color,
                ),
                const SizedBox(width: 10.0),
                Text(AppLocalizations.of(context)!.share),
              ],
            ),
          ),
        ],
        onSelected: (int? value) {
          if (value == 0) {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => SearchPage(
                  query: widget.data['title'].toString(),
                ),
              ),
            );
          }
          if (value == 1 || value == 2 || value == 3) {
            YouTubeServices()
                .formatVideoFromId(
              id: widget.data['id'].toString(),
              data: widget.data,
            )
                .then((songMap) {
              final MediaItem mediaItem =
                  MediaItemConverter.mapToMediaItem(songMap!);
              if (value == 1) {
                playNext(mediaItem, context);
              }
              if (value == 2) {
                addToNowPlaying(context: context, mediaItem: mediaItem);
              }
              if (value == 3) {
                AddToPlaylist().addToPlaylist(context, mediaItem);
              }
            });
          }
          if (value == 4) {
            launchUrl(
              Uri.parse('https://youtube.com/watch?v=${widget.data["id"]}'),
              mode: LaunchMode.externalApplication,
            );
          }
          if (value == 5) {
            Share.share('https://youtube.com/watch?v=${widget.data["id"]}');
          }
        },
      );
    }
  }
  