//
//  CryptoRouter.swift
//  CleanStore
//
//  Created by João Pedro Giarrante on 27/09/20.
//  Copyright (c) 2020 Clean Swift LLC. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//

import UIKit

@objc protocol CryptoRoutingLogic {
    func routeToCryptoDetail(segue: UIStoryboardSegue?)
}

protocol CryptoDataPassing {
    var dataStore: CryptoDataStore? { get }
}

class CryptoRouter: NSObject, CryptoRoutingLogic, CryptoDataPassing {
    weak var viewController: CryptoViewController?
    var dataStore: CryptoDataStore?
    
    // MARK: Routing
    
    func routeToCryptoDetail(segue: UIStoryboardSegue?) {
      if let segue = segue {
        let destinationVC = segue.destination as! CryptoDetailViewController
        var destinationDS = destinationVC.router!.dataStore
        passDataToCryptoDetail(source: dataStore, destination: &destinationDS)
      } else {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let destinationVC = storyboard.instantiateViewController(withIdentifier: "CryptoDetailViewController") as! CryptoDetailViewController
        var destinationDS = destinationVC.router!.dataStore
        passDataToCryptoDetail(source: dataStore, destination: &destinationDS)
        navigateToCryptoDetail(source: viewController!, destination: destinationVC)
      }
    }
    
    // MARK: Navigation
    
    func navigateToCryptoDetail(source: CryptoViewController, destination: CryptoDetailViewController) {
      source.show(destination, sender: nil)
    }
    
    // MARK: Passing data
    
    func passDataToCryptoDetail(source: CryptoDataStore?, destination: inout CryptoDetailDataStore?) {
        if var safeDestination = destination, let safeSource = source {
            safeDestination.crypto = safeSource.crypto
        }
    }
}
