 import Foundation
 import ObjectMapper
 
+enum PostStyle {
+    case ImageOnly
+    case ImageWithText
+    case TextOnly
+}
 
 class Post: Mappable {
 

    var imageNameString: String? { return "\(tim!)\(ext!)" }
    var thumbnailNameString: String? { return "\(tim!)s.jpg" }

    func getAttributedComment() -> NSAttributedString? {
        guard let comment = self.com else { return nil }
        let attributedString = comment.html2AttributedString?.mutableCopy() as! NSMutableAttributedString

        let foundQuotes = self.matchesForRegexInText(">>[0-9]+", text: attributedString.string)
        foundQuotes.forEach { (quoteRange, quotedPost) in
            let linkValue = "quote://\(quotedPost.substring(from: 2))"
            attributedString.addAttribute(NSLinkAttributeName, value: linkValue, range: quoteRange)
        }
        return attributedString
    }


    func matchesForRegexInText(_ regex: String!, text: String!) -> [(NSRange, NSString)] {
        // TODO: move somewhere else
        guard let regex = try? NSRegularExpression(pattern: regex, options: []) else { return [] }


        return results.map { ($0.range, nsString.substring(with: $0.range) as NSString ) }
    }

    lazy var attributedComment: NSAttributedString = {
        guard let comment = self.com else { return NSAttributedString() }

        let attributedString = comment.html2AttributedString?.mutableCopy() as! NSMutableAttributedString
        let foundQuotes = self.matchesForRegexInText(">>[0-9]+", text: attributedString.string)

        foundQuotes.forEach { (quoteRange, quotedPost) in
            let linkValue = "quote://\(quotedPost.substring(from: 2))"
            attributedString.addAttribute(NSLinkAttributeName, value: linkValue, range: quoteRange)
        }
        return attributedString
    }()

    lazy var style: PostStyle = {
        if self.tim == nil {
            return .TextOnly
        }
        else if self.com == nil && self.tim != nil {
            return .ImageOnly
        }
        return .ImageWithText
    }()
}

extension String {
