/**
  * this is meant to return a subset of metadata properties that is useful for determining fanart
  * locations
  *
  * @param mediaType
  * @param mediaObject
  * @return
  */
 private Map<String, String> getMetadata(MediaType mediaType, Object mediaObject) {
   if (mediaType == MediaType.TV) {
     Map<String, String> props = new HashMap<String, String>();
     for (String key : new String[] {FanartUtil.SEASON_NUMBER, FanartUtil.EPISODE_NUMBER}) {
       String v = SageFanartUtil.GetMediaFileMetadata(mediaObject, key);
       if (!SageFanartUtil.isEmpty(v)) {
         props.put(key, v);
       }
     }
     return props;
   }
   return null;
 }
  public File GetFanartArtifactDir(
      Object mediaObject,
      MediaType mediaType,
      String mediaTitle,
      MediaArtifactType artifactType,
      String artifactTitle,
      Map<String, String> metadata,
      boolean create) {
    SimpleMediaFile mf = SageFanartUtil.GetSimpleMediaFile(mediaObject);
    mediaType = Utils.returnNonNull(mediaType, mf.getMediaType());
    mediaTitle = Utils.returnNonNull(mediaTitle, mf.getTitle());
    metadata = Utils.returnNonNull(metadata, getMetadata(mediaType, mediaObject));
    String fanartFolder = GetFanartCentralFolder();
    File f =
        FanartUtil.getCentralFanartDir(
            mediaType, mediaTitle, artifactType, artifactTitle, fanartFolder, metadata);

    if (create && f != null && !f.exists()) {
      sagex.phoenix.util.FileUtils.mkdirsQuietly(f);
      if (!f.exists()) {
        log.warn("Unable to create directory: " + f.getAbsolutePath() + "; Pemission issue?");
      }
    }

    return f;
  }
  public File GetFanartArtifactForTitle(
      Object mediaObject,
      MediaType mediaType,
      String mediaTitle,
      MediaArtifactType artifactType,
      String artifactTitle,
      Map<String, String> metadata,
      String centralFolder) {
    File art = null;

    art =
        FanartUtil.getCentralFanartArtifact(
            mediaType, mediaTitle, artifactType, artifactTitle, centralFolder, metadata);
    if (art == null || !art.exists()) {
      if (mediaType == MediaType.TV
          && metadata != null
          && metadata.get(FanartUtil.SEASON_NUMBER) != null) {
        // do a search without the season metadata
        art =
            FanartUtil.getCentralFanartArtifact(
                mediaType, mediaTitle, artifactType, artifactTitle, centralFolder, null);
      }
    }

    // if no matches, then find the first one
    if (art == null || !art.exists()) {
      File all[] =
          GetFanartArtifactsForTitle(
              mediaObject,
              mediaType,
              mediaTitle,
              artifactType,
              artifactTitle,
              metadata,
              centralFolder);
      if (!SageFanartUtil.isEmpty(all)) {
        art = all[0];
      }
    }

    if (log.isDebugEnabled()) {
      log.debug(
          "GetFanartArtifactForTitle: MediaType: "
              + mediaType
              + "; MediaTitle: "
              + mediaTitle
              + "; ArtifactType: "
              + artifactType
              + "; ArtifactTitle: "
              + artifactTitle
              + "; Artifact: "
              + art
              + "; Season: "
              + getSeason(metadata)
              + "; MediaFile: "
              + mediaObject);
    }
    return art;
  }
  public File[] GetFanartArtifacts(
      Object mediaObject,
      MediaType mediaType,
      String mediaTitle,
      MediaArtifactType artifactType,
      String artifactTitle,
      Map<String, String> metadata) {
    File files[] = null;
    SimpleMediaFile mf = SageFanartUtil.GetSimpleMediaFile(mediaObject);
    mediaType = Utils.returnNonNull(mediaType, mf.getMediaType());
    mediaTitle = Utils.returnNonNull(mediaTitle, mf.getTitle());
    metadata = Utils.returnNonNull(metadata, getMetadata(mediaType, mediaObject));
    String fanartFolder = GetFanartCentralFolder();

    if (!fanartConfig.getUseSeason()) {
      // if we are setup to not use season specific fanart, null out
      // metadata
      metadata = null;
    }

    files =
        GetFanartArtifactsForTitle(
            mediaObject,
            mediaType,
            mediaTitle,
            artifactType,
            artifactTitle,
            metadata,
            fanartFolder);

    if (!exists(files) && mf.getMediaType() == MediaType.MUSIC) {
      files =
          GetFanartArtifactsForTitle(
              mediaObject,
              mediaType,
              SageFanartUtil.GetAlbumArtist(mediaObject),
              artifactType,
              artifactTitle,
              metadata,
              fanartFolder);
      if (!exists(files)) {
        files =
            GetFanartArtifactsForTitle(
                mediaObject,
                mediaType,
                SageFanartUtil.GetAlbumPersonArtist(mediaObject),
                artifactType,
                artifactTitle,
                metadata,
                fanartFolder);
      }
    }

    // if no central fanart, then check for local fanart
    // if (!exists(files)) {
    // File f = localFanart.GetFanartArtifact(mediaObject, mediaType,
    // mediaTitle, artifactType, artifactTitle, metadata);
    // if (f.exists()) {
    // files = new File[] {f};
    // }
    // }

    return files;
  }
  public File GetFanartArtifact(
      Object mediaObject,
      MediaType mediaType,
      String mediaTitle,
      MediaArtifactType artifactType,
      String artifactTitle,
      Map<String, String> metadata) {
    File file = null;
    SimpleMediaFile mf = SageFanartUtil.GetSimpleMediaFile(mediaObject);
    mediaType = Utils.returnNonNull(mediaType, mf.getMediaType());
    mediaTitle = Utils.returnNonNull(mediaTitle, mf.getTitle());
    metadata = Utils.returnNonNull(metadata, getMetadata(mediaType, mediaObject));
    String fanartFolder = GetFanartCentralFolder();

    if (!fanartConfig.getUseSeason()) {
      // if we are setup to not use season specific fanart, null out
      // metadata
      metadata = null;
    }

    // check for a default file
    file = getDefaultArtifact(mediaObject, artifactType);

    if (file == null) {
      file =
          GetFanartArtifactForTitle(
              mediaObject,
              mediaType,
              mediaTitle,
              artifactType,
              artifactTitle,
              metadata,
              fanartFolder);
    }

    if (!exists(file) && mf.getMediaType() == MediaType.MUSIC) {
      file =
          GetFanartArtifactForTitle(
              mediaObject,
              MediaType.MUSIC,
              SageFanartUtil.GetAlbumArtist(mediaObject),
              artifactType,
              artifactTitle,
              metadata,
              GetFanartCentralFolder());

      if (!exists(file)) {
        file =
            GetFanartArtifactForTitle(
                mediaObject,
                MediaType.MUSIC,
                SageFanartUtil.GetAlbumPersonArtist(mediaObject),
                artifactType,
                artifactTitle,
                metadata,
                GetFanartCentralFolder());
      }
    }

    // fallback to local fanart
    // if (!file.exists()) {
    // file = localFanart.GetFanartArtifact(mediaObject, mediaType,
    // mediaTitle, artifactType, artifactTitle, metadata);
    // }

    return file;
  }