package com.yidian.bcommon.utils

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.util.DisplayMetrics
import android.util.Log
import android.util.TypedValue
import android.widget.Toast
import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.services.XEventService
import com.yidian.utils.MD5Util
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.yac.ftdevicefinger.core.FtDeviceFingerManager
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.util.*


object ToolsUtil {
  //防止连续点击
  private var lastClickTime: Long = 0

  //自定义快速点击时间
  private var currentLastClickTime: Long = 0

  //备用自定义快速点击时间
  private var tempLastClickTime: Long = 0

  //快速点击检测
  val isFastClick: Boolean
    get() {
      val time = System.currentTimeMillis()
      val timeD = time - lastClickTime
      if (timeD < 500) {
        return false
      }
      lastClickTime = time
      return true
    }

  //单位转换
  fun dp2px(context: Context, dpVal: Float): Int {
    return TypedValue.applyDimension(
      TypedValue.COMPLEX_UNIT_DIP,
      dpVal,
      context.resources.displayMetrics
    ).toInt()
  }

  fun dp2px(dpValue: Float): Int {
    return TypedValue.applyDimension(1, dpValue, DisplayMetrics()).toInt()
  }

  //格式化展示
  fun formatShowNum(count: Int): String {
    var showCount = count.toString()
    when {
      count in 1000..9999 -> {
        val floutCount = count / 1000
        showCount = floutCount.toString().format("%.1f") + "K"
      }
      count in 10000..99999999 -> {
        val floutCount = count / 10000
        showCount = floutCount.toString().format("%.1f") + "W"
      }
      count >= 100000000 -> {
        val floutCount = count / 100000000
        showCount = floutCount.toString().format("%.1f") + "亿"
      }
    }
    return showCount
  }

  //event事件
  fun doAction(name: String, params: Any) {
    Log.d("song_test", "event_bus.name = $name")
    val xBaseEvent = XBaseEvent(name, params)
    XEventService.postEvent(xBaseEvent)
  }

  //自定义时间检测
  fun isCustomFastClick(timeNum: Long): Boolean {
    val time = System.currentTimeMillis()
    val timeD = time - currentLastClickTime
    if (timeD < timeNum) {
      return false
    }
    currentLastClickTime = time
    return true
  }

  //备用自定义时间检测
  fun isCustomFastStatus(timeNum: Long): Boolean {
    val time = System.currentTimeMillis()
    val timeD = time - tempLastClickTime
    if (timeD < timeNum) {
      return false
    }
    tempLastClickTime = time
    return true
  }

  //机型判断
  fun judgePhone(): Boolean {
    val manufacturer: String = Build.MANUFACTURER
    return if (manufacturer.isNotEmpty()) {
      when (manufacturer.toLowerCase(Locale.ROOT)) {
        "oppo" -> true
        "xiaomi" -> true
        else -> false

      }
    } else false
  }

  fun getYDEncryptionToken(): String {
    val temp = StringBuilder()
    //架构组提供的设备指纹
    temp.append(AppConfig.appid)
    Log.d("song_test", "appid = " + AppConfig.appid)
    temp.append(FtDeviceFingerManager.getDeviceFinger())
    Log.d("song_test", "getDeviceFinger = " + FtDeviceFingerManager.getDeviceFinger())

    return "UMPPTYKO" + MD5Util.md5Encrypt32Upper(temp.toString())
  }

  fun getDeviceBrandMask(): Int {
    val brand = Build.BRAND
    var mask = 64 //0100 0000
    if ("huawei".equals(brand, ignoreCase = true) || "honor".equals(brand, ignoreCase = true)) {
      mask = 32 //0010 0000
    } else if ("meizu".equals(brand, ignoreCase = true)) {
      mask = 48 //0011 0000
    } else if ("xiaomi".equals(brand, ignoreCase = true)) {
      mask = 16 //0001 0000
    } else if ("nubia".equals(brand, ignoreCase = true)) {
      mask = 80 //0101 0000
    } else if ("ZTE".equals(brand, ignoreCase = true)) {
      mask = 96 //0110 0000
    } else if ("oppo".equals(brand, ignoreCase = true)) {
      mask = 112 //0111 0000
    } else if ("vivo".equals(brand, ignoreCase = true)) {
      mask = 128 //0001 0000 0000
    } else if ("samsung".equals(brand, ignoreCase = true)) {
      mask = 144 //0001 0001 0000
    }
    return mask
  }

  fun saveImage(bitmap: Bitmap, name: String, context: Context): Boolean {
    try {
      val sdcardPath = System.getenv("EXTERNAL_STORAGE")
      val dir = "$sdcardPath/life_circle_business/"
      val file = File(dir)
      if (!file.exists()) {
        file.mkdirs()
      }

      val mFile = File(dir + name)

      if (mFile.exists()) {
        Toast.makeText(context, "该图片已存在!", Toast.LENGTH_SHORT).show();
        return false;
      }

      val outputStream = FileOutputStream(mFile)
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
      val uri: Uri = Uri.fromFile(mFile)
      context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri))
      return true
    } catch (e: FileNotFoundException) {
      e.printStackTrace()
    }
    return false
  }

  fun randomName(): String {
    val randomNum = (Math.random() * 1000).toInt()
    return "lifeCircle$randomNum"
  }


}
