//
//  ListCustomersPresenter.swift
//  Relationship
//
//  Created by Raymond Law on 9/16/17.
//  Copyright (c) 2017 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

protocol ListCustomersPresentationLogic
{
  func presentFetchedCustomers(response: ListCustomers.FetchCustomers.Response)
  func presentCreatedCustomer(response: ListCustomers.CreateCustomer.Response)
  func presentDeletedCustomer(response: ListCustomers.DeleteCustomer.Response)
}

class ListCustomersPresenter: ListCustomersPresentationLogic
{
  weak var viewController: ListCustomersDisplayLogic?
  
  // MARK: Fetch customers
  
  func presentFetchedCustomers(response: ListCustomers.FetchCustomers.Response)
  {
    let displayedCustomers = response.customers.map { formatCustomer(customer: $0) }
    let viewModel = ListCustomers.FetchCustomers.ViewModel(displayedCustomers: displayedCustomers)
    viewController?.displayFetchedCustomers(viewModel: viewModel)
  }
  
  // MARK: Create customer
  
  func presentCreatedCustomer(response: ListCustomers.CreateCustomer.Response)
  {
    let displayedCustomers = response.customers.map { formatCustomer(customer: $0) }
    let viewModel = ListCustomers.CreateCustomer.ViewModel(displayedCustomers: displayedCustomers)
    viewController?.displayCreatedCustomer(viewModel: viewModel)
  }
  
  // MARK: Delete customer
  
  func presentDeletedCustomer(response: ListCustomers.DeleteCustomer.Response)
  {
    let displayedCustomers = response.customers.map { formatCustomer(customer: $0) }
    let viewModel = ListCustomers.DeleteCustomer.ViewModel(displayedCustomers: displayedCustomers)
    viewController?.displayDeletedCustomer(viewModel: viewModel)
  }
  
  // MARK: Format customer
  
  private func formatCustomer(customer: Customer) -> ListCustomers.DisplayedCustomer
  {
    return ListCustomers.DisplayedCustomer(name: customer.name)
  }
}
