part of 'track.dart';

class AlbumIdentifierWrapper {
  String get displayAlbumName => album.isEmpty ? UnknownTags.ALBUM : album;

  final String album, albumArtist, year;

  const AlbumIdentifierWrapper({
    required this.album,
    required this.albumArtist,
    required this.year,
  });

  static String _normalize(String text, String parentDirPath) => text.isEmpty ? text : DownloadTaskFilename.cleanupFilename(text, parentDirPath: parentDirPath);

  static List<AlbumIdentifierWrapper> fromAlbums({
    required List<String> albums,
    required String albumArtist,
    required String year,
  }) {
    return albums
        .map(
          (a) => AlbumIdentifierWrapper(
            album: a,
            albumArtist: albumArtist,
            year: year,
          ),
        )
        .toList();
  }

  String resolved() => resolve(settings.albumIdentifiers.value, AppDirs.ARTWORKS);
  String resolve(List<AlbumIdentifier> identifiers, String parentDirPath) {
    final modified = modifyOnly(identifiers);
    return "${_normalize(modified.album, parentDirPath)}${_normalize(modified.albumArtist, parentDirPath)}${_normalize(modified.year, parentDirPath)}";
  }

  AlbumIdentifierWrapper modifiedOnly() => modifyOnly(settings.albumIdentifiers.value);
  AlbumIdentifierWrapper modifyOnly(List<AlbumIdentifier> identifiers) {
    final idWrapper = this;
    final n = identifiers.contains(AlbumIdentifier.albumName) ? idWrapper.album : '';
    final aa = identifiers.contains(AlbumIdentifier.albumArtist) ? idWrapper.albumArtist : '';
    final y = identifiers.contains(AlbumIdentifier.year) ? idWrapper.year : '';
    return AlbumIdentifierWrapper(
      album: n,
      albumArtist: aa,
      year: y,
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'album': album,
      'albumArtist': albumArtist,
      'year': year,
    };
  }

  factory AlbumIdentifierWrapper.fromMap(Map<String, dynamic> map) {
    return AlbumIdentifierWrapper(
      album: map['album'] as String,
      albumArtist: map['albumArtist'] as String,
      year: map['year'] as String,
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (other is! AlbumIdentifierWrapper) return false;

    return other.album == album && other.albumArtist == albumArtist && other.year == year;
  }

  @override
  int get hashCode => album.hashCode ^ albumArtist.hashCode ^ year.hashCode;

  @override
  String toString() => 'AlbumIdentifierWrapper(album: $album, albumArtist:$albumArtist, year: $year)';
}
