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.BaseUser
import com.yzx.ebrand.model.User
import com.yzx.ebrand.model.WxQrTicket
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 LoginPresenter(view: LoginView) : BasePresenter<LoginView>(view) {
    fun login(phone: String, psw: String) {
        val url = Config.BASE_URL + YzxInterface.INTERFACE_LOGIN_ALL
        OkGo.post<YzxResponse<BaseUser>>(url)
            .tag(this)
            .params("phone", phone)
            .params("password", psw)
            .params("original_password", psw)
            .execute(object : JsonCallBack<YzxResponse<BaseUser>>() {
                override fun onSuccess(response: Response<YzxResponse<BaseUser>>?) {
                    if (response?.isSuccessful == true) {
                        mView?.onLoginSuccess(response.body().data, response.body().list)
                        LogUtils.d("login", response.body().list)
                    }
                }

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

    fun switchUser(token: String, user_id: Int, unit_id: Int) {
        val url = Config.BASE_URL + YzxInterface.INTERFACE_USER_SWITCH
        OkGo.post<YzxResponse<User>>(url)
            .tag(this)
            .params("token", token)
            .params("user_id", user_id)
            .params("unit_id", unit_id)
            .params("type", 0)
            .execute(object : JsonCallBack<YzxResponse<User>>() {
                override fun onSuccess(response: Response<YzxResponse<User>>?) {
                    if (response?.isSuccessful == true) {
                        response.body().data.save()
                        getUserLoginInfo(response.body().data.token)
                    }
                }

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

    fun switchUnit(user_id: Int, school_id: Int, tmp_token: String) {
        val url = Config.BASE_URL + YzxInterface.INTERFACE_USER_SWITCH_UNIT
        OkGo.post<YzxResponse<User>>(url)
            .tag(this)
            .params("school_id", school_id)
            .params("user_id", user_id)
            .params("tmp_token", tmp_token)
            .params("client_id", 201)
            .params("type", 0)
            .execute(object : JsonCallBack<YzxResponse<User>>() {
                override fun onSuccess(response: Response<YzxResponse<User>>?) {
                    if (response?.isSuccessful == true) {
                        getUserLoginInfo(response.body().data.token)
                    }
                }

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

    fun getUserLoginInfo(token: String) {
        val url = Config.BASE_URL + YzxInterface.INTERFACE_COMMON_LOGIN_BY_TOKEN
        OkGo.post<YzxResponse<User>>(url)
            .tag(this)
            .params("token", token)
            .execute(object : JsonCallBack<YzxResponse<User>>() {
                override fun onSuccess(response: Response<YzxResponse<User>>?) {
                    if (response?.isSuccessful == true) {
                        response.body().data.save()
                        mView?.onUserInfoSuccess(response.body().data)
                    }
                }

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

    fun getUserRoleInfoByPhone(phone: String, temp_token: String = "") {
        val url = Config.BASE_URL + YzxInterface.INTERFACE_USER_ROLE_LIST_BY_PHONE
        OkGo.post<YzxResponse<BaseUser>>(url)
            .tag(this)
            .params("phone", phone)
            .execute(object : JsonCallBack<YzxResponse<BaseUser>>() {
                override fun onSuccess(response: Response<YzxResponse<BaseUser>>?) {
                    if (response?.isSuccessful == true) {
                        val teaList = response.body().list.filter { return@filter it.role.toInt() == 0 }
                            .toMutableList()
                        teaList.forEach {
                            it.unit_name = it.school_name
                            it.unit_id = it.school_id
                        }
                        mView?.onUserListSuccess(teaList, temp_token)
                    }
                }

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

    fun getUserRoleList(token: String) {
        val url = Config.BASE_URL + YzxInterface.INTERFACE_USER_ROLE_LIST
        OkGo.post<YzxResponse<BaseUser>>(url)
            .tag(this)
            .params("token", token)
            .execute(object : JsonCallBack<YzxResponse<BaseUser>>() {
                override fun onSuccess(response: Response<YzxResponse<BaseUser>>?) {
                    if (response?.isSuccessful == true) {
                        val teaList = response.body().list.filter { return@filter it.type == 0 }
                            .toMutableList()
                        mView?.onUserListSuccess(teaList, token)
                    }
                }

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

    fun getWXQrCode() {
        val url = Config.BASE_URL + YzxInterface.INTERFACE_GENERATE_WX_QR_TICKET
        OkGo.post<YzxResponse<WxQrTicket>>(url)
            .tag(this)
            .params("mp_id", 4)
            .execute(object : JsonCallBack<YzxResponse<WxQrTicket>>() {
                override fun onSuccess(response: Response<YzxResponse<WxQrTicket>>?) {
                    if (response?.isSuccessful == true) {
                        val url =
                            "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${response.body().data.ticket}"
                        mView?.onWxQrCodeSuccess(url, response.body().data.ticket)
                    }
                }

                override fun onError(response: Response<YzxResponse<WxQrTicket>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                }
            })
    }

    fun loginQuery(ticket: String) {
        val url = Config.BASE_URL + YzxInterface.INTERFACE_WX_DO_LOGIN_QUERY

        OkGo.post<YzxResponse<BaseUser>>(url)
            .tag(this)
            .params("ticket", ticket)
            .execute(object : JsonCallBack<YzxResponse<BaseUser>>() {
                override fun onSuccess(response: Response<YzxResponse<BaseUser>>?) {
                    if (response?.isSuccessful == true) {
                        val res = response.body()
                        mView?.onLoginQuerySuccess(res.data, res.code)
                    }
                }

                override fun onError(response: Response<YzxResponse<BaseUser>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                }
            })
    }
}

interface LoginView : IView {
    fun onLoginSuccess(user: BaseUser, list: MutableList<BaseUser>)
    fun onLoginError(msg: String)
    fun onUserInfoSuccess(user: User)
    fun onUserListSuccess(list: MutableList<BaseUser>, token: String)
    fun onWxQrCodeSuccess(imgUrl: String, ticket: String)
    fun onLoginQuerySuccess(user: BaseUser, code: Int)
}