class AvdWizardUtils {

  @VisibleForTesting
  @Nullable
  public static File ensureSkinsAreCurrent(@Nullable File resourcePath,
                                           @Nullable File skinDestination,
                                           @Nullable File deviceName,
                                           boolean emulatorCanDecodeWebp,
                                           @NotNull FileOp fop) {
    if (resourcePath == null) {
      return (skinDestination == null) ? deviceName : skinDestination;
    }
    if (skinDestination == null) {
      return resourcePath;
    }
    // The resource and destination paths both exist.
    if (fop.exists(skinDestination)) {
      // The destination skin directory already exists. Check if its files are up to date.
      File resourceLayout = new File(resourcePath, FN_SKIN_LAYOUT);
      File destLayout = new File(skinDestination, FN_SKIN_LAYOUT);
      if (!resourceLayout.exists() ||
          (destLayout.exists() && destLayout.lastModified() >= resourceLayout.lastModified())) {
        // The 'dest/layout' file is up to date. Assume the other 'dest/' files are, also.
        return skinDestination;
      }
      // The resource and destination directories exists, but the destination has old files.
      // Remove the destination directory. We'll re-create it below.
      fop.deleteFileOrFolder(skinDestination);
    }
    // Create the destination skin directory and populate it from
    // the resource.
    try {
      fop.mkdirs(skinDestination);
      if (!emulatorCanDecodeWebp) {
        // Convert webp skin files to PNG for older versions of the emulator.
        convertWebpSkinToPng(fop, skinDestination, resourcePath);
      }
      else {
        // Normal copy
        for (File src : fop.listFiles(resourcePath)) {
          File target = new File(skinDestination, src.getName());
          if (fop.isFile(src)) {
            fop.copyFile(src, target);
          }
        }
      }
      return skinDestination;
    }
    catch (IOException e) {
      getLog().warn(String.format("Failed to copy skin directory to %1$s, using studio-relative path %2$s",
                                  skinDestination, resourcePath));
    }
    return resourcePath;
  }

}