package cpm.com.gskmtorange.constant

import android.content.Context
import android.util.Log
import android.util.Patterns
import android.widget.ImageView
import androidx.core.net.toUri
import androidx.swiperefreshlayout.widget.CircularProgressDrawable
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.sj.camera_lib_android.utils.CameraSDK.init
import java.io.File

object CommonUtils {
    fun initializePDSDK(context: Context?,projectId:String, _userId: String?) {
        if (context != null) {
            if (_userId != null) {
                init(context = context, projectId = projectId, userId = _userId)
            }
        }
    }

    fun imgPreviewwithError(context: Context, url: String, imgView: ImageView, resId: Int) {
        val circularProgressDrawable = CircularProgressDrawable(context).apply {
            strokeWidth = 5f
            centerRadius = 30f
            start()
        }
        val options: RequestOptions =
            RequestOptions().centerCrop().placeholder(circularProgressDrawable).error(resId)
                .diskCacheStrategy(DiskCacheStrategy.ALL).priority(Priority.HIGH)
        Glide.with(context).load(url).apply(options).into(imgView)
    }

    fun isValidUrl(url: String): Boolean {
        return Patterns.WEB_URL.matcher(url).matches()
    }

    fun deleteSpecificImage(context: Context,filePath: String?) {
        if (!filePath.isNullOrEmpty()) {
           val  file = File(filePath)
            if (file.exists()) {
                val isDeleted = file.delete()
                if (isDeleted) {
                    Log.d("deletefile","File deleted successfully");
                } else {
                    println("Failed to delete file")
                }
            } else {
                println("File does not exist: $filePath")
            }
        }
    }

    fun deleteImage(context: Context, filePath: String) {
        val uri = filePath.toUri()
        val contentResolver = context.contentResolver
        val deleted = contentResolver.delete(uri, null, null)
        if (deleted > 0) {
            println("File deleted successfully: $filePath")
        } else {
            println("Failed to delete file: $filePath")
        }
    }
}