package com.bc_chat.account.presenter

import android.arch.lifecycle.LifecycleOwner
import com.bc_chat.account.contract.VerifySmsContract
import com.bc_chat.account.task.VerVerfiyCodeTask
import com.bc_chat.bc_base.presenter.WebPresenter
import com.google.gson.JsonObject
import com.zhaohaoting.framework.requset.NetWorkCallBack
import java.lang.Exception

/**
 * author：Created by zhaohaoting on 2019/3/5
 * email：526309416@qq.com
 * desc：验证验证码
 */
open class VerifySmsCodePresenter<VIEW : VerifySmsContract.View> constructor(view: VIEW) : SendCodePresenter<VIEW>(view), VerifySmsContract.Presenter {

    override fun loadWebPage(type: Int) {
        webPresenter?.loadWebPage(type)
    }

    private var webPresenter: WebPresenter<VerifySmsContract.View>? = null

    init {
        webPresenter = WebPresenter(view)
    }

    override fun onCreate(owner: LifecycleOwner?) {
        super.onCreate(owner)
        owner?.lifecycle?.addObserver(webPresenter!!)
    }

    override fun verifySmsCode(phone: String?, smsCode: String?, type: Int) {
        taskHelper.execute<Any>(VerVerfiyCodeTask(phone, smsCode, type), object : NetWorkCallBack<JsonObject>() {
            override fun onPreExecute(task: Any?) {
                view.showLoading(task)
            }

            override fun onPostExecuteSuccess(task: Any?, data: JsonObject?) {
                view.verifySuccess()
                view.dismissLoading(task)
            }

            override fun onPostExecuteFailure(task: Any?, exception: Exception?) {
                super.onPostExecuteFailure(task, exception)
                view.verifyFailure(exception)
                view.dismissLoading(task)
            }
        })
    }
}