package com.yidianling.ydlcommon.custom.http

import com.google.gson.Gson
import com.yidianling.ydlcommon.core.base.BaseApplication
import com.yidianling.common.tools.LogUtil
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxNetTool
import com.yidianling.ydlcommon.custom.bean.WebViewTimeBean
import com.yidianling.ydlcommon.custom.data.YdlDataManager
import com.yidianling.ydlcommon.custom.http.api.ApiRequestUtil
import com.yidianling.ydlcommon.custom.router.YdlCommonRouterManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author jiucheng
 * @描述:上传webview的初始化时间、加载url时间
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/10/8
 */
class UploadWebViewTimeUtils {

    companion object {

        /**
         * 上传webview初始化时间及加载url时间
         * @param url 加载的url
         * @param initTime  webview初始化时间
         * @param loadTime  url加载时间
         * @param netSpeed  网速
         */
        fun uploadWebViewTime(url: String, initTime: String, loadTime: String, netSpeed: String) {
            val userInfo = YdlCommonRouterManager.getYdlCommonRoute()?.getUserInfo()
            var uid = ""
            if (userInfo != null) {
                uid = userInfo.userId
            }
            var bean = WebViewTimeBean(
                url,
                initTime,
                loadTime,
                YdlDataManager.getRam().getChannelName(),
                RxDeviceTool.getBuildMANUFACTURER() + "," + RxDeviceTool.getBuildBrandModel() + "," + RxDeviceTool.getOsBuileVersion() + "," + RxDeviceTool.getAppVersionName(
                    BaseApplication.getApp()
                ),
                RxDeviceTool.getAppVersionName(BaseApplication.getApp()),
                (System.currentTimeMillis() / 1000).toString() + "",
                "1",
                uid,
                netSpeed,
                RxNetTool.getNetWorkTypeName(BaseApplication.getApp())
            )
            var gson = Gson()
            var params = gson.toJson(bean)

            ApiRequestUtil.uploadWebViewTime(params)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({ baseResponse -> LogUtil.d("upload status : " + baseResponse.code + " " + baseResponse.msg) })
                    { throwable -> LogUtil.d("upload log error: $throwable") }
        }
    }
}