//
//  MainViewController.swift
//  Pargi
//
//  Main view controller, contains both the map as well as the
//  detail VCs and controls the information the two display
//
//  Created by Henri Normak on 27/04/2017.
//  Copyright © 2017 Henri Normak. All rights reserved.
//

import Foundation
import UIKit
import Pulley

class MainViewController: PulleyViewController, MapViewControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Grab all zones and force them down to the map/detail views
        let zones = ApplicationData.currentDatabase.zones
        
        if let mapView = self.primaryContentViewController as? MapViewController {
            mapView.zones = zones
            mapView.delegate = self
        }
    }
    
    // MARK: MapViewControllerDelegate
    
    func mapViewController(_ controller: MapViewController, didUpdateVisibleZones zones: [Zone]) {
        let top = zones[0..<min(zones.count, 5)]
        print("Closest zones \(top.count) => \(top.map({ $0.code }))")
    }
}

//
//  MainViewController.swift
//  Pargi
//
//  Main view controller, contains both the map as well as the
//  detail VCs and controls the information the two display
//
//  Created by Henri Normak on 27/04/2017.
//  Copyright © 2017 Henri Normak. All rights reserved.
//

import Foundation
import UIKit
import Pulley

class MainViewController: PulleyViewController, MapViewControllerDelegate, DetailViewControllerDelegate {
    
    fileprivate var mapViewController: MapViewController? {
        get {
            return self.primaryContentViewController as? MapViewController
        }
    }
    
    fileprivate var detailViewController: DetailViewController? {
        get {
            return self.drawerContentViewController as? DetailViewController
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Grab all zones and force them down to the map/detail views
        let zones = ApplicationData.currentDatabase.zones
        
        if let mapView = self.mapViewController {
            mapView.zones = zones
            mapView.delegate = self
        }
        
        if let detailView = self.detailViewController {
            detailView.delegate = self
        }
    }
    
    // MARK: MapViewControllerDelegate
    
    func mapViewController(_ controller: MapViewController, didUpdateVisibleZones zones: [Zone]) {
        let bestMatches = Array(zones[0..<min(zones.count, 3)])
        
        if let detailView = self.drawerContentViewController as? DetailViewController {
            detailView.zones = bestMatches
        }
    }
    
    // MARK: DetailViewControllerDelegate
    
    func detailViewController(_ controller: DetailViewController, didSelectZone zone: Zone?) {
        print("Selected zone \(zone)")
    }
}

Handle zone selection in the main VC
Handle zone selection in the main VC

The main controller will contain all of the business logic, the detail/map VCs will merely feed in user actions and present data (as VCs should).
