package com.yzx.ebrand.presenter

import com.blankj.utilcode.util.LogUtils
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.yzx.ebrand.config.Config
import com.yzx.ebrand.config.YzxInterface
import com.yzx.ebrand.model.*
import com.yzx.ebrand.presenter.base.BasePresenter
import com.yzx.ebrand.presenter.base.IView
import com.yzx.ebrand.presenter.base.JsonCallBack
import com.yzx.ebrand.presenter.base.YzxResponse

class VisitorPresenter(view: VisitorView) : BasePresenter<VisitorView>(view) {


    fun getList(pageNo: Int, keyword: String, status: String) {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_ACCESS_CONTROL_GET_FACE_LIST}"
        OkGo.post<YzxResponse<Visitor>>(url)
            .tag(this)
            .params("keyword", keyword)
            .params("audit_status", status)
            .params("user_type", 3)
            .params("page_size", 6)
            .params("page_no", pageNo)
            .execute(object : JsonCallBack<YzxResponse<Visitor>>() {


                override fun onSuccess(response: Response<YzxResponse<Visitor>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.onListSuccess(response.body().list, response.body().pager)
                    }
                }

                override fun onError(response: Response<YzxResponse<Visitor>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onListError(response?.body()?.msg ?: "")
                }
            })
    }
    fun getDetail(id: Int) {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_ACCESS_CONTROL_GET_FACE_INFO_DETAIL}"
        OkGo.post<YzxResponse<Visitor>>(url)
            .tag(this)
            .params("id", id)
            .params("user_type", 3)
            .execute(object : JsonCallBack<YzxResponse<Visitor>>() {


                override fun onSuccess(response: Response<YzxResponse<Visitor>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.onDetailSuccess(response.body().data)
                    }
                }

                override fun onError(response: Response<YzxResponse<Visitor>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onDetailError(response?.body()?.msg ?: "")
                }
            })
    }


    fun getQrcodeWithAccessToken(){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_GET_ACCESS_TOKEN}"
        OkGo.post<YzxResponse<AccessToken>>(url)
            .tag(this)
            .params("app_id", Config.MP_APPID)
            .execute(object : JsonCallBack<YzxResponse<AccessToken>>() {


                override fun onSuccess(response: Response<YzxResponse<AccessToken>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        getQrCode(response.body().data.access_token)
                    }
                }

                override fun onError(response: Response<YzxResponse<AccessToken>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onQrCodeError(response?.body()?.msg ?: "")
                }
            })
    }

    fun getQrCode(accessToken:String){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_GET_MINI_CODE}"
        val unitId = User.getUser().school_id
        OkGo.post<YzxResponse<QrCode>>(url)
            .tag(this)
            .params("page_url", "pages/accesscontrol/apply/index")
            .params("scene", "_${unitId}")
            .params("access_token", accessToken)
            .execute(object : JsonCallBack<YzxResponse<QrCode>>() {


                override fun onSuccess(response: Response<YzxResponse<QrCode>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.onQrCodeSuccess(response.body().data)
                    }
                }

                override fun onError(response: Response<YzxResponse<QrCode>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onQrCodeError(response?.body()?.msg ?: "")
                }
            })
    }

    

}

interface VisitorView : IView {
    fun onListSuccess(list: MutableList<Visitor>, pager: Pager)
    fun onListError(error: String)
    fun onDetailSuccess(detail: Visitor)
    fun onDetailError(error: String)
    fun onQrCodeSuccess(qrCode: QrCode)
    fun onQrCodeError(error: String)
}