import UIKit
import MapKit

class ShowEventController: UIViewController {
    
    private var map = MKMapView()
    var event: VehicleEvent?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.map.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(self.map)
        self.map.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        self.map.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        self.map.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        self.map.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        
        if let event = self.event {
            self.title = event.address ?? String(format: "%.02f, %.02f", event.latitude, event.longitude)
            let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: event.latitude, longitude: event.longitude), latitudinalMeters: 1000, longitudinalMeters: 1000)
            self.map.setRegion(region, animated: true)
            let pin = EventPin(event: event)
            self.map.addAnnotation(pin)
        }
    }
}
