package com.duben.microtribe.mvp.presenters

import com.google.gson.JsonObject
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import com.duben.microtribe.manager.AppHttpManager
import com.duben.microtribe.mvp.model.BaseResponse
import com.duben.microtribe.mvp.views.FeedbackView
import java.util.*

class FeedbackPresenter : BasePresenter<FeedbackView>() {

    fun feedback(type: String, content: String) {
        view.showLoading("")
        val vo = HashMap<String, Any>()
        vo["type"] = type
        vo["content"] = content
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.feedback(vo),
                object : BaseSubscriber<BaseResponse<JsonObject>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                        view.hideLoading()
                    }

                    override fun onError(e: Throwable) {
                        if (isLinkView) return
                        view.hideLoading()
                        view.showToast(e.message)
                    }

                    override fun onNext(baseResponse: BaseResponse<JsonObject>) {
                        if (isLinkView) return

                        view.hideLoading()
                        val code = baseResponse.status
                        val message = baseResponse.message

                        when (code) {
                            200 -> view.feedbackSuc()
                            else -> view.showToast(message)
                        }
                    }
                })
    }
}