package com.ym.modulecommon.utils

import com.umeng.analytics.MobclickAgent
import com.ym.modulecommon.AppliContext
import com.ym.modulecommon.net.EventApiClient
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 PhoneAttributeEventUtils {

    val MODEL_ATTRIBUTE = "attribute"
    val DEVICE_ENV = "device_env"
    val APPLIST = "applist"

    fun onEventAppList(value: String) {
        val jsonObj = JSONObject()
        jsonObj.put("event", APPLIST)
        jsonObj.put("action", APPLIST)
        jsonObj.put("data", value)
        apiClient(jsonObj)
        MobclickAgent.onEvent(AppliContext.get(), APPLIST, value)
    }

    fun onEventDevice(root: Int, hook: Int, emulator: Int, assist: Int) {
        val jsonObj = JSONObject()
        jsonObj.put("event", DEVICE_ENV)
        jsonObj.put("action", DEVICE_ENV)
        jsonObj.put("root", root)
        jsonObj.put("hook", hook)
        jsonObj.put("emulator", emulator)
        jsonObj.put("assist", assist)
        apiClient(jsonObj)
//        MobclickAgent.onEvent(AppliContext.get(), "device_env", value)
    }

    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(MODEL_ATTRIBUTE, 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) {}
        })
    }
}