package cn.muchinfo.rma.business.customer

import cn.muchinfo.rma.business.contract.adapter.ContractAdapter
import cn.muchinfo.rma.business.customer.adapter.CustomerAdapter
import cn.muchinfo.rma.global.data.UserInfoData
import cn.muchinfo.rma.netManage.base.ResponseCallback
import cn.muchinfo.rma.netManage.utils.MyOkHttpUtils
import cn.muchinfo.rma.netcore.packet.Packet50
import cn.muchinfo.rma.netcore.socket.Callback
import cn.muchinfo.rma.protobuf.funcode.FunCode
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.autoWidget.guard
import cn.muchinfo.rma.view.base.app.BaseResult
import cn.muchinfo.rma.view.base.app.Constant
import com.blankj.utilcode.util.SPUtils
import okhttp3.Call
import java.lang.Exception

/**
 * 客户资料管理类
 */
class CustomerManage {

    /**
     * 查询客户资料列表
     * @param params Map<String, String> 请求参数
     * @param responseBack Function3<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] List<UserInfoData>?, [@kotlin.ParameterName] Error?, Unit>
     */
    fun queryUserInfo(
        params: Map<String, String>,
        responseBack: (isSuccess: Boolean, respData: List<UserInfoData>?, error: Error?) -> Unit
    ) {
        MyOkHttpUtils().query(
            URL = SPUtils.getInstance()
                .getString(Constant.goCommonSearchUrl) + "/Ermcp/QueryUserInfo",
            params = params,
            type = "1",
            callback = object : ResponseCallback<BaseResult<List<UserInfoData>>>() {
                override fun onResponse(response: BaseResult<List<UserInfoData>>?, id: Int) {
                    responseBack(true, response?.data, null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    responseBack(false, null, Error(e?.message))
                }
            })
    }

    /**
     * 新增客户申请
     * @param params Map<String, String> 请求参数
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     * @see /Erms3/AddUserInfoApply 新增客户申请
     */
    fun addUserInfoApply(
        params: Map<String, Any>,
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        MyOkHttpUtils().queryPostJson(
            URL = SPUtils.getInstance()
                .getString(Constant.goCommonSearchUrl) + "/Erms3/AddUserInfoApply",
            params = params,
            callback = object : ResponseCallback<BaseResult<String>>() {
                override fun onResponse(response: BaseResult<String>?, id: Int) {
                    callback(true, null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    callback(false, Error(e?.message))
                }
            })
    }

    /**
     * 修改客户申请
     * @param params Map<String, String> 请求参数
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     * @see /Erms3/ModifyUserInfoApply 修改客户申请
     */
    fun modifyUserInfoApply(
        params: Map<String, Any>,
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        MyOkHttpUtils().queryPostJson(
            URL = SPUtils.getInstance()
                .getString(Constant.goCommonSearchUrl) + "/Erms3/ModifyUserInfoApply",
            params = params,
            callback = object : ResponseCallback<BaseResult<String>>() {
                override fun onResponse(response: BaseResult<String>?, id: Int) {
                    callback(true, null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    callback(false, Error(e?.message))
                }
            })
    }

    /**
     * 更新用户状态
     * @param userID Int 目标用户ID
     * @param accountStatus Int 账户状态 - 4:正常 6:注销（停用）
     * @param callback Function2<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] Error?, Unit>
     */
    fun updateUserAccountStatus(
        userID: Int,
        accountStatus: Int,
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ) {
        MyOkHttpUtils().query(
            URL = SPUtils.getInstance()
                .getString(Constant.goCommonSearchUrl) + "/User/UpdateUserAccountStatus",
            params = mutableMapOf<String, String>().apply {
                put("userID", userID.toString())
                put("accountStatus", accountStatus.toString())
            },
            type = "2",
            callback = object : ResponseCallback<BaseResult<String>>() {
                override fun onResponse(response: BaseResult<String>?, id: Int) {
                    callback(true, null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    callback(false, Error(e?.message))
                }
            })
    }

    /**
     * 客户信息审核请求
     * @param auditflag Long 1审核通过2审核拒绝
     * @param userid Long 客户userid
     * @param audituserid Long 审核操作人userid
     * @param remark String 备注
     * @return Packet50
     */
    fun requestUserInfoCheckMange(
        auditflag : Long,
        userid : Long,
        audituserid : Long,
        remark : String = "",
        callback: (isCompleted: Boolean, err: Error?) -> Unit
    ){
        val tradeSocketManager = MyApplication.getInstance()?.tradeSocketManager.guard {
            callback(false, Error("交易链路未初始化"))
            return
        }

        val reqPacket = CustomerAdapter.getCustomerDataReqInfo(
            auditflag,userid,audituserid,remark
        )

        tradeSocketManager.send(
            reqPacket,
            FunCode.UserInfoCheckMangeRsp,
            object : Callback<Packet50> {
                override fun onSuccess(rsp: Packet50?) {
                    val rst = ContractAdapter.analysisPerationContractRsq(rsp!!)
                    callback(rst.first, rst.second)
                }

                override fun onFail(err: Error?) {
                    // 发送数据失败
                    callback(false, err)
                }

            }
        )

    }
}