package br.com.edsilfer.android.presence_control.place.presentation.presenter

import br.com.edsilfer.android.presence_control.core.presentation.BaseView
import br.com.edsilfer.android.presence_control.place.domain.observables.ObservableNewPlace
import br.com.edsilfer.android.presence_control.place.presentation.presenter.contract.IPlaceSummaryPresenter
import br.com.edsilfer.android.presence_control.place.presentation.view.contracts.IPlaceSummaryView
import io.reactivex.disposables.Disposable

/**
 * Created by ferna on 5/31/2017.
 */
class PlaceSummaryPresenter : IPlaceSummaryPresenter {

    private lateinit var view: IPlaceSummaryView
    private var placeObserver: Disposable? = null

    /**
     * LIFECYCLE
     */
    override fun takeView(view: BaseView) {
        placeObserver = onPlaceUpdated()
        this.view = view as IPlaceSummaryView
    }

    override fun dropView() {
        placeObserver?.dispose()
        super.dropView()
    }

    /**
     * LISTENERS
     */
    private fun onPlaceUpdated(): Disposable? {
        return ObservableNewPlace.register().subscribe { place -> view.update(place) }
    }
}