package com.rdiot.yx485.bean


import android.os.Build
import android.util.Base64
import com.rdiot.yx485.BuildConfig
import com.rdiot.yx485.util.curTime
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class YXBean(
    /** 渠道 */
    @SerialName("channel")
    var channel: String = "",
    /** 设备ID */
    @SerialName("device_id")
    var deviceId: String = "",
    /** 设备类型 */
    @SerialName("device_type")
    var deviceType: String = "",
    /** 定位（经度，纬度） */
    @SerialName("location")
    var location: String = "",
    /** mqtt令牌 */
    @SerialName("mqtt_token")
    var mqttToken: String = "",
    /** 网络类型 */
    @SerialName("net_type")
    var netType: String = "",
    /** 操作系统 */
    @SerialName("os")
    var os: String = "",
    /** 系统版本 */
    @SerialName("os_version")
    var osVersion: String = "",
    /** 手机厂商 */
    @SerialName("phone_brand")
    var phoneBrand: String = "",
    /** 手机型号 */
    @SerialName("phone_model")
    var phoneModel: String = "",
    /** 分辨率 高 */
    @SerialName("screen_height")
    var screenHeight: Int = 0,
    /** 分辨率 宽 */
    @SerialName("screen_width")
    var screenWidth: Int = 0,
    /** 来源渠道 */
    @SerialName("source")
    var source: String = "",
    /** 时间戳 */
    @SerialName("timestamp")
    var timestamp: Long = 0,
    /** 用户ID */
    @SerialName("user_id")
    var userId: String = "",
    /** 用户类型 */
    @SerialName("user_type")
    var userType: String = "",
    /** UUID */
    @SerialName("uuid")
    var uuid: String = "",
    /** 应用版本号 */
    @SerialName("version")
    var version: String = ""
) : java.io.Serializable {

    init {
        channel = "Android"
        deviceId = ""
        deviceType = "Android"
        location = ""
        mqttToken = ""
        netType = ""
        os = ""
        osVersion = Build.VERSION.RELEASE
        phoneBrand = Build.BRAND
        phoneModel = Build.MODEL
        screenHeight = 0
        screenWidth = 0
        source = ""
        userId = ""
        userType = ""
        uuid = ""
        version = BuildConfig.VERSION_NAME
    }

    fun toJsonString(): String {
        timestamp = curTime
        return Json.encodeToString(this)
    }

    fun toBase64(): String {
        return Base64.encodeToString(
            toJsonString().toByteArray(),
            Base64.DEFAULT
        ).trim().replace("\n", "")
    }

    companion object {
        private const val serialVersionUID = -48L
    }
}
