package org.foxarmy.barcodescannerforemployees

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider
import androidx.core.graphics.scale
import com.google.firebase.components.BuildConfig
import java.io.File
import java.io.FileOutputStream
import java.net.URLEncoder
import java.security.MessageDigest
import java.util.*

fun getImageUri(activity: Activity, imageFile: File): Uri? {
    return FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + "." + activity.localClassName + ".provider", imageFile)
}

@RequiresApi(Build.VERSION_CODES.R)
fun generateThumbnailForImage(context: Context, imageHash: String) {
    val thumbnailsDir = File(context.cacheDir, "thumbnails")
    thumbnailsDir.mkdirs()
    val picturesDir = File(context.filesDir, "pictures")
    picturesDir.mkdirs()
    val thumbnailFile = File(thumbnailsDir, "$imageHash.webp")
    if (thumbnailFile.exists()) return

    val imageFile = File(picturesDir, "$imageHash.png")
    val imageContent = imageFile.inputStream().readBytes()
    var img = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.size)
    img = img.scale(img.width/4,img.height/4)
    img.compress(Bitmap.CompressFormat.WEBP_LOSSY, 25, FileOutputStream(thumbnailFile))
}

@OptIn(ExperimentalStdlibApi::class)
fun String.md5(): String {
    val md = MessageDigest.getInstance("MD5")
    val digest = md.digest(this.toByteArray())
    return digest.toHexString()
}

fun stripNetWeight (netWeight: String): Double {
    return removeSubstringsFromString(netWeight, arrayOf("Л", "л", "мл", "Мл", "г", "Г", "кг", "Кг", "шт", "Шт", ",", " ", ".")).toDouble()
}

fun removeSubstringsFromString(text: String, toRemove: Array<String>): String {
    var result = text
    for (candidate in toRemove.iterator()) {
        result = result.replace(candidate, "")
    }
    return result
}

fun String.utf8(): String = URLEncoder.encode(this, "UTF-8")

fun getActivity(context: Context?): Activity? {
    if (context == null) {
        return null
    } else if (context is ContextWrapper) {
        return if (context is Activity) {
            context
        } else {
            getActivity(context.baseContext)
        }
    }

    return null
}

@RequiresApi(Build.VERSION_CODES.O)
fun calculateProductFreshness(dateOfProduction: Long, dateOfExpiry: Long): Double {

    val productLifeSpan = dateOfExpiry - dateOfProduction;
    val lifeSpanLeft: Long = dateOfExpiry - Date().time / 1000

    return lifeSpanLeft / productLifeSpan.toDouble()
}

fun getUnitNameById (context: Context, id: Int): String {
    return when(id) {
        0 -> { context.getString(R.string.kilogram) }
        1 -> { context.getString(R.string.gram) }
        2 -> { context.getString(R.string.liter) }
        3 -> { context.getString(R.string.milliliter) }
        4 -> { context.getString(R.string.pieces) }
        else -> { "" }
    }
}
