package co.tiagoaguiar.course.instagram.profile.presenter

import co.tiagoaguiar.course.instagram.common.base.RequestCallback
import co.tiagoaguiar.course.instagram.common.model.Post
import co.tiagoaguiar.course.instagram.common.model.UserAuth
import co.tiagoaguiar.course.instagram.profile.Profile
import co.tiagoaguiar.course.instagram.profile.data.ProfileRepository
import java.util.*

class ProfilePresenter(
  private var view: Profile.View?,
  private val repository: ProfileRepository
) : Profile.Presenter {

  override fun clear() {
    repository.clearCache()
  }

  override fun fetchUserProfile(uuid: String?) {
    view?.showProgress(true)
    repository.fetchUserProfile(uuid, object : RequestCallback<UserAuth> {
      override fun onSuccess(data: UserAuth) {
        view?.displayUserProfile(data)
      }

      override fun onFailure(message: String) {
        view?.displayRequestFailure(message)
      }

      override fun onComplete() {
      }
    })
  }

  override fun fetchUserPosts(uuid: String?) {
    repository.fetchUserPosts(uuid, object : RequestCallback<List<Post>> {
      override fun onSuccess(data: List<Post>) {
        if (data.isEmpty()) {
          view?.displayEmptyPosts()
        } else {
          view?.displayFullPosts(data)
        }
      }

      override fun onFailure(message: String) {
        view?.displayRequestFailure(message)
      }

      override fun onComplete() {
        view?.showProgress(false)
      }
    })
  }

  override fun onDestroy() {
    view = null
  }

}