import UIKit

class VehicleCell: UITableViewCell, ConfigurableCell {
    
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var plate: PlateView!
    @IBOutlet weak var addedDate: UILabel!
    @IBOutlet weak var updatedDate: UILabel!
    
    let formatter = DateFormatter()

    override func awakeFromNib() {
        super.awakeFromNib()
        
        formatter.dateStyle = .short
        formatter.timeStyle = .short
    }
    
    func configure(with vehicle: Vehicle) {
        self.name.text = vehicle.brand?.name?.original ?? "<unknown>"
		self.plate.number = PlateNumber(vehicle.getNumber())
        if vehicle.unrecognized {
            self.plate.foreground = .systemRed
        } else if vehicle.outdated {
            self.plate.foreground = .systemGray3
        } else {
            self.plate.foreground = nil
        }
        
        if vehicle.updatedDate - vehicle.addedDate > 60 {
            self.addedDate.text = formatter.string(from: Date(timeIntervalSince1970: vehicle.addedDate))
            self.updatedDate.text = formatter.string(from: Date(timeIntervalSince1970: vehicle.updatedDate))
            self.addedDate.isHidden = false
        } else {
            self.addedDate.isHidden = true
            self.updatedDate.text = formatter.string(from: Date(timeIntervalSince1970: vehicle.updatedDate))
        }

    }
}
