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:blackhole/CustomWidgets/custom_physics.dart';
  import 'package:blackhole/CustomWidgets/song_tile_trailing_menu.dart';
  import 'package:blackhole/Helpers/image_resolution_modifier.dart';
  import 'package:cached_network_image/cached_network_image.dart';
  import 'package:flutter/material.dart';
  
  class HorizontalAlbumsListSeparated extends StatelessWidget {
    final List songsList;
    final Function(int) onTap;
    const HorizontalAlbumsListSeparated({
      super.key,
      required this.songsList,
      required this.onTap,
    });
  
    String formatString(String? text) {
      return text == null
          ? ''
          : text
              .replaceAll('&amp;', '&')
              .replaceAll('&#039;', "'")
              .replaceAll('&quot;', '"')
              .trim();
    }
  
    String getSubTitle(Map item) {
      final type = item['type'];
      if (type == 'charts') {
        return '';
      } else if (type == 'playlist' || type == 'radio_station') {
        return formatString(item['subtitle']?.toString());
      } else if (type == 'song') {
        return formatString(item['artist']?.toString());
      } else {
        if (item['subtitle'] != null) {
          return formatString(item['subtitle']?.toString());
        }
        final artists = item['more_info']?['artistMap']?['artists']
            .map((artist) => artist['name'])
            .toList();
        if (artists != null) {
          return formatString(artists?.join(', ')?.toString());
        }
        if (item['artist'] != null) {
          return formatString(item['artist']?.toString());
        }
        return '';
      }
    }
  
    @override
    Widget build(BuildContext context) {
      final bool rotated =
          MediaQuery.of(context).size.height < MediaQuery.of(context).size.width;
      final bool biggerScreen = MediaQuery.of(context).size.width > 1050;
      final double portion = (songsList.length <= 4) ? 1.0 : 0.875;
      final double listSize = rotated
          ? biggerScreen
              ? MediaQuery.of(context).size.width * portion / 3
              : MediaQuery.of(context).size.width * portion / 2
          : MediaQuery.of(context).size.width * portion;
      return SizedBox(
        height: songsList.length < 4 ? songsList.length * 74 : 74 * 4,
        child: Align(
          alignment: Alignment.centerLeft,
          child: ListView.builder(
            physics: PagingScrollPhysics(itemDimension: listSize),
            scrollDirection: Axis.horizontal,
            shrinkWrap: true,
            itemExtent: listSize,
            itemCount: (songsList.length / 4).ceil(),
            itemBuilder: (context, index) {
              final itemGroup = songsList.skip(index * 4).take(4);
              return SizedBox(
                height: 72 * 4,
                width: listSize,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: itemGroup.map((item) {
                    final subTitle = getSubTitle(item as Map);
                    return ListTile(
                      title: Text(
                        formatString(item['title']?.toString()),
                        overflow: TextOverflow.ellipsis,
                      ),
                      subtitle: Text(
                        subTitle,
                        overflow: TextOverflow.ellipsis,
                      ),
                      leading: Card(
                        margin: EdgeInsets.zero,
                        elevation: 5,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(7.0),
                        ),
                        clipBehavior: Clip.antiAlias,
                        child: SizedBox.square(
                          dimension: 55.0,
                          child: CachedNetworkImage(
                            fit: BoxFit.cover,
                            errorWidget: (context, _, __) => const Image(
                              fit: BoxFit.cover,
                              image: AssetImage('assets/cover.jpg'),
                            ),
                            imageUrl: getImageUrl(item['image'].toString()),
                            placeholder: (context, url) => Image(
                              fit: BoxFit.cover,
                              image: (item['type'] == 'playlist' ||
                                      item['type'] == 'album')
                                  ? const AssetImage(
                                      'assets/album.png',
                                    )
                                  : item['type'] == 'artist'
                                      ? const AssetImage(
                                          'assets/artist.png',
                                        )
                                      : const AssetImage(
                                          'assets/cover.jpg',
                                        ),
                            ),
                          ),
                        ),
                      ),
                      trailing: SongTileTrailingMenu(
                        data: item,
                      ),
                      onTap: () => onTap(songsList.indexOf(item)),
                    );
                  }).toList(),
                ),
              );
            },
          ),
        ),
      );
    }
  }
  