import UIKit
import MapKit
import RxSwift
import PKHUD

class GlobalEventsController: UIViewController {
    
    @IBOutlet weak var map: MKMapView!
    
    let bag = DisposeBag()
    var filter: Filter!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        #if targetEnvironment(macCatalyst)
        
        if #available(OSX 11.0, *) {
            self.map.showsCompass = true
        }
        self.map.showsZoomControls = true
        
        #endif
        
        HUD.show(.progress)
        Api.events(with: self.filter)
            .observeOn(MainScheduler.init())
            .subscribe(onSuccess: { events in
                self.title = String.localizedStringWithFormat(NSLocalizedString("events found", comment: ""), events.count) 
                let pins = events.map(EventPin.init(event:))
                self.map.removeAnnotations(self.map.annotations)
                self.map.addAnnotations(pins)
                self.map.centerOnPins()
                HUD.hide()
            }) { error in
                HUD.show(error: error)
            }
            .disposed(by: self.bag)
    }
    
    @IBAction func close(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }
}
