//
//  User.swift
//  SwipeMatchFirestore
//
//  Created by Binh Huynh on 1/24/19.
//  Copyright © 2019 Binh Huynh. All rights reserved.
//

import UIKit

struct User: ProducesCardViewModel {
    var name: String?
    var age: Int?
    var profession: String?
    var imageUrl: String?
    var imageUrl1: String?
    var imageUrl2: String?
    var imageUrl3: String?
    
    var uid: String?
    
    var minSeekingAge: Int?
    var maxSeekingAge: Int?
    
    init(dictionary: [String: Any]) {
        // we will initialize our user
        self.age = dictionary["age"] as? Int
        self.profession = dictionary["profession"] as? String
        self.name = dictionary["fullName"] as? String ?? ""
        self.imageUrl = dictionary["imageUrl"] as? String ?? ""
        self.imageUrl1 = dictionary["imageUrl1"] as? String
        self.imageUrl2 = dictionary["imageUrl2"] as? String
        self.imageUrl3 = dictionary["imageUrl3"] as? String
        
        self.minSeekingAge = dictionary["minSeekingAge"] as? Int
        self.maxSeekingAge = dictionary["maxSeekingAge"] as? Int
        self.uid = dictionary["uid"] as? String ?? ""
    }
    
    func toCardViewModel() -> CardViewModel {
        let attributedText = NSMutableAttributedString(string: name ?? "", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 32, weight: .heavy)])
        
        let ageString = (age != nil) ? "\(age!)" : "N//A"
        
        attributedText.append(NSAttributedString(string: " \(ageString)", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24, weight: .regular)]))
        
        let professionString = (profession != nil) ? "\(profession!)" : "Not available"
        
        attributedText.append(NSAttributedString(string: "\n\(professionString)", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .regular)]))
        
        var imageUrls = [String]() // empty string array
        if let url = imageUrl  { imageUrls.append(url) }
        if let url = imageUrl1 { imageUrls.append(url) }
        if let url = imageUrl2 { imageUrls.append(url) }
        if let url = imageUrl3 { imageUrls.append(url) }
        
        return CardViewModel(uid: self.uid ?? "", imageNames: imageUrls, attributedString: attributedText, textAlignment: .left)
    }
}



