package cn.muchinfo.rma.netManage.utils

import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.netManage.base.ResponseCallback
import com.blankj.utilcode.util.LogUtils
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.StringCallback

import okhttp3.MediaType
import org.json.JSONObject

class MyOkHttpUtils {


    /**
     * 通用查询的方法
     * @param URL String
     * @param params Map<String, Any>
     * @param statementid String
     * @param callback ResponseCallback<*>
     */
    fun queryCommon(URL: String,params: Map<String, String>,statementid : String,callback: StringCallback){
        if (!(URL.contains("http") || URL.contains("https"))) { // Expected URL scheme 'http' or 'https' but no colon was found
            LogUtils.e("Expected URL scheme 'http' or 'https' but no colon was found")
            return
        }
        val json = JSONObject()
        params.forEach {
            json.put(it.key,it.value)
        }
        OkHttpUtils.get()
            .url(URL)
            .addHeader("Content-Type", "application/json")
            .addParams("statementid",statementid)
            .addParams("data",json.toString())
            .build()
            .execute(callback)
    }

    /**
     * url 接口地址
     * callback 接口回调
     * type 接口传参类型（1：get 2：post 参数 3：json）
     * params 参数
     */
    fun query(
        URL: String,
        params: Map<String, String>,
        type: String,
        callback: ResponseCallback<*>
    ) {
        if (!(URL.contains("http") || URL.contains("https"))) { // Expected URL scheme 'http' or 'https' but no colon was found
            LogUtils.e("Expected URL scheme 'http' or 'https' but no colon was found")
            return
        }
        when (type) {
            "1" -> {
                queryGet(URL = URL, params = params, callback = callback)
            }
            "2" -> {
                queryPost(URL = URL, params = params, callback = callback)
            }
            "3" -> {
                queryPostJson(URL = URL, params = params, callback = callback)
            }
        }
    }


    /**
     * get
     * @param URL String 请求地址
     * @param callback ResponseCallback<*> 回调
     * @param params Map<String, Any> 参数
     */
    private fun queryGet(URL: String, callback: ResponseCallback<*>, params: Map<String, String>) {
        if (!(URL.contains("http") || URL.contains("https"))) { // Expected URL scheme 'http' or 'https' but no colon was found
            LogUtils.e("Expected URL scheme 'http' or 'https' but no colon was found")
            return
        }
        OkHttpUtils.get()
            .url(URL)
            .params(params)
            .addHeader("Authorization", GlobalDataCollection.instance?.loginRsp?.token)
            .build()
            .execute(callback)
    }

    /**
     * get 用于基础服务地址查询
     * @param URL String 请求地址
     * @param callback ResponseCallback<*> 回调
     * @param params Map<String, Any> 参数
     */
    fun queryGetForUrl(URL: String, callback: ResponseCallback<*>, params: Map<String, String>) {
        if (!(URL.contains("http") || URL.contains("https"))) { // Expected URL scheme 'http' or 'https' but no colon was found
            LogUtils.e("Expected URL scheme 'http' or 'https' but no colon was found")
            return
        }
        OkHttpUtils.get()
            .url(URL)
            .params(params)
            .build()
            .execute(callback)
    }

    /**
     *
     * @param URL String 请求地址
     * @param callback ResponseCallback<*> 回调
     * @param params Map<String, String> 参数
     */
    private fun queryPost(URL: String, callback: ResponseCallback<*>, params: Map<String, String>) {
        if (!(URL.contains("http") || URL.contains("https"))) { // Expected URL scheme 'http' or 'https' but no colon was found
            LogUtils.e("Expected URL scheme 'http' or 'https' but no colon was found")
            return
        }
        OkHttpUtils.post()
            .url(URL)
            .params(params)
            .addHeader("Authorization", GlobalDataCollection.instance?.loginRsp?.token)
            .build()
            .execute(callback)
    }


    /**
     * JsonBody提交方法
     * @param URL String 请求地址
     * @param callback ResponseCallback<*> 回调
     * @param params Map<String, String> 参数
     */
    fun queryPostJson(
        URL: String,
        callback: ResponseCallback<*>,
        params: Map<String, Any>
    ) {
        if (!(URL.contains("http") || URL.contains("https"))) { // Expected URL scheme 'http' or 'https' but no colon was found
            LogUtils.e("Expected URL scheme 'http' or 'https' but no colon was found")
            return
        }
        val json = JSONObject()
        params.forEach {
            json.put(it.key, it.value)
        }
        OkHttpUtils.postString()
            .url(URL)
            .content(json.toString())
            .mediaType(MediaType.parse("application/json; charset=utf-8"))
            .addHeader("Authorization", GlobalDataCollection.instance?.loginRsp?.token)
            .build()
            .execute(callback)
    }
}