package com.dlc.exam.common

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.provider.Settings
import com.dlc.exam.ui.StartActivity
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.math.BigDecimal
import java.math.BigInteger
import java.security.MessageDigest
import kotlin.system.exitProcess

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

    fun getAndroidId(context: Context, defId: String = "NULL"): String {
        return if (Constants.Mock.MACNO_TEST) {
//            return "c80551bd7050b335"
//            return "741a8cb4f8826666"
            return "EXAM_7f60990ad26a9db2"
//            return "55e2dffeeca9660e"
        } else {
            try {
                "EXAM_${
                    Settings.Secure.getString(
                        context.contentResolver,
                        Settings.Secure.ANDROID_ID
                    )
                }"
            } catch (e: Exception) {
                defId
            }
        }
    }

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

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

    fun formatEmpty(value: String?, defValue: String = "0"): String {
        return if (value.isNullOrEmpty()) defValue else value
    }

    /**
     * @param time 秒
     */
    fun formatExamTime(time: Int): String {
        val minute = time / 60
        val second = time % 60
        return "${minute}分${second}秒"
    }

    // 3700s = 3600s + 60s + 40s = 1时1分40秒
    fun formatLearnTime(time: Long): String {
        val hour = time / 3600
        val minute = (time - hour * 3600) / 60
        val second = time % 60
        return if (0L == hour) {
            if (0L == minute)
                "${second}秒"
            else "${minute}分${second}秒"
        } else "${hour}时${minute}分${second}秒"
    }

    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
    }

    fun saveBitmapToFile(bitmap: Bitmap?, filePath: String?) {
        if (bitmap == null || filePath.isNullOrEmpty()) return
        try {
            val file = File(filePath)

            if (!file.exists() && !file.createNewFile()) return

            val bos = BufferedOutputStream(FileOutputStream(file))
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)
            bos.flush()
            bos.close()
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

}