<commit_before>//
//  ViewController.swift
//  TripKitUIExample
//
//  Created by <NAME> on 29.05.19.
//  Copyright © 2019 SkedGo Pty Ltd. All rights reserved.
//

import UIKit

import TGCardViewController
import TripKitUI
import TripKitInterApp

class MainViewController: UITableViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    
    // Customizing the look of TripKitUI, showing how to integrate the
    // inter-app actions from TripKitInterApp
     if #available(iOS 13.0, *) {
      TKUITripOverviewCard.config.segmentActionsfactory = { segment in
        var actions = [TKUITripOverviewCardAction]()
        
        if segment.isPublicTransport {
          actions.append(SegmentTimetableAction(segment: segment))
        }
        
        for action in TKInterAppCommunicator.shared.externalActions(for: segment) {
          actions.append(SegmentExternalAction(segment: segment, action: action))
        }
        
        if TKInterAppCommunicator.canOpenInMapsApp(segment) {
          actions.append(SegmentDirectionsAction(segment: segment))
        }
        return actions
      }
    }
    
  }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard let id = tableView.cellForRow(at: indexPath)?.accessibilityIdentifier else {
      preconditionFailure("Missing accessibility identifier for row at \(indexPath)")
    }
    
    switch id {
    case "showSearch":
      showSearch()
      
    case "showRoutes":
      showRoutes()
      
    case "showHome":
      showHome()
      
    default:
      preconditionFailure("Don't know what to do with \(id)")
    }
    
    tableView.deselectRow(at: indexPath, animated: true)
  }
  
}

// MARK: - Search

extension MainViewController {

  func showSearch() {
    // We include TKSkedGoGeocoder here which will provide public transport
    // locations. This requires also a bias map rect, as this only searches on
    // a per-city level.
    guard let randomCity = TKRegionManager.shared.regions.randomElement()?.cities.first else {
      print("Couldn't search as regions weren't fetched.")
      return
    }
    
    let resultsController = TKUIAutocompletionViewController(providers: [TKSkedGoGeocoder()])
    resultsController.biasMapRect = randomCity.centerBiasedMapRect

    resultsController.delegate = self

    // Fix for bad padding between search bar and first row
    // Kudos to https://stackoverflow.com/questions/40435806/extra-space-on-top-of-uisearchcontrollers-uitableview
    resultsController.tableView.contentInsetAdjustmentBehavior = .never

    let searchController = UISearchController(searchResultsController: resultsController)
    searchController.searchResultsUpdater = resultsController
    searchController.searchBar.placeholder = "Search in \(randomCity.title ?? "unnamed city")"
    self.present(searchController, animated: true)
  }

}

extension MainViewController: TKUIAutocompletionViewControllerDelegate {
  
  func autocompleter(_ controller: TKUIAutocompletionViewController, didSelect annotation: MKAnnotation) {
    if let stop = annotation as? TKUIStopAnnotation {
      dismiss(animated: true) {
        self.showDepartures(stop: stop)
      }
      
    } else {
      print("Selected \(annotation)")
    }
  }
  
  func autocompleter(_ controller: TKUIAutocompletionViewController, didSelectAccessoryFor annotation: MKAnnotation) {
    if let stop = annotation as? TKUIStopAnnotation {
      dismiss(animated: true) {
        self.showDepartures(stop: stop)
      }

    } else {
      print("Selected accessor for \(annotation)")
    }
  }
  
}


// MARK: - Departures

extension MainViewController {
  
  func showDepartures(stop: TKUIStopAnnotation) {
    let departures = TKUITimetableViewController(stop: stop)
    departures.delegate = self
    navigationController?.setNavigationBarHidden(true, animated: true)
    navigationController?.pushViewController(departures, animated: true)
  }
  
}

extension MainViewController: TKUITimetableViewControllerDelegate {
  
  func requestsDismissal(for controller: TGCardViewController) {
    navigationController?.setNavigationBarHidden(false, animated: true)
    navigationController?.popViewController(animated: true)
  }
  
}


// MARK: - Routes

extension MainViewController {

  func showRoutes() {
    
    // we generate a random routing request from some city to a random destination
    // diagonally nearby
    guard let randomCity = TKRegionManager.shared.regions.randomElement()?.cities.first else {
      print("Couldn't search as regions weren't fetched.")
      return
    }

    let delta = Double((-10...10).randomElement()!) / 50.0
    let nearby = MKPointAnnotation()
    nearby.coordinate = CLLocationCoordinate2D(latitude: randomCity.coordinate.latitude + delta, longitude: randomCity.coordinate.longitude + delta)
    nearby.title = "Destination"

    let request = TripRequest.insert(
      from: randomCity,
      to: nearby,
      for: nil, timeType: .leaveASAP, into: TripKit.shared.tripKitContext
    )
    
    let routes = TKUIRoutingResultsViewController(request: request)
    routes.delegate = self
    navigationController?.setNavigationBarHidden(true, animated: true)
    navigationController?.pushViewController(routes, animated: true)
  }

}

extension MainViewController: TKUIRoutingResultsViewControllerDelegate {
}

// MARK: - Home

extension MainViewController {
  
  func showHome() {
    let homeController = TKUIHomeViewController()
    homeController.searchResultsDelegate = self
    present(homeController, animated: true, completion: nil)
  }
  
}

extension MainViewController: TKUIHomeCardSearchResultsDelegate {
  
  func homeCard(_ card: TKUIHomeCard, selected searchResult: MKAnnotation) {
    InMemoryHistoryManager.shared.add(searchResult)
  }
  
}

// MARK: - Segment actions

@available(iOS 13.0, *)
fileprivate struct SegmentExternalAction: TKUITripOverviewCardAction {
  let segment: TKSegment
  let action: TKInterAppCommunicator.ExternalAction
  
  var title: String { action.title }
  <commit_msg>push instead of presenting home controller<commit_after>//
//  ViewController.swift
//  TripKitUIExample
//
//  Created by <NAME> on 29.05.19.
//  Copyright © 2019 SkedGo Pty Ltd. All rights reserved.
//

import UIKit

import TGCardViewController
import TripKitUI
import TripKitInterApp

class MainViewController: UITableViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    
    // Customizing the look of TripKitUI, showing how to integrate the
    // inter-app actions from TripKitInterApp
     if #available(iOS 13.0, *) {
      TKUITripOverviewCard.config.segmentActionsfactory = { segment in
        var actions = [TKUITripOverviewCardAction]()
        
        if segment.isPublicTransport {
          actions.append(SegmentTimetableAction(segment: segment))
        }
        
        for action in TKInterAppCommunicator.shared.externalActions(for: segment) {
          actions.append(SegmentExternalAction(segment: segment, action: action))
        }
        
        if TKInterAppCommunicator.canOpenInMapsApp(segment) {
          actions.append(SegmentDirectionsAction(segment: segment))
        }
        return actions
      }
    }
    
  }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard let id = tableView.cellForRow(at: indexPath)?.accessibilityIdentifier else {
      preconditionFailure("Missing accessibility identifier for row at \(indexPath)")
    }
    
    switch id {
    case "showSearch":
      showSearch()
      
    case "showRoutes":
      showRoutes()
      
    case "showHome":
      showHome()
      
    default:
      preconditionFailure("Don't know what to do with \(id)")
    }
    
    tableView.deselectRow(at: indexPath, animated: true)
  }
  
}

// MARK: - Search

extension MainViewController {

  func showSearch() {
    // We include TKSkedGoGeocoder here which will provide public transport
    // locations. This requires also a bias map rect, as this only searches on
    // a per-city level.
    guard let randomCity = TKRegionManager.shared.regions.randomElement()?.cities.first else {
      print("Couldn't search as regions weren't fetched.")
      return
    }
    
    let resultsController = TKUIAutocompletionViewController(providers: [TKSkedGoGeocoder()])
    resultsController.biasMapRect = randomCity.centerBiasedMapRect

    resultsController.delegate = self

    // Fix for bad padding between search bar and first row
    // Kudos to https://stackoverflow.com/questions/40435806/extra-space-on-top-of-uisearchcontrollers-uitableview
    resultsController.tableView.contentInsetAdjustmentBehavior = .never

    let searchController = UISearchController(searchResultsController: resultsController)
    searchController.searchResultsUpdater = resultsController
    searchController.searchBar.placeholder = "Search in \(randomCity.title ?? "unnamed city")"
    self.present(searchController, animated: true)
  }

}

extension MainViewController: TKUIAutocompletionViewControllerDelegate {
  
  func autocompleter(_ controller: TKUIAutocompletionViewController, didSelect annotation: MKAnnotation) {
    if let stop = annotation as? TKUIStopAnnotation {
      dismiss(animated: true) {
        self.showDepartures(stop: stop)
      }
      
    } else {
      print("Selected \(annotation)")
    }
  }
  
  func autocompleter(_ controller: TKUIAutocompletionViewController, didSelectAccessoryFor annotation: MKAnnotation) {
    if let stop = annotation as? TKUIStopAnnotation {
      dismiss(animated: true) {
        self.showDepartures(stop: stop)
      }

    } else {
      print("Selected accessor for \(annotation)")
    }
  }
  
}


// MARK: - Departures

extension MainViewController {
  
  func showDepartures(stop: TKUIStopAnnotation) {
    let departures = TKUITimetableViewController(stop: stop)
    departures.delegate = self
    navigationController?.setNavigationBarHidden(true, animated: true)
    navigationController?.pushViewController(departures, animated: true)
  }
  
}

extension MainViewController: TKUITimetableViewControllerDelegate {
  
  func requestsDismissal(for controller: TGCardViewController) {
    navigationController?.setNavigationBarHidden(false, animated: true)
    navigationController?.popViewController(animated: true)
  }
  
}


// MARK: - Routes

extension MainViewController {

  func showRoutes() {
    
    // we generate a random routing request from some city to a random destination
    // diagonally nearby
    guard let randomCity = TKRegionManager.shared.regions.randomElement()?.cities.first else {
      print("Couldn't search as regions weren't fetched.")
      return
    }

    let delta = Double((-10...10).randomElement()!) / 50.0
    let nearby = MKPointAnnotation()
    nearby.coordinate = CLLocationCoordinate2D(latitude: randomCity.coordinate.latitude + delta, longitude: randomCity.coordinate.longitude + delta)
    nearby.title = "Destination"

    let request = TripRequest.insert(
      from: randomCity,
      to: nearby,
      for: nil, timeType: .leaveASAP, into: TripKit.shared.tripKitContext
    )
    
    let routes = TKUIRoutingResultsViewController(request: request)
    routes.delegate = self
    navigationController?.setNavigationBarHidden(true, animated: true)
    navigationController?.pushViewController(routes, animated: true)
  }

}

extension MainViewController: TKUIRoutingResultsViewControllerDelegate {
}

// MARK: - Home

extension MainViewController {
  
  func showHome() {
    let homeController = TKUIHomeViewController()
    homeController.searchResultsDelegate = self
    navigationController?.setNavigationBarHidden(true, animated: true)
    navigationController?.pushViewController(homeController, animated: true)
  }
  
}

extension MainViewController: TKUIHomeCardSearchResultsDelegate {
  
  func homeCard(_ card: TKUIHomeCard, selected searchResult: MKAnnotation) {
    InMemoryHistoryManager.shared.add(searchResult)
  }
  
}

// MARK: - Segment actions

@available(iOS 13.0, *)
fileprivate struct SegmentExternalAction: TKUITripOverviewCardAction {
  let segment: TKSegment
  let action: TKInterAppCommunicator.ExternalAction
  
  var title: String { action.title }
  
{'fraction_non_alphanumeric': 0.08388879856933831, 'fraction_numerical': 0.005202406112827182, 'mean_word_length': 3.5381777941718924, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '519952', 'n_tokens_mistral': 3579, 'n_tokens_neox': 3329, 'n_words': 802}