package com.mints.goodmoney.mvp.presenters

import com.google.gson.JsonObject
import com.mints.goodmoney.manager.AppHttpManager
import com.mints.goodmoney.mvp.model.BaseResponse
import com.mints.goodmoney.mvp.views.BookView
import com.mints.library.net.neterror.BaseSubscriber
import com.mints.library.net.neterror.Throwable
import java.util.*

class BookPresenter : BasePresenter<BookView>() {

    fun addReadSeconds(seconds: Int) {
        val vo = HashMap<String, Any>()
        vo["seconds"] = seconds
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.addReadSeconds(vo),
                        object : BaseSubscriber<BaseResponse<JsonObject>>() {
                            override fun onCompleted() {
                            }

                            override fun onError(e: Throwable) {
                            }

                            override fun onNext(baseResponse: BaseResponse<JsonObject>) {
                                val code = baseResponse.getStatus()
                                val data = baseResponse.data
                                when (code) {
                                    200 -> {
                                        if (data != null) {
                                            val coin = data["coin"].asInt
                                            view.readSecondsSuc(coin)
                                        }
                                    }
                                    else -> view.showToast(baseResponse.getMessage())
                                }
                            }
                        })
    }

    fun getReadInfo() {
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.getReadInfo(),
                        object : BaseSubscriber<BaseResponse<JsonObject>>() {
                            override fun onCompleted() {
                            }

                            override fun onError(e: Throwable) {
                            }

                            override fun onNext(baseResponse: BaseResponse<JsonObject>) {
                                val code = baseResponse.getStatus()
                                val data = baseResponse.data
                                when (code) {
                                    200 -> {
                                        if (data != null) {
                                            val msg=data.getAsJsonObject("msg")
                                            view.readInfo(msg["needSecods"].asInt,msg["coin"].asInt,msg["completeSeconds"].asInt)
                                        }
                                    }
                                    else -> view.showToast(baseResponse.getMessage())
                                }
                            }
                        })
    }
}