package com.peterjosling.scroball;

import android.graphics.Bitmap;
import android.media.MediaMetadata;

import com.google.common.base.Optional;
import com.peterjosling.scroball.transforms.TitleExtractor;

import org.immutables.value.Value;

@Value.Immutable
public abstract class Track {

  public abstract String track();
  public abstract String artist();
  public abstract Optional<String> album();
  public abstract Optional<String> albumArtist();
  @Value.Auxiliary public abstract Optional<Long> duration();
  @Value.Auxiliary public abstract Optional<Bitmap> art();

  public boolean isValid() {
    return !track().equals("") && !artist().equals("");
  }

  public static Track fromMediaMetadata(MediaMetadata metadata) {
    String title = metadata.getString(MediaMetadata.METADATA_KEY_TITLE);
    String artist = metadata.getString(MediaMetadata.METADATA_KEY_ARTIST);
    String album = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM);
    String albumArtist = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST);
    Bitmap art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART);
    long duration = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION);

    if (title == null) {
      title = metadata.getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE);

      if (title  == null) {
        title = "";
      }
    }

    if (art == null) {
      art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
    }

    ImmutableTrack.Builder builder = ImmutableTrack.builder()
        .track(title)
        .artist(artist)
        .album(Optional.fromNullable(album))
        .albumArtist(Optional.fromNullable(albumArtist))
        .art(Optional.fromNullable(art));

    if (duration > 0) {
      builder.duration(duration);
    }

    if (artist == null) {
      new TitleExtractor().transform(builder.build());
    }
    return builder.build();
  }

  @Value.Lazy
  public static Track empty() {
    return ImmutableTrack.builder().track("").artist("").build();
  }
}

@@ -1559,20 +1559,18 @@
       .a
-lbum
+rt
 (Optiona
@@ -1589,170 +1589,252 @@
 le(a
-lbum))%0A        .albumArtist(Optional.fromNullable(albumArtist))%0A        .art(Optional.fromNullable(art));%0A%0A    if (duration %3E 0) %7B%0A      builder.duration(duration
+rt));%0A%0A    if (duration %3E 0) %7B%0A      builder.duration(duration);%0A    %7D%0A    if (album != null && !album.isEmpty()) %7B%0A      builder.album(album);%0A    %7D%0A    if (albumArtist != null && !albumArtist.isEmpty()) %7B%0A      builder.albumArtist(albumArtist
 );%0A 

Fix Track handling of empty strings for optional fields