package com.peterjosling.scroball.transforms;

import com.google.common.base.Joiner;
import com.peterjosling.scroball.Track;

import java.util.Arrays;

public class TitleExtractor implements MetadataTransform {

  private static final String[] SEPARATORS =
      new String[]{" -- ", "--", " - ", " – ", " — ", "-", "–", "—", ":", "|", "///"};

  @Override
  public Track transform(Track track) {
    String title = null;
    String artist = null;

    for (String separator : SEPARATORS) {
      String[] components = track.track().split(separator);

      if (components.length > 1) {
        String[] titleComponents = Arrays.copyOfRange(components, 1, components.length);

        artist = components[0];
        title = Joiner.on(separator).join(titleComponents);
        break;
      }
    }

    if (title == null || artist == null) {
      return track;
    }

    title = title.trim();
    artist = artist.trim();

    return track.toBuilder().artist(artist).track(title).build();
  }
}

@@ -143,16 +143,48 @@
 .Arrays;
+%0Aimport java.util.regex.Pattern;
 %0A%0Apublic
@@ -564,16 +564,30 @@
 ).split(
+Pattern.quote(
 separato
@@ -588,16 +588,17 @@
 parator)
+)
 ;%0A%0A     

Fix TitleExtractor adding extra | to unmatched titles