import UIKit
import Kingfisher

class VehiclePhotoCell: UICollectionViewCell {
    @IBOutlet weak var photo: UIImageView!
    @IBOutlet weak var model: UILabel!
    @IBOutlet weak var date: UILabel!
    
    let formatter = DateFormatter()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.layer.cornerRadius = 8
        formatter.timeStyle = .none
        formatter.dateStyle = .medium
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        self.photo.kf.cancelDownloadTask()
    }
    
    func configure(with photoModel: VehiclePhoto) {
        if let url = URL(string: photoModel.url) {
            self.photo.kf.setImage(with: url)
        }
        
        self.model.text = "\(photoModel.brand ?? "") \(photoModel.model ?? "")"
        
        let date = Date(timeIntervalSince1970: photoModel.date/1000)
        self.date.text = formatter.string(from: date)
    }
}
