package com.secspace.lib.common.utils

import android.app.Activity
import android.content.Context
import android.os.Environment
import android.telephony.TelephonyManager
import android.util.Log
import com.skr.activation.utils.DesUtil
import java.io.File
import java.io.FileWriter
import java.util.*

object ImeiUtil {
    private val TEMP_DIR = Environment.getExternalStorageDirectory().toString() + "/.uuid"
    private const val TEMP_FILE_UUID_NAME = ".system_file_uuid"
    private const val TEMP_FILE_IMEI_NAME = ".system_file_imei"
    private const val TAG = "ImeiUtil"

    fun uniqueId(context: Context?, show: Boolean): String {
        val imei = imei(context)
        var uuid = uuid()
        var uniqueId: String
        if (show) {
            uuid = uuid.substring(uuid.length - 15)
            uniqueId = "$uuid\n$imei"
        } else {
            uuid = uuid.substring(uuid.length - 15)
            uniqueId = "$uuid-$imei"
        }
        return uniqueId
    }

    private fun imei(context: Context?): String {
        var imei = ""
        val applicationFileDir = File(TEMP_DIR)
        if (!applicationFileDir.exists()) {
            if (!applicationFileDir.mkdirs()) {
                Log.e(TAG, "Folder creation failed: ${applicationFileDir.path}")
            }
        }
        val file = File(applicationFileDir, TEMP_FILE_IMEI_NAME)
        if (!file.exists()) {
            val tm: TelephonyManager = context?.getSystemService(Activity.TELEPHONY_SERVICE) as TelephonyManager
            imei = try {
                tm.deviceId
            } catch (e: Exception) {
                Log.e(TAG, "No imei was obtained. error is ${e.message}")
                "unKnown"
            }
            if (imei != "unKnown") {
                if (file.createNewFile()) {
                    val fileWriter = FileWriter(file, false)
                    fileWriter.use {
                        fileWriter.write(DesUtil.encrypt(imei))
                    }
                } else {
                    Log.e(TAG, "createNewFile failed")
                }
            }
        } else {
            imei = DesUtil.decrypt(file.readText())
            Log.i(TAG, "imei  $imei")
        }
        return imei
    }

    private fun uuid(): String {
        val deviceUuid = UUID.randomUUID()
        var uuid = deviceUuid.toString().replace("-", "")
        val applicationFileDir = File(TEMP_DIR)
        if (!applicationFileDir.exists()) {
            if (!applicationFileDir.mkdirs()) {
                Log.e(TAG, "Folder creation failed: ${applicationFileDir.path}")
            }
        }
        val file = File(applicationFileDir, TEMP_FILE_UUID_NAME)
        if (!file.exists()) {
            if (file.createNewFile()) {
                val fileWriter = FileWriter(file, false)
                fileWriter.use {
                    fileWriter.write(DesUtil.encrypt(uuid))
                }
            } else {
                Log.e(TAG, "createNewFile failed")
            }
        } else {
            uuid = DesUtil.decrypt(file.readText())
            Log.i(TAG, "uuid  $uuid")
        }
        return uuid
    }
}

