package com.zxhl.cms.utils

import android.content.Context
import android.text.TextUtils
import android.util.Log
import com.umeng.analytics.MobclickAgent
import com.zxhl.cms.AppContext
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.EventApiClient
import com.zxhl.cms.utils.LogUtils
import com.zxhl.cms.utils.Utils
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.Headers
import org.jetbrains.anko.doAsync
import org.json.JSONObject
import java.util.*

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


    val EVENT = "guazinovel"
    val MODEL_BEHAVIOR = "behavior"
    val EVENT_APP_LIST = "applist"

    fun onAdClick(action: String?) {
        if (TextUtils.isEmpty(action))
            return
        val jsonObj = getJSON(action!!)
        apiClient(jsonObj)
//        MobclickAgent.onEvent(AppContext.get(), action, jsonObj.toString())
    }

    fun onEvent(action: String) {
        val jsonObj = getJSON(action)
        apiClient(jsonObj)
//        MobclickAgent.onEvent(AppContext.get(), action)
    }


    fun onUserShowEvent(action: String,id:String, requestId: String) {
        val json = Utils.obj2Str(UserShow(action,requestId))
        val jsonObj = getJSON(action)
        jsonObj.put("value", json)
        apiClient(jsonObj)
//        MobclickAgent.onEvent(AppContext.get(), action)
    }

    class UserShow {
        var id: String = ""
        var requestId: String = ""

        constructor(id: String, requestId: String) {
            this.id = id
            this.requestId = requestId
        }
    }

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


    //    /**
//     * 统计新用户首次登陆  男女生
//     */
//    fun genderClickJson(gender: String): JSONObject {
//        val jsonObj = JSONObject()
//        jsonObj.put("gender", gender)
//        return jsonObj
//    }
    private var time: Long = 0


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

    private fun apiClient(data: JSONObject) {
        val str = data.toString()
        Log.d("EventUtils", str)
        val key = AESUtils.encrypt(str)
        Log.d("EventUtils", key)
        EventApiClient.cfgApi.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) {
                }
            })
    }

    fun apiAppUpload(module: String, event: String, data: JSONObject) {
        data.put("event", event)
        val str = data.toString()
        LogUtils.d("EventUtils", str)
        val key = AESUtils.encrypt(str)
        LogUtils.d("EventUtils", key)
        EventApiClient.cfgApi.requestEvent(module, 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) {
                }
            })
    }

}