package com.swago.baseswago.util

import android.Manifest
import android.os.Build
import android.text.TextUtils
import com.permissionx.guolindev.PermissionX
import java.io.*
import java.util.*

/**
 *@date 2021/12/18 16:16
 *description:
 */
object SwagoInfo {

    private var versionCode = ""
    private var languageCode = "en"
    var languageId = 0
    private var appClient = "android"
    private var channel = "DDD04"
    private var imei = ""

    fun isGooglePackage():Boolean{
        return true
    }

    fun getLanguageCode(): String {
        val language = Locale.getDefault().language
        when (language) {
            "en" -> {
                languageCode = "en"
                languageId = 2
            }
            "in" -> {
                languageCode = "id"
                languageId = 3
            }
            "ms" -> {
                languageCode = "ms"
                languageId = 4
            }
            "ar" -> {
                languageCode = "ar"
                languageId = 5
            }
            "zh" -> {
                languageCode = "zh"
                languageId = 1
            }
            else -> {
                languageCode = "en"
                languageId = 2
            }
        }
        return languageCode
    }


    fun getDeviceInfo():String{
        return Build.BRAND + "--" + Build.MODEL
    }

    fun getVersionCode():String{
        if (TextUtils.isEmpty(versionCode)) {
            versionCode = try {
                val context = AppContext.getContext()
                val packageManager = context.packageManager
                val info = packageManager.getPackageInfo(context.packageName, 0)
                info.versionCode.toString()
            } catch (e: Exception) {
                e.printStackTrace()
                "-1"
            }
        }
        return versionCode
    }

    fun getAppClient():String{
        return appClient
    }

    fun getChannel():String{
        return channel
    }


    fun getIMEI():String{
        if (imei.isEmpty()){
            imei = getUniqueDeviceId()
        }
        return imei
    }

    private fun getUniqueDeviceId():String{
        if (PermissionX.isGranted(AppContext.getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)){
           if (AppContext.getContext().getExternalFilesDir(null) == null){
               return ""
           }else{
               val path = "${AppContext.getContext().getExternalFilesDir(null)!!.path}/swagoFile"
               val pathFile = File("$path/uuid.txt")
               if (pathFile.exists()){
                   var br: BufferedReader? = null
                   var isr: InputStreamReader? = null
                   try {
                       isr = InputStreamReader(FileInputStream(pathFile))
                       br = BufferedReader(isr)
                       val stringBuilder = StringBuffer()
                       var line:String?
                       while (br.readLine().also { line = it } != null) {
                           stringBuilder.append(line)
                       }
                       LogUtil.d("设备唯一标识","本地文件获取："+stringBuilder.toString())
                       return stringBuilder.toString()
                   }catch (e:Exception){
                       e.printStackTrace()
                   }finally {
                       try {
                           isr?.close()
                           br?.close()
                       } catch (e: Exception) {
                           e.printStackTrace()
                       }
                   }
               }else{
                   File(path).mkdirs()
                   pathFile.createNewFile()
                   val rawImei = "${System.currentTimeMillis()}${"Android ${Build.VERSION.RELEASE}"}"
                   val imei = MD5Util.md5Hex(rawImei)
                   var writer:FileWriter? = null
                   try {
                       writer = FileWriter(pathFile)
                       writer.write(imei)
                       LogUtil.d("设备唯一标识","创建的时候获取："+imei)
                       return imei
                   }catch (e:Exception){
                       e.printStackTrace()
                   }finally {
                       try {
                           writer?.close()
                       } catch (e: Exception) {
                           e.printStackTrace()
                       }
                   }
               }
           }
        }
        return ""
    }

}