package com.yzx.ebrand.utils

import android.os.Build
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.HttpHeaders
import com.lzy.okgo.model.Response
import com.yzx.ebrand.model.User
import java.text.SimpleDateFormat
import java.util.*


object Monitor {
    val DT_URL =
        "https://oapi.dingtalk.com/robot/send?access_token=886b2c609ddbcc1c8ff80abfd847e9c192bbde80815721204799b0b300cd7418"

    fun postDingTalkMsg() {
        val monitorText = SPUtils.getInstance().getString("crashData")
        if (!monitorText.isBlank()) {
            val monitor = MonitorInfo("text", MonitorText(monitorText))
            val headers = HttpHeaders()
            val json = Gson().toJson(monitor)
            headers.put("content-type", "application/json")
            OkGo.post<String>(DT_URL)
                .tag("")
                .headers(headers)
                .upJson(json)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        LogUtils.d(json)
                        LogUtils.d(response)
                        SPUtils.getInstance().remove("crashData")
                    }

                    override fun onError(response: Response<String>?) {
                        super.onError(response)
                        LogUtils.d(response)
                        Thread {
                            Thread.sleep(3000)
                            postDingTalkMsg()
                        }
                    }
                })
        }

    }

    fun saveErrorInfo(
        crashType: Int,
        errorType: String?,
        errorMessage: String?,
        errorStack: String?
    ) {
        val monitorText = "crashType：${crashType}\n" +
                "errorType：${errorType}\n" +
                "errorMessage：${errorMessage}\n" +
                "errorStack：\n${errorStack}"
        val stringBuffer = StringBuffer()
        stringBuffer.append("电子屏异常上报:").append("\n")
        stringBuffer.append("系统信息：").append(AppUtils.getAppInfo().versionName).append("\n")
        val user = User.getUser()
        if (user.user_id > 0) {
            stringBuffer.append("用户信息：").append("\n")
                .append("user_name:${user.user_name}").append("\n")
                .append("user_id:${user.user_id}").append("\n")
                .append("phone:${user.phone}").append("\n")
                .append("token:${user.token}").append("\n")
        }
        stringBuffer.append("崩溃时间：")
            .append(SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()))
            .append("\n")
        stringBuffer.append("手机系统：").append(Build.VERSION.RELEASE).append("\n")
        stringBuffer.append("手机型号：").append(Build.MODEL).append("\n")
        stringBuffer.append("崩溃信息：").append("\n").append(monitorText)
        SPUtils.getInstance().put("crashData", stringBuffer.toString())
    }

    fun postNetError(errorInfo:StringBuilder){
        val stringBuffer = StringBuffer()
        stringBuffer.append("电子屏异常上报:").append("\n")
        stringBuffer.append("异常类型：").append("接口异常").append("\n")
        stringBuffer.append("系统信息：").append(AppUtils.getAppInfo().versionName).append("\n")
        val user = User.getUser()
        if (user.user_id > 0) {
            stringBuffer.append("用户信息：").append("\n")
                .append("user_name:${user.user_name}").append("\n")
                .append("user_id:${user.user_id}").append("\n")
                .append("phone:${user.phone}").append("\n")
                .append("token:${user.token}").append("\n")
        }
        stringBuffer.append("请求时间：")
            .append(SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()))
            .append("\n")
        stringBuffer.append("手机系统：").append(Build.VERSION.RELEASE).append("\n")
        stringBuffer.append("手机型号：").append(Build.MODEL).append("\n")
        stringBuffer.append(errorInfo)
        val monitor = MonitorInfo("text", MonitorText(stringBuffer.toString()))
        val headers = HttpHeaders()
        val json = Gson().toJson(monitor)
        headers.put("content-type", "application/json")
        OkGo.post<String>(DT_URL)
            .tag("")
            .headers(headers)
            .upJson(json)
            .execute(object : StringCallback() {
                override fun onSuccess(response: Response<String>?) {
                    LogUtils.d(json)
                    LogUtils.d(response)
                }

                override fun onError(response: Response<String>?) {
                    super.onError(response)
                    LogUtils.d(response)

                }
            })
    }
}

class MonitorInfo(
    val msgtype: String = "text",
    val text: MonitorText = MonitorText("电子屏异常上报")
)

class MonitorText(val content: String = "")

