//
//  Copyright (c) 2017. Uber Technologies
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import RIBs

protocol LoggedInDependency: Dependency {
  var loggedInViewController: LoggedInViewControllable { get }
}

final class LoggedInComponent: Component<LoggedInDependency> {
  
  let player1Name: String
  let player2Name: String
  
  var mutableScoreStream: MutableScoreStream {
    return shared { ScoreStreamImpl() }
  }
  
  fileprivate var loggedInViewController: LoggedInViewControllable {
    return dependency.loggedInViewController
  }
  
  init(
    dependency: LoggedInDependency,
    player1Name: String,
    player2Name: String
  ) {
    self.player1Name = player1Name
    self.player2Name = player2Name
    super.init(dependency: dependency)
  }
}

// MARK: - Builder

protocol LoggedInBuildable: Buildable {
  func build(
    withListener listener: LoggedInListener,
    player1Name: String,
    player2Name: String
  ) -> LoggedInRouting
}

final class LoggedInBuilder: Builder<LoggedInDependency>, LoggedInBuildable {
  
  override init(dependency: LoggedInDependency) {
    super.init(dependency: dependency)
  }
  
  func build(
    withListener listener: LoggedInListener,
    player1Name: String,
    player2Name: String
  ) -> LoggedInRouting {
    let component = LoggedInComponent(
      dependency: dependency,
      player1Name: player1Name,
      player2Name: player2Name)
    
    let interactor = LoggedInInteractor(mutableScoreStream: component.mutableScoreStream)
    
    interactor.listener = listener
    
    let offGameBuilder = OffGameBuilder(dependency: component)
    let ticTacToeBuilder = TicTacToeBuilder(dependency: component)
    return LoggedInRouter(interactor: interactor,
                          viewController: component.loggedInViewController,
                          offGameBuilder: offGameBuilder,
                          ticTacToeBuilder: ticTacToeBuilder)
  }
}
