package com.ym.modulecommon.utils

import com.umeng.analytics.MobclickAgent
import com.ym.modulecommon.AppliContext
import com.ym.modulecommon.net.EventApiClient
import com.ym.modulecommon.net.ZXADClient
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

/**
 * Created by gaoleichao on 2019/1/11
 */
object BaseEventUtils {

    val EVENT = "guazinovel"

    fun onEvent(action: String, value: String) {
        val jsonObj = getJSON(action)
        jsonObj.put("value", value)
        apiClient(jsonObj)
        MobclickAgent.onEvent(AppliContext.get(), action, value)
    }

    fun getJSON(action: String): JSONObject {
        val data = JSONObject()
        data.put("event", EVENT)
        data.put("action", action)
        return data
    }

    private fun apiClient(data: JSONObject) {
        val str = data.toString()
        LogUtils.d("EventUtils", str)
        val key = AESUtils.encrypt(str)
        LogUtils.d("EventUtils", key)
        EventApiClient.adApi.requestEvent("behavior", key).subscribeOn(Schedulers.io())
            .subscribe(object : Observer<Any> {
                override fun onComplete() {

                }

                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: Any) {
                }

                override fun onError(e: Throwable) {
                }
            })
    }
}