 import Foundation
 import UIKit
 
+
+// MARK: - MediaAttachmentDelegate
+//
 protocol MediaAttachmentDelegate: class {
     func mediaAttachment(
         _ mediaAttachment: MediaAttachment,
-        imageForURL url: URL,
+        imageFor url: URL,
         onSuccess success: @escaping (UIImage) -> (),
         onFailure failure: @escaping () -> ())
 
     func mediaAttachmentPlaceholderImageFor(attachment: MediaAttachment) -> UIImage
 }
 
-/// Custom text attachment.
-///
+// MARK: - MediaAttachment
+//
 open class MediaAttachment: NSTextAttachment {
 
+    /// This property allows the global customization of appearance properties of the TextAttachment
+    ///
+    open static var appearance = Appearance()
+
     /// Attributes accessible by the user, for general purposes.
     ///
     public var extraAttributes = [String: String]()
 
-    public struct Appearance {
-        public var overlayColor = UIColor(white: 0.6, alpha: 0.6)
-
-        /// The height of the progress bar for progress indicators
-        public var progressHeight = CGFloat(2.0)
-
-        /// The color to use when drawing the backkground of the progress indicators
-        ///
-        public var progressBackgroundColor = UIColor.cyan
-
-        /// The color to use when drawing progress indicators
-        ///
-        public var progressColor = UIColor.blue
-
-        /// The margin apply to the images being displayed. This is to avoid that two images in a row get glued together.
-        ///
-        public var imageMargin = CGFloat(10.0)
-    }
-
-
-    /// This property allows the global customization of appearance properties of the TextAttachment
-    ///
-    open static var appearance: Appearance = Appearance()
-
     /// Identifier used to match this attachment with a custom UIView subclass
     ///
-    fileprivate(set) open var identifier: String
+    private(set) open var identifier: String
     
     /// Attachment URL
     ///

        }
    }

    fileprivate enum EncodeKeys: String {
        case identifier
        case url
    }

    // MARK: - Position and size calculation
    func xPosition(forContainerWidth containerWidth: CGFloat) -> CGFloat {
            return 0
    }

    func onScreenHeight(_ containerWidth: CGFloat) -> CGFloat {
        if let image = image {
            let targetWidth = onScreenWidth(containerWidth)
            let scale = targetWidth / image.size.width

            return floor(image.size.height * scale) + (imageMargin * 2)
        } else {
            return 0
        }
    }

    func onScreenWidth(_ containerWidth: CGFloat) -> CGFloat {
        if let image = image {
            return floor(min(image.size.width, containerWidth))
        } else {
            return 0
        }
    }

    // MARK: - NSTextAttachmentContainer

    override open func image(forBounds imageBounds: CGRect, textContainer: NSTextContainer?, characterIndex charIndex: Int) -> UIImage? {
        
        updateImage(inTextContainer: textContainer)

        guard let image = image else {
            return delegate!.mediaAttachmentPlaceholderImageFor(attachment: self)

                
                strongSelf.isFetchingImage = false
                strongSelf.lastRequestedURL = nil
                strongSelf.invalidateLayout(in: textContainer)
            })
    }
    
    func invalidateLayout(in textContainer: NSTextContainer?) {
        textContainer?.layoutManager?.invalidateLayout(for: self)
    }
}
