package com.sketch.ar.artdrawing.utils


import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Point
import android.net.Uri
import android.util.DisplayMetrics
import android.util.Log
import android.view.MotionEvent
import android.view.WindowManager
import com.sketch.ar.artdrawing.MyApp
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import kotlin.math.sqrt

object CommonUtils {









    fun listAssetFiles(context: Context, folderName: String): List<String> {
        return try {
            val assetManager = context.assets
            val files = assetManager.list(folderName) ?: emptyArray()
            files.toList()
        } catch (e: IOException) {
            e.printStackTrace()
            emptyList()
        }
    }

    fun dpToPx(dpValue: Float): Float {
        val density: Float = MyApp.Companion.mInstance.resources.displayMetrics.density
        return density * dpValue + 0.5f
    }

    fun getDisPlayName(fileName: String): String {
        return fileName.replace("kt_", "")
    }

    fun getScreen(context: Context): Point {
        val displayMetrics = DisplayMetrics()
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        val width = displayMetrics.widthPixels
        val height = displayMetrics.heightPixels
        val point = Point()
        point.x = width
        point.y = height
        return point
    }

    fun getDistance(event: MotionEvent): Float {
        val x = event.getX(0) - event.getX(1)
        val y = event.getY(0) - event.getY(1)
        return sqrt((x * x + y * y).toDouble()).toFloat()
    }
    fun isInternalCacheFile(context: Context, filePath: String): Boolean {
        val internalCachePath = context.cacheDir.absolutePath       // /data/user/0/<package>/cache
        val internalFilePath = context.filesDir.absolutePath // /data/user/0/<package>/files

        return filePath.startsWith(internalCachePath) || filePath.startsWith(internalFilePath)
    }
    fun saveToStorage(fileName: String, fileUri: Uri, context: Context): Boolean {
        try {
            // 获取输入流
            val bitmap =
                BitmapFactory.decodeStream(context.contentResolver.openInputStream(fileUri))
            // 创建图片文件
            val file = File(context.filesDir, fileName)
            val fileOutputStream = FileOutputStream(file)
            // 将Bitmap图片进行存储
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
            fileOutputStream.close()
            return true
        } catch (e: IOException) {
            e.printStackTrace()
            return false
        }
    }
    fun getUriFileSize(context: Context, uri: Uri): Double {
        var fileSize = 0.0
        val contentResolver = context.contentResolver

        // 如果Uri是文件类型
        if (uri.scheme == "file") {
            val file = File(uri.path)
            fileSize = file.length().toDouble()
        } else { // 如果Uri不是文件类型，则通过ContentResolver获取大小
            try {
                fileSize = contentResolver.openFileDescriptor(uri, "r")!!.statSize.toDouble()
            } catch (e: java.lang.Exception) {
                Log.e("UriUtils", "Error while retrieving file size: " + e.message)
            }
        }
        fileSize = fileSize / 1048576.0
        return fileSize
    }
    fun getImportPath(name: String): String {
        val filesDir: File = MyApp.Companion.mInstance.filesDir
        val s = "$filesDir/$name"
        return s
    }
}