import { CaseInsensitiveTagMap } from '../common/CaseInsensitiveTagMap.js';
import type { INativeTagMap } from '../common/GenericTagTypes.js';
import type {ITag} from "../type.js";
import type {INativeMetadataCollector} from "../common/MetadataCollector.js";

/**
 * Ref: https://github.com/sergiomb2/libmp4v2/wiki/iTunesMetadata
 */
const mp4TagMap: INativeTagMap = {
  '©nam': 'title',
  '©ART': 'artist',
  aART: 'albumartist',
  /**
   * ToDo: Album artist seems to be stored here while Picard documentation says: aART
   */
  '----:com.apple.iTunes:Band': 'albumartist',
  '©alb': 'album',
  '©day': 'date',
  '©cmt': 'comment',
  '©com': 'comment',
  trkn: 'track',
  disk: 'disk',
  '©gen': 'genre',
  covr: 'picture',
  '©wrt': 'composer',
  '©lyr': 'lyrics',
  soal: 'albumsort',
  sonm: 'titlesort',
  soar: 'artistsort',
  soaa: 'albumartistsort',
  soco: 'composersort',
  '----:com.apple.iTunes:LYRICIST': 'lyricist',
  '----:com.apple.iTunes:CONDUCTOR': 'conductor',
  '----:com.apple.iTunes:REMIXER': 'remixer',
  '----:com.apple.iTunes:ENGINEER': 'engineer',
  '----:com.apple.iTunes:PRODUCER': 'producer',
  '----:com.apple.iTunes:DJMIXER': 'djmixer',
  '----:com.apple.iTunes:MIXER': 'mixer',
  '----:com.apple.iTunes:LABEL': 'label',
  '©grp': 'grouping',
  '----:com.apple.iTunes:SUBTITLE': 'subtitle',
  '----:com.apple.iTunes:DISCSUBTITLE': 'discsubtitle',
  cpil: 'compilation',
  tmpo: 'bpm',
  '----:com.apple.iTunes:MOOD': 'mood',
  '----:com.apple.iTunes:MEDIA': 'media',
  '----:com.apple.iTunes:CATALOGNUMBER': 'catalognumber',
  tvsh: 'tvShow',
  tvsn: 'tvSeason',
  tves: 'tvEpisode',
  sosn: 'tvShowSort',
  tven: 'tvEpisodeId',
  tvnn: 'tvNetwork',
  pcst: 'podcast',
  purl: 'podcasturl',
  '----:com.apple.iTunes:MusicBrainz Album Status': 'releasestatus',
  '----:com.apple.iTunes:MusicBrainz Album Type': 'releasetype',
  '----:com.apple.iTunes:MusicBrainz Album Release Country': 'releasecountry',
  '----:com.apple.iTunes:SCRIPT': 'script',
  '----:com.apple.iTunes:LANGUAGE': 'language',
  cprt: 'copyright',
  '©cpy': 'copyright',
  '----:com.apple.iTunes:LICENSE': 'license',
  '©too': 'encodedby',
  pgap: 'gapless',
  '----:com.apple.iTunes:BARCODE': 'barcode',
  '----:com.apple.iTunes:ISRC': 'isrc',
  '----:com.apple.iTunes:ASIN': 'asin',
  '----:com.apple.iTunes:NOTES': 'comment',
  '----:com.apple.iTunes:MusicBrainz Track Id': 'musicbrainz_recordingid',
  '----:com.apple.iTunes:MusicBrainz Release Track Id': 'musicbrainz_trackid',
  '----:com.apple.iTunes:MusicBrainz Album Id': 'musicbrainz_albumid',
  '----:com.apple.iTunes:MusicBrainz Artist Id': 'musicbrainz_artistid',
  '----:com.apple.iTunes:MusicBrainz Album Artist Id': 'musicbrainz_albumartistid',
  '----:com.apple.iTunes:MusicBrainz Release Group Id': 'musicbrainz_releasegroupid',
  '----:com.apple.iTunes:MusicBrainz Work Id': 'musicbrainz_workid',
  '----:com.apple.iTunes:MusicBrainz TRM Id': 'musicbrainz_trmid',
  '----:com.apple.iTunes:MusicBrainz Disc Id': 'musicbrainz_discid',
  '----:com.apple.iTunes:Acoustid Id': 'acoustid_id',
  '----:com.apple.iTunes:Acoustid Fingerprint': 'acoustid_fingerprint',
  '----:com.apple.iTunes:MusicIP PUID': 'musicip_puid',
  '----:com.apple.iTunes:fingerprint': 'musicip_fingerprint',
  '----:com.apple.iTunes:replaygain_track_gain': 'replaygain_track_gain',
  '----:com.apple.iTunes:replaygain_track_peak': 'replaygain_track_peak',
  '----:com.apple.iTunes:replaygain_album_gain': 'replaygain_album_gain',
  '----:com.apple.iTunes:replaygain_album_peak': 'replaygain_album_peak',
  '----:com.apple.iTunes:replaygain_track_minmax': 'replaygain_track_minmax',
  '----:com.apple.iTunes:replaygain_album_minmax': 'replaygain_album_minmax',
  '----:com.apple.iTunes:replaygain_undo': 'replaygain_undo',
  // Additional mappings:
  gnre: 'genre', // ToDo: check mapping

  '----:com.apple.iTunes:ALBUMARTISTSORT': 'albumartistsort',
  '----:com.apple.iTunes:ARTISTS': 'artists',
  '----:com.apple.iTunes:ORIGINALDATE': 'originaldate',
  '----:com.apple.iTunes:ORIGINALYEAR': 'originalyear',
  '----:com.apple.iTunes:RELEASEDATE': 'releasedate',
  // '----:com.apple.iTunes:PERFORMER': 'performer'
  desc: 'description',
  ldes: 'longDescription',
  '©mvn': 'movement',
  '©mvi': 'movementIndex',
  '©mvc': 'movementTotal',
  '©wrk': 'work',
  catg: 'category',
  egid: 'podcastId',
  hdvd: 'hdVideo',
  keyw: 'keywords',
  shwm: 'showMovement',
  stik: 'stik',
  rate: 'rating'
};

export const tagType = 'iTunes';

export class MP4TagMapper extends CaseInsensitiveTagMap {

  public constructor() {
    super([tagType], mp4TagMap);
  }

  protected postMap(tag: ITag, warnings: INativeMetadataCollector): void {

    switch (tag.id) {

      case 'rate':
        tag.value = {
          source: undefined,
          rating: Number.parseFloat(tag.value as string) / 100
        };
        break;
    }
  }

}
