package com.bc_chat.contacts.presenter

import com.bc_chat.contacts.contract.PhoneAddFriendContract
import com.bc_chat.contacts.task.PhoneSearchFriendTask
import com.google.gson.JsonObject
import com.zhaohaoting.framework.requset.NetWorkCallBack
import java.lang.Exception

/**
 * author：Created by zhaoaoting on 2019/3/6
 * email：526309416@qq.com
 * desc：电话号码搜索好友
 */
class PhoneAddFriendPresenter(view: PhoneAddFriendContract.View) : AddFriendPresenter<PhoneAddFriendContract.View>(view), PhoneAddFriendContract.Presenter {

    override fun searchPhone(phone: String) {

        taskHelper.execute<Any>(PhoneSearchFriendTask(phone), object : NetWorkCallBack<JsonObject>() {

            override fun onPreExecute(task: Any?) {
                super.onPreExecute(task)
                view.showLoading(task)
            }

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

            override fun onPostExecuteSuccess(task: Any?, data: JsonObject?) {
                view.dismissLoading(task)
                if (data == null) {
                    view.searchNotMember()
                } else {
                    if (data.has("uid")) {
                        val uid = data.get("uid").asString
                        view.searchSuccess(uid)
                    } else {
                        view.searchNotMember()
                    }
                }
            }
        })
    }
}
