package com.yzx.escreen.presenter

import com.blankj.utilcode.util.LogUtils
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.yzx.escreen.config.Config
import com.yzx.escreen.config.YzxInterface
import com.yzx.escreen.model.Device
import com.yzx.escreen.model.Pager
import com.yzx.escreen.model.TeacherInfo
import com.yzx.escreen.model.User
import com.yzx.escreen.presenter.base.BasePresenter
import com.yzx.escreen.presenter.base.IView
import com.yzx.escreen.presenter.base.JsonCallBack
import com.yzx.escreen.presenter.base.YzxResponse

class SettingPresenter(view: SettingView) : BasePresenter<SettingView>(view) {


    fun getTeacherInfo(user_id: Int) {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_GET_TEACHER_INFO}"
        OkGo.post<YzxResponse<TeacherInfo>>(url)
            .tag(this)
            .params("user_id", user_id)
            .execute(object : JsonCallBack<YzxResponse<TeacherInfo>>() {

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

                override fun onError(response: Response<YzxResponse<TeacherInfo>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onUserInfoError(response?.body()?.msg ?: "")
                }
            })
    }
    fun getDeviceList(pageNo: Int) {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_ACCESS_CONTROL_GET_FACE_DEVICE_LIST}"
        OkGo.post<YzxResponse<Device>>(url)
            .tag(this)
            .params("page_size", 10)
            .params("page_no", pageNo)
            .execute(object : JsonCallBack<YzxResponse<Device>>() {


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

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


    fun setDoorRelayMode(deviceIdList: MutableList<Int>,mode:Int = 0) {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_ACCESS_CONTROL_POST_DOOR_RELAY_MODE}"
        OkGo.post<YzxResponse<*>>(url)
            .tag(this)
            .params("device_ids", deviceIdList.joinToString(","))
            .params("mode", mode)
            .execute(object : JsonCallBack<YzxResponse<*>>() {

                override fun onSuccess(response: Response<YzxResponse<*>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.onSetSuccess()
                    }
                }

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




}

interface SettingView : IView {
    fun onSetSuccess()
    fun onSetError(error:String)

    fun onDeviceListSuccess(list:MutableList<Device>, pager: Pager)
    fun onDeviceListError(error:String)
    fun onUserInfoSuccess(info: TeacherInfo)
    fun onUserInfoError(error:String)
}