package com.zxhl.cms.ad.upload.Utils

import android.util.Log
import com.zxhl.cms.ad.upload.model.NewsEntity
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.callback.BaseAdOberver
//import com.zxhl.cms.net.SettingPreference
//import com.zxhl.cms.net.callback.BaseAdOberver
//import com.zxhl.cms.net.callback.BaseObserver
//import com.zxhl.cms.utils.EventUtils
import com.zxhl.cms.utils.PhoneUtils
import io.reactivex.schedulers.Schedulers
import org.jetbrains.anko.doAsync
import java.lang.StringBuilder
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

/**
 * Created by gaoleichao on 2018/7/12.
 */
object AdUpload {

    var time: Long = 0
   


    fun onAdImpression(entity: NewsEntity?) {
        if (entity == null) {
            return
        }
        doAsync {
            if (!entity?.isShow) {
                entity?.isShow = true
                if (entity?.imp_tracking != null && entity?.imp_tracking?.size ?: 0 > 0) {
                    for (i in 0 until entity?.imp_tracking?.size!!) {
                        Log.d("onAdImpression", "slotName:" + entity?.slot_id)
                        var newUrl: String = PhoneUtils.replaceUrl(entity?.imp_tracking!![i], entity)
                        apiClient(newUrl)
                    }
                }
            }
        }
    }


    fun onAdClick(newsEntity: NewsEntity?) {
        if (newsEntity == null) {
            return
        }
        doAsync {
            if (!newsEntity.isClick) {
                newsEntity.isClick = true
                if (newsEntity?.clk_tracking != null && newsEntity?.clk_tracking?.size ?: 0 > 0) {
                    for (i in 0 until newsEntity?.clk_tracking?.size!!) {
                        var newUrl: String = PhoneUtils.replaceUrl(newsEntity?.clk_tracking!![i], newsEntity)
                        Log.d("glc", newUrl)
                        apiClient(newUrl)
                    }
                }
            }
        }
    }

    fun onAdtrack(data: List<String>?) {
        if (data == null) {
            return
        }
        val list: MutableList<String> = ArrayList()
        list.addAll(data)
        list?.forEach {
            Log.d("InstalledReceiver", "onAdtrack")
            apiClient(it)
        }
    }
    private fun apiClient(url: String) {
        ApiClient.adApi.showAd(url).subscribeOn(Schedulers.io()).subscribe(object : BaseAdOberver<Any>() {
            override fun onSuccess(result: Any) {

            }

            override fun onFailure(e: Throwable, code: String, errorMsg: String) {
//                entity?.trackFailIndex.add(index)
//                if (errorMsg?.contains("请求被重定向到其他页面") == false) {
//                    EventUtils.onAdTrackiError(entity?.slot_id
//                            ?: "", entity?.sid.toString(), entity?.type.toString(), clickOrShow, url, entity?.reqId
//                            ?: "")
//                }
            }

        })
    }

}





fun getUrl(feed: String?, map: MutableMap<String, String>?, tab_id: String): String {
    var random1: String = "13149876"
    var random2: String = "98761314"

    var mStringBuilder: StringBuilder = StringBuilder();//拼接m报文
    mStringBuilder.append("${random1}")

    var requestBuilder: StringBuilder = StringBuilder();//拼接请求地址
    requestBuilder.append(feed)
    requestBuilder.append("?")
    for ((k, v) in map!!) {//替换参数信息
        if (v.contains("__TAB_ID__")) {
            map!![k] = v.replace("__TAB_ID__", tab_id)
        } else if (v.contains("__UTC_TS__")) {
            map!![k] = v.replace("__UTC_TS__", System.currentTimeMillis().toString())
        }
    }

    var i: Int = 1
    for ((k, v) in map!!) {
        if (i == 1) {
            requestBuilder.append(k + "=" + v)
        } else {
            requestBuilder.append("&" + k + "=" + v)
        }
        mStringBuilder.append(k + "=" + v)
        i++
    }
    mStringBuilder.append("${random2}")

//        var mMd5 = Md5Util.encode(mStringBuilder.toString())

//        requestBuilder.append("&m=" + mMd5)

    return requestBuilder.toString()
}


