package com.dlc.eboard.common

import android.content.Context
import android.content.Intent
import com.dlc.eboard.R
import com.dlc.eboard.ui.SplashActivity
import com.dlc.eboard.ui.sign.CheckItem
import com.rc.core.util.DeviceUtils
import com.rc.httpcore.vo.response.LaboratoryVo
import java.math.BigDecimal
import java.math.BigInteger
import java.security.MessageDigest
import kotlin.system.exitProcess

/**
 * info
 *
 * @author ReiChin_
 */
object CommonUtils {

    fun getAndroidId(defId: String = "NULL"): String {
        return if (Constants.Mock.MACNO_TEST) {
//            return "c80551bd7050b335"
//            return "741a8cb4f8826666"
//            return "7f60990ad26a9db2"
//            return "EBOARD_25123a0c4371f0f5"
//            return "55e2dffeeca9660e"
//            return "CYB123456789"
//            return "EBOARD_F5C1C40C7CDCD859"
            return "1DS289OIU"
        } else {
            try {
                "EBOARD_${DeviceUtils.getAndroidSN(defId)}"
            } catch (e: Exception) {
                defId
            }
        }
    }

    fun restartApp(appContext: Context) {
        val intent = Intent(appContext, SplashActivity::class.java)
        val restartIntent = Intent.makeRestartActivityTask(intent.component)
        appContext.startActivity(restartIntent)
        exitProcess(0)
    }

    /**
     * 1：佩戴护目镜   2：穿实验服  3：戴手套
     */
    fun nextSafetyCheckItem(data: List<LaboratoryVo.InCheckItem>?, index: Int = 0): CheckItem? {
        if (data.isNullOrEmpty() || index >= data.size) return null
        return when (data[index].code) {
            "1" -> CheckItem("1", "护目镜穿戴安全准入检测", R.mipmap.img_qd_hmj, R.string.tip_info_hmj)
            "2" -> CheckItem("2", "实验服穿戴安全准入检测", R.mipmap.img_qd_syf, R.string.tip_info_syf)
            "3" -> CheckItem("3", "手套穿戴安全准入检测", R.mipmap.img_qd_st, R.string.tip_info_st)
            else -> null
        }
    }

    fun stringParseInt(value: String?): Int {
        try {
            return BigDecimal(value).toInt()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    fun stringToMD5(value: String?): String? {
        if (value.isNullOrEmpty()) return null

        val secretBytes  = MessageDigest.getInstance("MD5").digest(value.toByteArray())
        var md5code: String = BigInteger(1, secretBytes).toString(16)
        for (i in 0 until 32 - md5code.length) {
            md5code = "0$md5code"
        }
        return md5code
    }

}