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.CBClassInfo
import com.yzx.ebrand.model.ClassActiveInfo
import com.yzx.ebrand.model.ClassStylePic
import com.yzx.ebrand.model.ClassTeaInfo
import com.yzx.ebrand.model.HeadClassInfo
import com.yzx.ebrand.model.TimeTable
import com.yzx.ebrand.model.User
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 ClassBrandPresenter(view: ClassBrandView) : BasePresenter<ClassBrandView>(view) {
    fun getHeadClassList(){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_GET_VOTE_TEA_CLASS}"
        OkGo.post<YzxResponse<HeadClassInfo>>(url)
            .tag(this)
            .execute(object : JsonCallBack<YzxResponse<HeadClassInfo>>() {

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

                override fun onError(response: Response<YzxResponse<HeadClassInfo>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                }
            })
    }
    fun getClassInfo(class_id:String){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_CLASS_BRAND_CLASS_INFO}"
        OkGo.post<YzxResponse<CBClassInfo>>(url)
            .tag(this)
            .params("class_id", class_id)
            .execute(object : JsonCallBack<YzxResponse<CBClassInfo>>() {

                override fun onSuccess(response: Response<YzxResponse<CBClassInfo>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        val info = response.body().data
                        mView?.onAvatarSuccess(info.user_photo.identity,info.user_photo.head_portrait)
                        mView?.onClassInfoSuccess(response.body().data)
                    }
                }

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

    fun getTeaList(class_id: String){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_CLASS_BRAND_TEA_INFO}"
        OkGo.post<YzxResponse<ClassTeaInfo>>(url)
            .tag(this)
            .params("class_id", class_id)
            .execute(object : JsonCallBack<YzxResponse<ClassTeaInfo>>() {

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

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

    fun getTimeTable(class_id: String){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_CLASS_BRAND_TIME_TABLE_INFO}"
        OkGo.post<YzxResponse<TimeTable>>(url)
            .tag(this)
            .params("class_id", class_id)
            .execute(object : JsonCallBack<YzxResponse<TimeTable>>() {

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

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

    fun getActiveInfo(class_id:String){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_CLASS_BRAND_ACTIVE_INFO}"
        OkGo.post<YzxResponse<ClassActiveInfo>>(url)
            .tag(this)
            .params("class_id", class_id)
            .execute(object : JsonCallBack<YzxResponse<ClassActiveInfo>>() {

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

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

    fun getImageInfo(class_id: String){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_CLASS_BRAND_IMAGE_INFO}"
        OkGo.post<YzxResponse<ClassStylePic>>(url)
            .tag(this)
            .params("class_id", class_id)
            .execute(object : JsonCallBack<YzxResponse<ClassStylePic>>() {

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

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

interface  ClassBrandView : IView {
    fun onHeadClassSuccess(list:MutableList<HeadClassInfo>)
    fun onAvatarSuccess(identity:String,head_portrait:String)
    fun onClassInfoSuccess(info: CBClassInfo)
    fun onTeaListSuccess(list:MutableList<ClassTeaInfo>)
    fun onTimeTableSuccess(list:MutableList<TimeTable>)
    fun onActiveInfoSuccess(list:MutableList<ClassActiveInfo>)
    fun onImageInfoSuccess(list: MutableList<ClassStylePic>)
}