 //
 // ----------------------------
 
+
 /**
- Settings for Moa image downloader.
- */
+
+Settings for Moa image downloader.
+
+*/
 public struct MoaSettings {
   /// Settings for caching of the images.
   public var cache = MoaSettingsCache() {

  }
  
  /**
   
   Called asynchronously by image downloader if imaged download fails.
   
   - parameter error: Error object.
   - parameter response: HTTP response object, can be useful for getting HTTP status code.
   - parameter isSimulated: True if the image was supplied by moa simulator rather than real network.
   
   */
  private func handleErrorAsync(_ error: Error?, response: HTTPURLResponse?, isSimulated: Bool) {
    if let errorImage = globalOrInstanceErrorImage {
      handleSuccessAsync(errorImage, isSimulated: isSimulated)

    let simulated = simulatedDownloader != nil
    
    imageDownloader?.startDownload(url,
      onSuccess: { [weak self] image in
        self?.handleSuccessAsync(image, isSimulated: simulated)
      },
      onError: { [weak self] error, response in
        self?.handleErrorAsync(error, response: response, isSimulated: simulated)
      }
    )
  }

  /**

  Called asynchronously by image downloader when image is received.
  
  - parameter image: Image received by the downloader.
  - parameter isSimulated: True if the image was supplied by moa simulator rather than real network.

  */
  private func handleSuccessAsync(_ image: MoaImage, isSimulated: Bool) {
    var imageForView: MoaImage? = image

    if let onSuccessAsync = onSuccessAsync {
      imageForView = onSuccessAsync(image)
    }

    if isSimulated {
      // Assign image in the same queue for simulated download to make unit testing simpler with synchronous code
      handleSuccessMainQueue(imageForView)
